@NotNull public static Optional<PsiAnnotationMemberValue> getAnnotationAttributeValue(@NotNull PsiModifierListOwner target, @NotNull Annotation annotation, @NotNull String attrName) { if (!isAnnotationPresent(target, annotation)) { return Optional.absent(); } Optional<PsiAnnotation> psiAnnotation = getPsiAnnotation(target, annotation); return psiAnnotation.isPresent() ? Optional.fromNullable(psiAnnotation.get().findAttributeValue(attrName)) : Optional.<PsiAnnotationMemberValue>absent(); }
private boolean isTargetField(PsiField field) { if (JavaUtils.isAnnotationPresent(field, Annotation.AUTOWIRED)) { return true; } Optional<PsiAnnotation> resourceAnno = JavaUtils.getPsiAnnotation(field, Annotation.RESOURCE); if (resourceAnno.isPresent()) { PsiAnnotationMemberValue nameValue = resourceAnno.get().findAttributeValue("name"); String name = nameValue.getText().replaceAll("\"", ""); return StringUtils.isBlank(name) || name.equals(field.getName()); } return false; }
public static int getRestrictionScope(@NonNull PsiAnnotation annotation) { PsiAnnotationMemberValue value = annotation.findAttributeValue(ATTR_VALUE); return getRestrictionScope(value); }
@Nullable public static String getStringAttributeValue(@NotNull PsiAnnotation anno, @Nullable final String attributeName) { PsiAnnotationMemberValue attrValue = anno.findAttributeValue(attributeName); Object constValue = JavaPsiFacade.getInstance(anno.getProject()).getConstantEvaluationHelper().computeConstantExpression(attrValue); return constValue instanceof String ? (String)constValue : null; } @Nullable
@Nullable public static Boolean getBooleanAttributeValue(@NotNull PsiAnnotation anno, @Nullable final String attributeName) { PsiAnnotationMemberValue attrValue = anno.findAttributeValue(attributeName); Object constValue = JavaPsiFacade.getInstance(anno.getProject()).getConstantEvaluationHelper().computeConstantExpression(attrValue); return constValue instanceof Boolean ? (Boolean)constValue : null; }
public static int getVisibilityForTesting(@NonNull PsiAnnotation annotation) { PsiAnnotationMemberValue value = annotation.findAttributeValue(ATTR_OTHERWISE); if (value instanceof PsiLiteral) { Object v = ((PsiLiteral) value).getValue(); if (v instanceof Integer) { return (Integer)v; } } else if (value instanceof PsiReferenceExpression) { // Not compiled; this is unlikely (but can happen when editing the support // library project itself) PsiReferenceExpression referenceExpression = (PsiReferenceExpression)value; String name = referenceExpression.getReferenceName(); if ("NONE".equals(name)) { return VISIBILITY_NONE; } else if ("PRIVATE".equals(name)) { return VISIBILITY_PRIVATE; } else if ("PROTECTED".equals(name)) { return VISIBILITY_PROTECTED; } else if ("PACKAGE_PRIVATE".equals(name)) { return VISIBILITY_PACKAGE_PRIVATE; } } return VISIBILITY_PRIVATE; // the default }
private String getAttributeFromAnnotation( PsiAnnotation annotation, String annotationType, final String attribute) throws InvalidAnnotationException, MissingAttributeException { String annotationQualifiedName = annotation.getQualifiedName(); if (annotationQualifiedName == null) { throw new InvalidAnnotationException(annotation, annotationType); } if (annotationQualifiedName.equals(annotationType)) { PsiAnnotationMemberValue annotationMemberValue = annotation.findAttributeValue(attribute); if (annotationMemberValue == null) { throw new MissingAttributeException(annotation, attribute); } String httpMethodWithQuotes = annotationMemberValue.getText(); return httpMethodWithQuotes.substring(1, httpMethodWithQuotes.length() - 1); } else { throw new InvalidAnnotationException(annotation, annotationType); } }
private static boolean isNullabilityDefault(@NotNull PsiAnnotation annotation, boolean nullable, PsiAnnotation.TargetType[] placeTargetTypes) { PsiJavaCodeReferenceElement element = annotation.getNameReferenceElement(); PsiElement declaration = element == null ? null : element.resolve(); if (!(declaration instanceof PsiClass)) return false; if (!AnnotationUtil.isAnnotated((PsiClass)declaration, nullable ? JAVAX_ANNOTATION_NULLABLE : JAVAX_ANNOTATION_NONNULL, false, true)) { return false; } PsiAnnotation tqDefault = AnnotationUtil.findAnnotation((PsiClass)declaration, true, "javax.annotation.meta.TypeQualifierDefault"); if (tqDefault == null) return false; Set<PsiAnnotation.TargetType> required = AnnotationTargetUtil.extractRequiredAnnotationTargets(tqDefault.findAttributeValue(null)); if (required == null) return false; return required.isEmpty() || ContainerUtil.intersects(required, Arrays.asList(placeTargetTypes)); }
annotation.findAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); if (ownerDomainMember == null) { return; annotation.findAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); if (ownerNameMember == null) { return;
annotation.findAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); if (ownerDomainMember == null) { return; annotation.findAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); if (ownerNameMember == null) { return; annotation.findAttributeValue(API_NAMESPACE_PACKAGE_PATH_ATTRIBUTE).getText());
@Override public Object invoke(Object proxy, Method method, Object[] args) { Class<?>[] paramTypes = method.getParameterTypes(); assert paramTypes.length == 0: Arrays.toString(paramTypes); String member = method.getName(); if (member.equals("toString")) { return toStringImpl(); } if (member.equals("annotationType")) { return type; } // Handle annotation member accessors PsiAnnotationMemberValue value = myAnnotation.findAttributeValue(member); if (value == null) { throw new IncompleteAnnotationException(type, member+". (Unable to find attribute in '"+myAnnotation.getText()+"')"); } Object result = JavaPsiFacade.getInstance(myAnnotation.getProject()).getConstantEvaluationHelper().computeConstantExpression(value); if (result == null) { throw new IncompleteAnnotationException(type, member+". (Unable to evaluate annotation value '"+value+"')"); } // todo arrays return result; }
.getConstantEvaluationHelper(); PsiAnnotationMemberValue tempValues = psa_fields.findAttributeValue("value"); if(tempValues == null){ return false; if(child instanceof PsiAnnotation){ PsiAnnotation expect = (PsiAnnotation) child; String prop = (String) evaluation.computeConstantExpression(expect.findAttributeValue("propName")); PsiAnnotationMemberValue typeVal = expect.findAttributeValue("type"); boolean is_bool = false; if(typeVal instanceof PsiExpression){
@Override public void visitAnnotation(PsiAnnotation annotation) { if (!EndpointUtilities.isEndpointClass(annotation)) { return; } if (!GctConstants.APP_ENGINE_ANNOTATION_API_METHOD.equals(annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue memberValue = annotation.findAttributeValue(API_NAME_ATTRIBUTE); if (memberValue == null) { return; } String nameValueWithQuotes = memberValue.getText(); String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes); if (nameValue.isEmpty()) { return; } if (!API_NAME_PATTERN .matcher(EndpointUtilities.collapseSequenceOfDots(nameValue)) .matches()) { holder.registerProblem( memberValue, "Invalid method name: letters, digits, underscores and dots are acceptable " + "characters. Leading and trailing dots are prohibited.", new MyQuickFix()); } } };
annotation.findAttributeValue(API_NAME_ATTRIBUTE); if (annotationMemberValue == null) { return;
private void initializePsiMethod(String methodName, String httpMethodValue, String pathValue) { PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue1.getText()).thenReturn(httpMethodValue); PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue2.getText()).thenReturn(pathValue); PsiAnnotation mockAnnotation = mock(PsiAnnotation.class); when(mockAnnotation.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_METHOD); when(mockAnnotation.findAttributeValue("httpMethod")).thenReturn(mockAnnotationMemberValue1); when(mockAnnotation.findAttributeValue("path")).thenReturn(mockAnnotationMemberValue2); PsiAnnotation[] mockAnnotationsArray = {mockAnnotation}; PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray); mockPsiMethod = mock(PsiMethod.class); when(mockPsiMethod.getModifierList()).thenReturn(mockModifierList); when(mockPsiMethod.getName()).thenReturn(methodName); when(mockPsiMethod.getContainingClass()).thenReturn(mockPsiClass); PsiParameterList mockParameterList = mock(PsiParameterList.class); when(mockParameterList.getParameters()).thenReturn(new PsiParameter[0]); when(mockPsiMethod.getParameterList()).thenReturn(mockParameterList); }
private void initializePsiClass(String apiResource, String apiClassResource) { PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue1.getText()).thenReturn(apiResource); PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue2.getText()).thenReturn(apiClassResource); // Mock @Api(resource = "") PsiAnnotation mockAnnotation1 = mock(PsiAnnotation.class); when(mockAnnotation1.getQualifiedName()).thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API); when(mockAnnotation1.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue1); // Mock @ApiClass(resource = "") PsiAnnotation mockAnnotation2 = mock(PsiAnnotation.class); when(mockAnnotation2.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_CLASS); when(mockAnnotation2.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue2); PsiAnnotation[] mockAnnotationsArray = {mockAnnotation1, mockAnnotation2}; PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray); mockPsiClass = mock(PsiClass.class); when(mockPsiClass.getModifierList()).thenReturn(mockModifierList); } }
private void ensureUniqueValues(@NonNull PsiAnnotation node) { PsiAnnotationMemberValue value = node.findAttributeValue(ATTR_VALUE); if (value == null) { value = node.findAttributeValue(null);
private void initializePsiClass(boolean hasApiTransformerApi, boolean hasTransformerAttribute) { PsiAnnotation mockTransformerAnnotation = null; PsiAnnotation mockApiAnnotation = null; if (hasApiTransformerApi) { // Mock @ApiTransformer(MyTransformer.class)) mockTransformerAnnotation = mock(PsiAnnotation.class); when(mockTransformerAnnotation.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_TRANSFORMER); } if (hasTransformerAttribute) { PsiAnnotationMemberValue mockAnnotationMemberValue = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue.getText()).thenReturn("MyTransformer.class"); // Mock @Api(transformer = MyTransformer.class) mockApiAnnotation = mock(PsiAnnotation.class); when(mockApiAnnotation.getQualifiedName()).thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API); when(mockApiAnnotation.findAttributeValue(API_TRANSFORMER_ATTRIBUTE)) .thenReturn(mockAnnotationMemberValue); } PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API_TRANSFORMER)) .thenReturn(mockTransformerAnnotation); when(mockModifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API)) .thenReturn(mockApiAnnotation); mockPsiClass = mock(PsiClass.class); when(mockPsiClass.getModifierList()).thenReturn(mockModifierList); }
if (apiAnnotation != null) { PsiAnnotationMemberValue transformerMember = apiAnnotation.findAttributeValue(API_TRANSFORMER_ATTRIBUTE); if (transformerMember != null && !transformerMember.getText().equals("{}")) { return true;
@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); } }