forked from typetools/checker-framework
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for multiple method parameters to be @MustCallAlias in Resource…
… Leak Checker; fixes typetools#4785 (typetools#4808)
- Loading branch information
Showing
2 changed files
with
153 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
checker/tests/resourceleak/MultipleMethodParamsMustCallAliasTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import java.io.*; | ||
import java.net.*; | ||
import org.checkerframework.checker.calledmethods.qual.*; | ||
import org.checkerframework.checker.mustcall.qual.*; | ||
import org.checkerframework.common.returnsreceiver.qual.*; | ||
|
||
class MultipleMethodParamsMustCallAliasTest { | ||
|
||
void testMultiMethodParamsCorrect1(@Owning InputStream in1, @Owning InputStream in2) | ||
throws IOException { | ||
|
||
ReplicaInputStreams r = new ReplicaInputStreams(in1, in2); | ||
|
||
r.close(); | ||
} | ||
|
||
void testMultiMethodParamsCorrect2(@Owning InputStream in1, @Owning InputStream in2) | ||
throws IOException { | ||
|
||
ReplicaInputStreams r = new ReplicaInputStreams(in1, in2); | ||
|
||
try { | ||
in1.close(); | ||
} catch (IOException e) { | ||
} finally { | ||
in2.close(); | ||
} | ||
} | ||
|
||
// It's a FP, see: https://github.com/typetools/checker-framework/issues/4843 | ||
// :: error: required.method.not.called | ||
void testMultiMethodParamsCorrect3(@Owning InputStream in1, @Owning InputStream in2) | ||
throws IOException { | ||
|
||
ReplicaInputStreams r = new ReplicaInputStreams(in1, in2); | ||
|
||
try { | ||
in1.close(); | ||
} finally { | ||
in2.close(); | ||
} | ||
} | ||
|
||
// :: error: required.method.not.called | ||
void testMultiMethodParamsWrong1(@Owning InputStream in1, @Owning InputStream in2) | ||
throws IOException { | ||
|
||
ReplicaInputStreams r = new ReplicaInputStreams(in1, in2); | ||
|
||
in1.close(); | ||
} | ||
|
||
// :: error: required.method.not.called | ||
void testMultiMethodParamsWrong2(@Owning InputStream in1, @Owning InputStream in2) | ||
throws IOException { | ||
|
||
ReplicaInputStreams r = new ReplicaInputStreams(in1, in2); | ||
|
||
in2.close(); | ||
} | ||
|
||
// :: error: required.method.not.called | ||
void testMultiMethodParamsWrong3(@Owning InputStream in1) throws IOException { | ||
// :: error: required.method.not.called | ||
Socket socket = new Socket("address", 12); | ||
ReplicaInputStreams r = new ReplicaInputStreams(in1, socket.getInputStream()); | ||
} | ||
|
||
class ReplicaInputStreams implements Closeable { | ||
|
||
private final @Owning InputStream in1; | ||
private final @Owning InputStream in2; | ||
|
||
public @MustCallAlias ReplicaInputStreams( | ||
@MustCallAlias InputStream i1, @MustCallAlias InputStream i2) { | ||
this.in1 = i1; | ||
this.in2 = i2; | ||
} | ||
|
||
@Override | ||
@EnsuresCalledMethods( | ||
value = {"this.in1", "this.in2"}, | ||
methods = {"close"}) | ||
public void close() throws IOException { | ||
in1.close(); | ||
in2.close(); | ||
} | ||
} | ||
} |