Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
*/
public class AnnotationDiagnosticsCollector extends AbstractDiagnosticsCollector {

private static final String[] VALID_ANNOTATIONS = { AnnotationConstants.GENERATED_FQ_NAME };
private static final String[] VALID_TYPE_ANNOTATIONS = { AnnotationConstants.GENERATED_FQ_NAME,
AnnotationConstants.RESOURCE_FQ_NAME };
private static final String[] VALID_METHOD_ANNOTATIONS = { AnnotationConstants.GENERATED_FQ_NAME,
AnnotationConstants.POST_CONSTRUCT_FQ_NAME, AnnotationConstants.PRE_DESTROY_FQ_NAME,
AnnotationConstants.RESOURCE_FQ_NAME };

public AnnotationDiagnosticsCollector() {
super();
}
Expand All @@ -59,57 +66,31 @@ protected String getDiagnosticSource() {
public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
if (unit != null) {
ArrayList<Tuple.Two<PsiAnnotation, PsiElement>> annotatables = new ArrayList<Tuple.Two<PsiAnnotation, PsiElement>>();
String[] validAnnotations = { AnnotationConstants.GENERATED_FQ_NAME };
String[] validTypeAnnotations = { AnnotationConstants.GENERATED_FQ_NAME,
AnnotationConstants.RESOURCE_FQ_NAME };
String[] validMethodAnnotations = { AnnotationConstants.GENERATED_FQ_NAME,
AnnotationConstants.POST_CONSTRUCT_FQ_NAME, AnnotationConstants.PRE_DESTROY_FQ_NAME,
AnnotationConstants.RESOURCE_FQ_NAME };

PsiPackage psiPackage = JavaPsiFacade.getInstance(unit.getProject())
.findPackage(unit.getPackageName());
if (psiPackage != null) {
PsiAnnotation[] pkgAnnotations = psiPackage.getAnnotations();
for (PsiAnnotation annotation : pkgAnnotations) {
if (isValidAnnotation(annotation.getQualifiedName(), validAnnotations))
annotatables.add(new Tuple.Two<>(annotation, psiPackage));
}
processAnnotations(psiPackage, annotatables, VALID_ANNOTATIONS);
}

PsiClass[] types = unit.getClasses();
for (PsiClass type : types) {
// Type
PsiAnnotation[] annotations = type.getAnnotations();
for (PsiAnnotation annotation : annotations) {
if (isValidAnnotation(annotation.getQualifiedName(), validTypeAnnotations))
annotatables.add(new Tuple.Two<>(annotation, type));
}
processAnnotations(type, annotatables, VALID_TYPE_ANNOTATIONS);
// Method
PsiMethod[] methods = type.getMethods();
for (PsiMethod method : methods) {
annotations = method.getAnnotations();
for (PsiAnnotation annotation : annotations) {
if (isValidAnnotation(annotation.getQualifiedName(), validMethodAnnotations))
annotatables.add(new Tuple.Two<>(annotation, method));
}
processAnnotations(method, annotatables, VALID_METHOD_ANNOTATIONS);
// method parameters
PsiParameter[] parameters = method.getParameterList().getParameters();
for (PsiParameter parameter : parameters) {
annotations = parameter.getAnnotations();
for (PsiAnnotation annotation : annotations) {
if (isValidAnnotation(annotation.getQualifiedName(), validAnnotations))
annotatables.add(new Tuple.Two<>(annotation, parameter));
}
processAnnotations(parameter, annotatables, VALID_ANNOTATIONS);
}
}
// Field
PsiField[] fields = type.getFields();
for (PsiField field : fields) {
annotations = field.getAnnotations();
for (PsiAnnotation annotation : annotations) {
if (isValidAnnotation(annotation.getQualifiedName(), validTypeAnnotations))
annotatables.add(new Tuple.Two<>(annotation, field));
}
processAnnotations(field, annotatables, VALID_TYPE_ANNOTATIONS);
}
}

Expand Down Expand Up @@ -224,6 +205,16 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
}
}

private void processAnnotations(PsiJvmModifiersOwner psiModifierOwner,
ArrayList<Tuple.Two<PsiAnnotation, PsiElement>> annotatables,
String[] validAnnotations) {
PsiAnnotation[] annotations = psiModifierOwner.getAnnotations();
for (PsiAnnotation annotation : annotations) {
if (isValidAnnotation(annotation.getQualifiedName(), validAnnotations))
annotatables.add(new Tuple.Two<>(annotation, psiModifierOwner));
}
}

private static boolean isValidAnnotation(String annotationName, String[] validAnnotations) {
if (annotationName != null) {
for (String fqName : validAnnotations) {
Expand Down