Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine FilterOutputStreamSlowMultibyteWrite findings #2031

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -62,6 +62,8 @@ public final class FilterOutputStreamSlowMultibyteWrite extends BugChecker imple
Matchers.methodHasParameters(Matchers.isSameType(Suppliers.INT_TYPE)));

private static final Supplier<Name> WRITE = VisitorState.memoize(state -> state.getName("write"));
private static final Supplier<Type> FILTER_OUTPUT_STREAM =
VisitorState.memoize(state -> state.getTypeFromString(FilterOutputStream.class.getTypeName()));

@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
Expand All @@ -81,6 +83,11 @@ public Description matchClass(ClassTree classTree, VisitorState state) {
return Description.NO_MATCH;
}

Type filterOutputStreamType = FILTER_OUTPUT_STREAM.get(state);
if (filterOutputStreamType == null) {
return Description.NO_MATCH;
}

Type byteArrayType = state.arrayTypeForType(state.getSymtab().byteType);
MethodSymbol multiByteWriteMethod = ASTHelpers.resolveExistingMethod(
state,
Expand All @@ -89,7 +96,11 @@ public Description matchClass(ClassTree classTree, VisitorState state) {
ImmutableList.of(byteArrayType, intType, intType),
ImmutableList.of());

if (multiByteWriteMethod != null && multiByteWriteMethod.owner.equals(thisClassSymbol)) {
if ((multiByteWriteMethod != null)
&& (multiByteWriteMethod.owner.equals(thisClassSymbol)
|| (singleByteWriteMethod.owner.equals(multiByteWriteMethod.owner)
&& !singleByteWriteMethod.owner.equals(filterOutputStreamType.tsym)))) {
// non-FilterOutputStream class defines both single & multibyte write
return Description.NO_MATCH;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,38 @@ public void inheritedSingleByteWrite() {
"}")
.doTest();
}

@Test
public void inheritedSingleAndMultiByteWrite() {
compilationHelper
.addSourceLines(
"Super.java",
"import java.io.*;",
"abstract class Super extends FilterOutputStream {",
" Super() { super(new ByteArrayOutputStream()); }",
" public void write(int b) {}",
" public void write(byte[] b, int a, int c) {}",
"}")
.addSourceLines("TestClass.java", "class TestClass extends Super {}")
.doTest();
}

@Test
public void singleAndMultiByteWrite() {
compilationHelper
.addSourceLines(
"Super.java",
"import java.io.*;",
" // BUG: Diagnostic contains:",
"abstract class Super extends FilterOutputStream {",
" Super() { super(new ByteArrayOutputStream()); }",
"}")
.addSourceLines(
"TestClass.java",
"class TestClass extends Super {",
" public void write(int b) {}",
" public void write(byte[] b, int a, int c) {}",
"}")
.doTest();
}
}