33import com .puppycrawl .tools .checkstyle .api .AbstractCheck ;
44import com .puppycrawl .tools .checkstyle .api .DetailAST ;
55import com .puppycrawl .tools .checkstyle .api .TokenTypes ;
6+ import org .jspecify .annotations .NullMarked ;
7+ import org .jspecify .annotations .NullUnmarked ;
8+ import org .jspecify .annotations .Nullable ;
69
710import java .util .ArrayList ;
811
12+ @ NullMarked
913public class JspecifyMapStructMapperAnnotationCheck extends AbstractCheck {
1014
1115 public static final String MSG_KEY = "jspecify.mapstruct.annotation.invalid" ;
1216
1317 private String mapperAnnotationName = "Mapper" ;
1418 private String annotateWithAnnotationName = "AnnotateWith" ;
15- private String nullUnmarkedAnnotationName = " NullUnmarked" ;
19+ private String nullUnmarkedAnnotationName = NullUnmarked . class . getSimpleName () ;
1620
21+ // The three setters below are called by Checkstyle via reflection for the corresponding <property .../> entries.
22+ @ SuppressWarnings ("unused" )
1723 public void setMapperAnnotationName (String name ) {
1824 this .mapperAnnotationName = name ;
1925 }
2026
27+ @ SuppressWarnings ("unused" )
2128 public void setAnnotateWithAnnotationName (String name ) {
2229 this .annotateWithAnnotationName = name ;
2330 }
2431
32+ @ SuppressWarnings ("unused" )
2533 public void setNullUnmarkedAnnotationName (String name ) {
2634 this .nullUnmarkedAnnotationName = name ;
2735 }
@@ -68,7 +76,7 @@ public void visitToken(DetailAST ast) {
6876 logInvalid (ast );
6977 return ;
7078 }
71- var last = annotations .get ( annotations . size () - 1 );
79+ var last = annotations .getLast ( );
7280 var secondLast = annotations .get (annotations .size () - 2 );
7381 var lastOk = nullUnmarkedAnnotationName .equals (AnnotationNames .simpleName (last ));
7482 var secondLastOk = annotateWithAnnotationName .equals (AnnotationNames .simpleName (secondLast ))
@@ -97,7 +105,7 @@ private static boolean annotateWithArgumentReferences(DetailAST annotation, Stri
97105 return false ;
98106 }
99107
100- private static String memberValuePairClassLiteralName (DetailAST pair ) {
108+ private static @ Nullable String memberValuePairClassLiteralName (DetailAST pair ) {
101109 var ident = pair .findFirstToken (TokenTypes .IDENT );
102110 if (ident == null || !"value" .equals (ident .getText ())) {
103111 return null ;
@@ -106,26 +114,14 @@ private static String memberValuePairClassLiteralName(DetailAST pair) {
106114 return expr == null ? null : classLiteralName (expr );
107115 }
108116
109- private static String classLiteralName (DetailAST expr ) {
117+ private static @ Nullable String classLiteralName (DetailAST expr ) {
110118 var dot = expr .findFirstToken (TokenTypes .DOT );
111119 if (dot == null ) {
112120 return null ;
113121 }
114- var classLiteral = dot .findFirstToken (TokenTypes .LITERAL_CLASS );
115- if (classLiteral == null ) {
122+ if (dot .findFirstToken (TokenTypes .LITERAL_CLASS ) == null ) {
116123 return null ;
117124 }
118- var ident = dot .findFirstToken (TokenTypes .IDENT );
119- if (ident != null ) {
120- return ident .getText ();
121- }
122- var nestedDot = dot .findFirstToken (TokenTypes .DOT );
123- if (nestedDot != null ) {
124- var last = nestedDot .getLastChild ();
125- if (last != null && last .getType () == TokenTypes .IDENT ) {
126- return last .getText ();
127- }
128- }
129- return null ;
125+ return AnnotationNames .lastIdentIn (dot );
130126 }
131127}
0 commit comments