2222import com .google .errorprone .BugPattern .SeverityLevel ;
2323import com .google .errorprone .VisitorState ;
2424import com .google .errorprone .bugpatterns .BugChecker ;
25+ import com .google .errorprone .bugpatterns .BugChecker .ClassTreeMatcher ;
26+ import com .google .errorprone .bugpatterns .BugChecker .PackageTreeMatcher ;
2527import com .google .errorprone .fixes .SuggestedFix ;
2628import com .google .errorprone .fixes .SuggestedFixes ;
2729import com .google .errorprone .matchers .AnnotationMatcherUtils ;
3234import com .sun .source .tree .AnnotationTree ;
3335import com .sun .source .tree .ClassTree ;
3436import com .sun .source .tree .ExpressionTree ;
37+ import com .sun .source .tree .PackageTree ;
38+ import com .sun .source .tree .Tree ;
3539import com .sun .tools .javac .code .Symbol ;
3640import com .sun .tools .javac .code .Type ;
37- import java .lang .annotation .ElementType ;
3841import java .lang .annotation .Retention ;
3942import java .lang .annotation .RetentionPolicy ;
40- import java .lang .annotation .Target ;
4143import java .util .List ;
4244import javax .annotation .Nullable ;
4345
5052 + "retention forces consumers to add a Immutables annotations to their compile classpath."
5153 + "Instead use a meta-annotation with SOURCE retention."
5254 + "See https://github.com/immutables/immutables/issues/291." )
53- public final class ImmutablesStyle extends BugChecker implements BugChecker . ClassTreeMatcher {
55+ public final class ImmutablesStyle extends BugChecker implements ClassTreeMatcher , PackageTreeMatcher {
5456
55- private static final Matcher <ClassTree > STYLE_ANNOTATION =
56- Matchers .hasAnnotation ("org.immutables.value.Value$Style" );
57+ private static final Matcher <Tree > STYLE_ANNOTATION = Matchers .hasAnnotation ("org.immutables.value.Value$Style" );
58+
59+ @ Override
60+ public Description matchPackage (PackageTree tree , VisitorState state ) {
61+ if (STYLE_ANNOTATION .matches (tree , state )) {
62+ return buildDescription (tree ).build ();
63+ }
64+
65+ return Description .NO_MATCH ;
66+ }
5767
5868 @ Override
5969 public Description matchClass (ClassTree tree , VisitorState state ) {
6070 if (STYLE_ANNOTATION .matches (tree , state )) {
6171 switch (tree .getKind ()) {
6272 case CLASS , INTERFACE -> {
63- return matchStyleAnnotatedType (tree , state );
73+ return buildDescription (tree ). build ( );
6474 }
6575 case ANNOTATION_TYPE -> {
6676 return matchStyleMetaAnnotation (tree , state );
6777 }
6878 default -> {}
6979 }
7080 }
71- return Description .NO_MATCH ;
72- }
7381
74- private Description matchStyleAnnotatedType (ClassTree tree , VisitorState state ) {
75- if (ASTHelpers .enclosingClass (ASTHelpers .getSymbol (tree )) == null ) {
76- // Top level class, we cannot create new files with suggested fixes, nor should we create
77- // additional top-level classes.
78- return buildDescription (tree ).build ();
79- }
80- SuggestedFix .Builder fix = SuggestedFix .builder ();
81- String qualifiedTarget = SuggestedFixes .qualifyType (state , fix , Target .class .getName ());
82- String qualifiedElementType = SuggestedFixes .qualifyType (state , fix , ElementType .class .getName ());
83- String qualifiedRetention = SuggestedFixes .qualifyType (state , fix , Retention .class .getName ());
84- String qualifiedRetentionPolicy = SuggestedFixes .qualifyType (state , fix , RetentionPolicy .class .getName ());
85- AnnotationTree styleAnnotationTree = getAnnotation (tree , "org.immutables.value.Value$Style" , state );
86- return buildDescription (tree )
87- .addFix (fix .prefixWith (
88- tree ,
89- String .format (
90- "\n @%s(%s.TYPE)\n @%s(%s.SOURCE)\n %s\n @interface %sStyle {}\n @%sStyle\n " ,
91- qualifiedTarget ,
92- qualifiedElementType ,
93- qualifiedRetention ,
94- qualifiedRetentionPolicy ,
95- state .getSourceForNode (styleAnnotationTree ),
96- tree .getSimpleName (),
97- tree .getSimpleName ()))
98- .replace (styleAnnotationTree , "" )
99- .build ())
100- .build ();
82+ return Description .NO_MATCH ;
10183 }
10284
10385 private Description matchStyleMetaAnnotation (ClassTree tree , VisitorState state ) {
@@ -112,6 +94,7 @@ private Description matchStyleMetaAnnotation(ClassTree tree, VisitorState state)
11294 SuggestedFixes .qualifyType (state , fix , RetentionPolicy .class .getName ())));
11395 return buildDescription (tree ).addFix (fix .build ()).build ();
11496 }
97+
11598 ExpressionTree retentionValue = AnnotationMatcherUtils .getArgument (retention , "value" );
11699 Symbol retentionValueSymbol = ASTHelpers .getSymbol (retentionValue );
117100 if (retentionValueSymbol == null
@@ -125,6 +108,7 @@ private Description matchStyleMetaAnnotation(ClassTree tree, VisitorState state)
125108 "%s.SOURCE" , SuggestedFixes .qualifyType (state , fix , RetentionPolicy .class .getName ())))));
126109 return buildDescription (tree ).addFix (fix .build ()).build ();
127110 }
111+
128112 return Description .NO_MATCH ;
129113 }
130114
0 commit comments