@Nullable public static Long getAnnotationLongValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof Number) { return ((Number)o).longValue(); } } } return null; }
@Nullable public static Double getAnnotationDoubleValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof Number) { return ((Number)o).doubleValue(); } } } return null; }
@Nullable public static Boolean getAnnotationBooleanValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof Boolean) { return (Boolean) o; } } } return null; }
@Nullable public static String getAnnotationStringValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof String) { return (String) o; } } } return null; }
@NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null);
@Override protected void run(final Result result) throws Throwable { // @A(ownerName = "YourCo", ownerDomain = "your-company.com") String annotationString = "@A(" + API_NAMESPACE_NAME_ATTRIBUTE + " = \"" + SUGGESTED_OWNER_ATTRIBUTE + "\", " + API_NAMESPACE_DOMAIN_ATTRIBUTE + " = \"" + "your-company.com" + "\")"; PsiAnnotation newAnnotation = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText(annotationString, null); PsiAnnotationMemberValue newDomainMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); PsiAnnotationMemberValue newNameMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE, newNameMemberValue); annotation.setDeclaredAttributeValue( API_NAMESPACE_DOMAIN_ATTRIBUTE, newDomainMemberValue); } }.execute();
.map( psiAnnotation -> psiAnnotation.findDeclaredAttributeValue( "target" ) ) .filter( Objects::nonNull ) .map( ElementManipulators::getValueText )
@Override protected void run(final Result result) throws Throwable { // @A(ownerDomain = "your-company.com") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_NAMESPACE_DOMAIN_ATTRIBUTE + " = \"" + SUGGESTED_DOMAIN_ATTRIBUTE + "\")", null) .findDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE, newMemberValue); } }.execute();
@Override protected void run(final Result result) throws Throwable { // @A(ownerName = "YourCo") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_NAMESPACE_NAME_ATTRIBUTE + " = \"" + SUGGESTED_OWNER_ATTRIBUTE + "\")", null) .findDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE, newMemberValue); } }.execute();
PsiAnnotationMemberValue value = annotation.findDeclaredAttributeValue(ATTR_VALUE); if (value == null) { value = annotation.findDeclaredAttributeValue(null);
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (!(element instanceof PsiAnnotation)) { return; } PsiAnnotation annotation = (PsiAnnotation) element; if (!GctConstants.APP_ENGINE_ANNOTATION_API_METHOD.equals(annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue apiMethodNameAttribute = annotation.findAttributeValue(API_METHOD_NAME_ATTRIBUTE); if (apiMethodNameAttribute == null) { return; } // Get name String nameValueWithQuotes = apiMethodNameAttribute.getText(); String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes); // @A(name = "someName") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_METHOD_NAME_ATTRIBUTE + " = \"" + nameValue + "_1\")", null) .findDeclaredAttributeValue(API_METHOD_NAME_ATTRIBUTE); apiMethodNameAttribute.replace(newMemberValue); } }