-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spotbugs rule for direct instantiation of BinaryDecoder (#161)
- Loading branch information
1 parent
113b0a3
commit a1fada4
Showing
18 changed files
with
145 additions
and
25 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
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
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
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
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
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
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
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
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
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
31 changes: 31 additions & 0 deletions
31
...gin/src/main/java/com/linkedin/avroutil1/spotbugs/BinaryDecoderInstantiationDetector.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,31 @@ | ||
package com.linkedin.avroutil1.spotbugs; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector; | ||
import org.apache.bcel.Const; | ||
|
||
/** | ||
* detects direct instantiations of BinaryDecoder | ||
*/ | ||
public class BinaryDecoderInstantiationDetector extends OpcodeStackDetector { | ||
private final BugReporter bugReporter; | ||
|
||
public BinaryDecoderInstantiationDetector(BugReporter bugReporter) { | ||
this.bugReporter = bugReporter; | ||
} | ||
|
||
@Override | ||
public void sawOpcode(int seen) { | ||
if (seen != Const.NEW) { | ||
return; | ||
} | ||
if (getClassConstantOperand().equals("org/apache/avro/io/BinaryDecoder")) { | ||
// new BinaryEncoder call | ||
BugInstance bug = new BugInstance(this, "BINARY_DECODER_INSTANTIATION", NORMAL_PRIORITY) | ||
.addClassAndMethod(this) | ||
.addSourceLine(this, getPC()); | ||
bugReporter.reportBug(bug); | ||
} | ||
} | ||
} |
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
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
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
5 changes: 5 additions & 0 deletions
5
spotbugs-plugin/src/test/java/com/linkedin/avroutil1/spotbugs/BadClass.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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package com.linkedin.avroutil1.spotbugs; | ||
|
||
import org.apache.avro.io.BinaryDecoder; | ||
import org.apache.avro.io.BinaryEncoder; | ||
|
||
public class BadClass { | ||
|
||
public void instantiateBinaryEncoder() { | ||
BinaryEncoder bobTheEncoder = new BinaryEncoder(null); | ||
} | ||
|
||
public void instantiateBinaryDecoder() { | ||
BinaryDecoder bobTheDecoder = new BinaryDecoder(null); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...src/test/java/com/linkedin/avroutil1/spotbugs/BinaryDecoderInstantiationDetectorTest.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,41 @@ | ||
package com.linkedin.avroutil1.spotbugs; | ||
|
||
import edu.umd.cs.findbugs.BugCollection; | ||
import edu.umd.cs.findbugs.test.SpotBugsRule; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
|
||
public class BinaryDecoderInstantiationDetectorTest { | ||
@Rule | ||
public SpotBugsRule spotbugs = new SpotBugsRule(); | ||
|
||
@Test | ||
public void testBadCase() { | ||
Path path = Paths.get("build/classes/java/test", BadClass.class.getName().replace('.', '/') + ".class"); | ||
BugCollection bugCollection = spotbugs.performAnalysis(path); | ||
|
||
BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType("BINARY_DECODER_INSTANTIATION") | ||
.inMethod("instantiateBinaryDecoder").build(); | ||
MatcherAssert.assertThat(bugCollection, containsExactly(1, bugTypeMatcher)); | ||
} | ||
|
||
@Test | ||
public void testGoodCase() { | ||
Path path = Paths.get("build/classes/java/test", GoodClass.class.getName().replace('.', '/') + ".class"); | ||
BugCollection bugCollection = spotbugs.performAnalysis(path); | ||
|
||
BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType("BINARY_DECODER_INSTANTIATION") | ||
.build(); | ||
MatcherAssert.assertThat(bugCollection, containsExactly(0, bugTypeMatcher)); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
spotbugs-plugin/src/test/java/com/linkedin/avroutil1/spotbugs/GoodClass.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 |
---|---|---|
@@ -1,13 +1,21 @@ | ||
package com.linkedin.avroutil1.spotbugs; | ||
|
||
import com.linkedin.avroutil1.compatibility.AvroCompatibilityHelper; | ||
import org.apache.avro.io.BinaryDecoder; | ||
import org.apache.avro.io.BinaryEncoder; | ||
import org.apache.avro.io.DecoderFactory; | ||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public class GoodClass { | ||
|
||
public void instantiateBinaryEncoder() { | ||
BinaryEncoder bobTheEncoder = AvroCompatibilityHelper.newBinaryEncoder( (OutputStream) null); | ||
} | ||
|
||
public void instantiateBinaryDecoder() { | ||
BinaryDecoder bobTheDecoder = AvroCompatibilityHelper.newBinaryDecoder( (InputStream) null); | ||
BinaryDecoder robertTheDecoder = DecoderFactory.defaultFactory().createBinaryDecoder(new byte[] {1, 2, 3}, null); | ||
} | ||
} |