public static boolean appendAnnotations(@NotNull StringBuilder sb, @NotNull List<PsiAnnotation> annotations, boolean canonical) { boolean updated = false; for (PsiAnnotation annotation : annotations) { if (canonical) { String name = annotation.getQualifiedName(); if (name != null) { sb.append('@').append(name).append(annotation.getParameterList().getText()).append(' '); updated = true; } } else { PsiJavaCodeReferenceElement refElement = annotation.getNameReferenceElement(); if (refElement != null) { sb.append('@').append(refElement.getText()).append(' '); updated = true; } } } return updated; } }
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)); }
PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement(); if (ref == null) { continue;
/** * Searches for a name node corresponding to the given node * @return the name node to use, if applicable */ @Nullable public static PsiElement findNameElement(@NonNull PsiElement element) { if (element instanceof PsiClass) { if (element instanceof PsiAnonymousClass) { return ((PsiAnonymousClass)element).getBaseClassReference(); } return ((PsiClass) element).getNameIdentifier(); } else if (element instanceof PsiMethod) { return ((PsiMethod) element).getNameIdentifier(); } else if (element instanceof PsiMethodCallExpression) { return ((PsiMethodCallExpression) element).getMethodExpression(). getReferenceNameElement(); } else if (element instanceof PsiNewExpression) { return ((PsiNewExpression) element).getClassReference(); } else if (element instanceof PsiField) { return ((PsiField)element).getNameIdentifier(); } else if (element instanceof PsiAnnotation) { return ((PsiAnnotation)element).getNameReferenceElement(); } else if (element instanceof PsiReferenceExpression) { return ((PsiReferenceExpression) element).getReferenceNameElement(); } else if (element instanceof PsiLabeledStatement) { return ((PsiLabeledStatement)element).getLabelIdentifier(); } return null; }
.getNameReferenceElement(); if (referenceElement != null) { PsiElement resolved = referenceElement.resolve();