Skip to content

Commit

Permalink
update JooqResultStreamLeak
Browse files Browse the repository at this point in the history
  • Loading branch information
carterkozak committed Oct 13, 2022
1 parent fee86b5 commit 770c400
Showing 1 changed file with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
import com.google.errorprone.bugpatterns.StreamResourceLeak;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.matchers.method.MethodMatchers;
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.tools.javac.code.Type;

@AutoService(BugChecker.class)
Expand All @@ -39,32 +40,28 @@
+ " try-with-resources. Not doing so can result in leaked database resources (such as connections"
+ " or cursors) in code paths that throw an exception or fail to call #close().")
public final class JooqResultStreamLeak extends StreamResourceLeak {
private static final Matcher<ExpressionTree> MATCHER = MethodMatchers.instanceMethod()
.onDescendantOf("org.jooq.ResultQuery")
.withAnyName();
private static final Matcher<ExpressionTree> MATCHER = Matchers.allOf(
MethodMatchers.instanceMethod()
.onDescendantOf("org.jooq.ResultQuery")
.withAnyName(),
JooqResultStreamLeak::shouldBeAutoClosed);

private static final Supplier<Type> JOOQ_QUERY_PART =
VisitorState.memoize(state -> state.getTypeFromString("org.jooq.QueryPart"));

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}

if (!shouldBeAutoClosed(tree, state)) {
return Description.NO_MATCH;
}
private static final Supplier<Type> AUTO_CLOSEABLE =
VisitorState.memoize(state -> state.getTypeFromString(AutoCloseable.class.getName()));

return matchNewClassOrMethodInvocation(tree, state, findingPerSite());
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
return scanEntireMethodFor(MATCHER, tree, state);
}

private static boolean shouldBeAutoClosed(MethodInvocationTree tree, VisitorState state) {
private static boolean shouldBeAutoClosed(ExpressionTree tree, VisitorState state) {
Type returnType = ASTHelpers.getReturnType(tree);

// Most auto-closeable returns should be auto-closed.
boolean isAutoCloseable =
ASTHelpers.isSubtype(returnType, state.getTypeFromString(AutoCloseable.class.getName()), state);
boolean isAutoCloseable = ASTHelpers.isSubtype(returnType, AUTO_CLOSEABLE.get(state), state);

// QueryParts can hold resources but usually don't, so auto-tripping and trying to fix on things
// like SelectConditionStep is unnecessary.
Expand Down

0 comments on commit 770c400

Please sign in to comment.