|
38 | 38 | import com.google.errorprone.util.ASTHelpers; |
39 | 39 | import com.sun.source.tree.ClassTree; |
40 | 40 | import com.sun.source.tree.ExpressionStatementTree; |
| 41 | +import com.sun.source.tree.IdentifierTree; |
41 | 42 | import com.sun.source.tree.MethodInvocationTree; |
42 | 43 | import com.sun.source.tree.MethodTree; |
43 | 44 | import com.sun.source.tree.StatementTree; |
44 | 45 | import com.sun.source.tree.Tree; |
45 | 46 | import com.sun.tools.javac.code.Symbol.MethodSymbol; |
46 | | -import java.util.List; |
47 | 47 |
|
48 | 48 | /** |
49 | 49 | * Checks if a constructor or method annotated with {@link |
@@ -159,15 +159,19 @@ public Description matchClass(ClassTree tree, VisitorState state) { |
159 | 159 | } |
160 | 160 |
|
161 | 161 | private static boolean invokedConstructorMustBeClosed(VisitorState state, MethodTree methodTree) { |
162 | | - // The first statement in a constructor should be an invocation of the super/this constructor. |
163 | | - List<? extends StatementTree> statements = methodTree.getBody().getStatements(); |
164 | | - if (statements.isEmpty()) { |
165 | | - // Not sure how the body would be empty, but just filter it out in case. |
166 | | - return false; |
| 162 | + for (StatementTree statement : methodTree.getBody().getStatements()) { |
| 163 | + if (!(statement instanceof ExpressionStatementTree est)) { |
| 164 | + continue; |
| 165 | + } |
| 166 | + if (!(est.getExpression() instanceof MethodInvocationTree mit)) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + if (mit.getMethodSelect() instanceof IdentifierTree id |
| 170 | + && (id.getName().contentEquals("super") || id.getName().contentEquals("this"))) { |
| 171 | + MethodSymbol invokedConstructorSymbol = ASTHelpers.getSymbol(mit); |
| 172 | + return hasAnnotation(invokedConstructorSymbol, MUST_BE_CLOSED_ANNOTATION, state); |
| 173 | + } |
167 | 174 | } |
168 | | - ExpressionStatementTree est = (ExpressionStatementTree) statements.getFirst(); |
169 | | - MethodInvocationTree mit = (MethodInvocationTree) est.getExpression(); |
170 | | - MethodSymbol invokedConstructorSymbol = ASTHelpers.getSymbol(mit); |
171 | | - return hasAnnotation(invokedConstructorSymbol, MUST_BE_CLOSED_ANNOTATION, state); |
| 175 | + return false; |
172 | 176 | } |
173 | 177 | } |
0 commit comments