Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from maddalab/master
Added a new detector CommonsEqualsBuilderToEquals && CommonsHashcodeBuilderToHashcode
- Loading branch information
Showing
173 changed files
with
349 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="samples"/> | ||
<classpathentry excluding="**/*.*" kind="src" path="etc"/> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="src" path="samples"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="samples/lib/jsp-api.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/junit.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/log4j.jar" sourcepath="/home/dave/.m2/repository/log4j/log4j/1.2.15/log4j-1.2.15-sources.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/servlet-api.jar"/> | ||
<classpathentry combineaccessrules="false" kind="src" path="/findbugs"/> | ||
<classpathentry kind="lib" path="lib/bcel-2.0.0.jar"/> | ||
<classpathentry kind="lib" path="lib/findbugs-2.0.0.jar"/> | ||
<classpathentry kind="lib" path="lib/annotations-2.0.0.jar"/> | ||
<classpathentry kind="lib" path="lib/asm-tree-3.3.1.jar"/> | ||
<classpathentry kind="lib" path="lib/bcel-2.0.0.jar"/> | ||
<classpathentry kind="lib" path="lib/findbugs-2.0.0.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/commons-lang3-3.1.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/jsp-api-2.2.1.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/junit-4.10.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/log4j-1.2.16.jar"/> | ||
<classpathentry kind="lib" path="samples/lib/servlet-api-3.0.1.jar"/> | ||
<classpathentry kind="output" path="classes"/> | ||
</classpath> |
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 |
---|---|---|
|
@@ -8,3 +8,5 @@ classes | |
*.xlr | ||
*.jar | ||
*.zip | ||
.settings/org.eclipse.core.resources.prefs | ||
.settings/org.eclipse.m2e.core.prefs |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 @@ | ||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
|
||
public class CEBE_EqualsToEqualsSample { | ||
public final String name; | ||
public final int age; | ||
|
||
CEBE_EqualsToEqualsSample(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder().append(name).append(age).toHashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
CEBE_EqualsToEqualsSample other = (CEBE_EqualsToEqualsSample) obj; | ||
return new EqualsBuilder().append(this.name, other.name) | ||
.append(this.age, other.age).equals(obj); | ||
} | ||
|
||
} |
Binary file not shown.
Binary file not shown.
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,30 @@ | ||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
|
||
public class CHBH_HashcodeToHashcodeSample { | ||
public final String name; | ||
public final int age; | ||
|
||
CHBH_HashcodeToHashcodeSample(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder().append(name).append(age).hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
CHBH_HashcodeToHashcodeSample other = (CHBH_HashcodeToHashcodeSample) obj; | ||
return new EqualsBuilder().append(this.name, other.name) | ||
.append(this.age, other.age).isEquals(); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
79 changes: 79 additions & 0 deletions
79
src/com/mebigfatguy/fbcontrib/detect/CommonsEqualsBuilderToEquals.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,79 @@ | ||
package com.mebigfatguy.fbcontrib.detect; | ||
|
||
import org.apache.bcel.classfile.Code; | ||
import org.apache.bcel.classfile.LocalVariableTable; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.BytecodeScanningDetector; | ||
import edu.umd.cs.findbugs.OpcodeStack; | ||
|
||
/** | ||
* Find usage of EqualsBuilder from Apache commons, where the code invoke | ||
* equals() on the constructed object rather than isEquals() | ||
* | ||
* <pre> | ||
* new EqualsBuilder().append(this.name, other.name).equals(other); | ||
* </pre> | ||
*/ | ||
public class CommonsEqualsBuilderToEquals extends BytecodeScanningDetector { | ||
|
||
private final OpcodeStack stack; | ||
private final BugReporter bugReporter; | ||
|
||
/** | ||
* constructs a CEBE detector given the reporter to report bugs on. | ||
* | ||
* @param bugReporter | ||
* the sync of bug reports | ||
*/ | ||
public CommonsEqualsBuilderToEquals(final BugReporter bugReporter) { | ||
stack = new OpcodeStack(); | ||
this.bugReporter = bugReporter; | ||
} | ||
|
||
/** | ||
* implements the visitor to pass through constructors and static | ||
* initializers to the byte code scanning code. These methods are not | ||
* reported, but are used to build SourceLineAnnotations for fields, if | ||
* accessed. | ||
* | ||
* @param obj | ||
* the context object of the currently parsed code attribute | ||
*/ | ||
@Override | ||
public void visitCode(Code obj) { | ||
stack.resetForMethodEntry(this); | ||
LocalVariableTable lvt = getMethod().getLocalVariableTable(); | ||
if (lvt != null) { | ||
super.visitCode(obj); | ||
} | ||
} | ||
|
||
@Override | ||
public void sawOpcode(int seen) { | ||
try { | ||
switch (seen) { | ||
case INVOKEVIRTUAL: | ||
String methodName = getNameConstantOperand(); | ||
if ("equals".equals(methodName) | ||
&& "(Ljava/lang/Object;)Z" | ||
.equals(getSigConstantOperand())) { | ||
String calledClass = stack.getStackItem(1).getSignature(); | ||
if ("Lorg/apache/commons/lang3/builder/EqualsBuilder;" | ||
.equals(calledClass) | ||
|| "org/apache/commons/lang/builder/EqualsBuilder" | ||
.equals(calledClass)) { | ||
bugReporter.reportBug(new BugInstance(this, | ||
"CEBE_COMMONS_EQUALS_BUILDER_ISEQUALS", | ||
HIGH_PRIORITY).addClass(this).addMethod(this) | ||
.addSourceLine(this)); | ||
} | ||
} | ||
} | ||
} finally { | ||
super.sawOpcode(seen); | ||
stack.sawOpcode(this, seen); | ||
} | ||
} | ||
} |
Oops, something went wrong.