use of com.google.errorprone.annotations.MustBeClosed in project error-prone by google.
the class MustBeClosedChecker method matchNewClassOrMethodInvocation.
/**
* Check that constructors and methods annotated with {@link MustBeClosed} occur within the
* resource variable initializer of a try-with-resources statement.
*
* @param name The name of the method or constructor.
*/
private Description matchNewClassOrMethodInvocation(String name, Tree tree, VisitorState state) {
if (!HAS_MUST_BE_CLOSED_ANNOTATION.matches(tree, state)) {
// {@link MustBeClosed}.
return NO_MATCH;
}
String errorMessage = String.format("%s must be called within the resource variable initializer of a try-with-resources" + " statement.", name);
MethodTree callerMethodTree = enclosingMethod(state);
if (state.getPath().getParentPath().getLeaf().getKind() == Tree.Kind.RETURN && callerMethodTree != null) {
if (HAS_MUST_BE_CLOSED_ANNOTATION.matches(callerMethodTree, state)) {
// statement of an annotated caller method, since invocations of the caller are enforced.
return NO_MATCH;
}
// enforced. Suggest fixing this by annotating the caller method.
return buildDescription(tree).addFix(SuggestedFix.builder().prefixWith(callerMethodTree, "@MustBeClosed\n").addImport(MustBeClosed.class.getCanonicalName()).build()).setMessage(errorMessage).build();
}
if (!inTWR(state)) {
// initializer of a try-with-resources statement.
return buildDescription(tree).setMessage(errorMessage).build();
}
return NO_MATCH;
}
Aggregations