Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import static com.google.errorprone.util.AnnotationNames.MUST_BE_CLOSED_ANNOTATION;

import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
Expand All @@ -52,13 +51,8 @@
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import javax.inject.Inject;

/**
* Checks if a constructor or method annotated with {@link
* com.google.errorprone.annotations.MustBeClosed} is called within the resource variable
* initializer of a try-with-resources statement.
*/
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@BugPattern(
altNames = "MustBeClosed",
summary =
Expand All @@ -80,24 +74,8 @@ public class MustBeClosedChecker extends AbstractMustBeClosedChecker
private static final Matcher<ExpressionTree> HARDCODED_MUST_BE_CLOSED_METHODS =
staticMethod().onClass("java.lang.foreign.Arena").namedAnyOf("ofConfined", "ofShared");

private final Matcher<ExpressionTree> mustBeClosedMatcher;

@Inject
MustBeClosedChecker(ErrorProneFlags flags) {
var checkArena =
flags
.getBoolean("MustBeClosed:CheckArena")
.or(() -> flags.getBoolean("MustBeClosedChecker:CheckArena"))
.orElse(true);
this.mustBeClosedMatcher =
checkArena
? anyOf(HAS_MUST_BE_CLOSED_ANNOTATION, HARDCODED_MUST_BE_CLOSED_METHODS)
: HAS_MUST_BE_CLOSED_ANNOTATION::matches;
}

public MustBeClosedChecker() {
this(ErrorProneFlags.empty());
}
private static final Matcher<ExpressionTree> MUST_BE_CLOSED_ANNOTATION_OR_METHOD =
anyOf(HAS_MUST_BE_CLOSED_ANNOTATION, HARDCODED_MUST_BE_CLOSED_METHODS);

/**
* Check that the {@code MustBeClosed} annotation is only used for constructors of AutoCloseables
Expand All @@ -109,7 +87,7 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
state.reportMatch(
scanEntireMethodFor(
(t, s) -> {
if (!mustBeClosedMatcher.matches(t, s)) {
if (!MUST_BE_CLOSED_ANNOTATION_OR_METHOD.matches(t, s)) {
return false;
}
if (t instanceof MethodInvocationTree && getSymbol(t).isConstructor()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1227,50 +1227,4 @@ Arena unannotatedReturn() {
""")
.doTest();
}

@Test
public void arenaFlagDisabled() {
assume().that(Runtime.version().feature()).isAtLeast(22);
compilationHelper
.setArgs("-XepOpt:MustBeClosed:CheckArena=false")
.addSourceLines(
"Test.java",
"""
import java.lang.foreign.Arena;

class Test {
void unclosedOfConfined() {
Arena arena = Arena.ofConfined();
}

void unclosedOfShared() {
Arena arena = Arena.ofShared();
}
}
""")
.doTest();
}

@Test
public void arenaFlagDisabled_altCheckerName() {
assume().that(Runtime.version().feature()).isAtLeast(22);
compilationHelper
.setArgs("-XepOpt:MustBeClosedChecker:CheckArena=false")
.addSourceLines(
"Test.java",
"""
import java.lang.foreign.Arena;

class Test {
void unclosedOfConfined() {
Arena arena = Arena.ofConfined();
}

void unclosedOfShared() {
Arena arena = Arena.ofShared();
}
}
""")
.doTest();
}
}
10 changes: 7 additions & 3 deletions docs/bugpattern/MustBeClosedChecker.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Methods or constructors annotated with `@MustBeClosed` require that the returned
resource is closed. This is enforced by checking that invocations occur within
the resource variable initializer of a try-with-resources statement:
Some methods and constructors require that the returned resource is closed. This
includes user methods/constructors annotated with `@MustBeClosed` and some
additional well-known APIs. (Some further well-known APIs are covered by a
different check, StreamResourceLeak.)

This is enforced by checking that invocations occur within the resource variable
initializer of a try-with-resources statement:

```java
try (AutoCloseable resource = createTheResource()) {
Expand Down
Loading