Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create new detector for rule MET04-J
MET04-J
- Loading branch information
1 parent
2775ed5
commit 8cc9cf1
Showing
15 changed files
with
494 additions
and
2 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
97 changes: 97 additions & 0 deletions
97
...sts/src/test/java/edu/umd/cs/findbugs/detect/FindIncreasedAccessibilityOfMethodsTest.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,97 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
import org.junit.Test; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasItem; | ||
|
||
public class FindIncreasedAccessibilityOfMethodsTest extends AbstractIntegrationTest { | ||
|
||
@Test | ||
public void findIAOMBugInClass_IncreasedAccessibilityOfMethods_SubClassFromSamePackage() { | ||
performAnalysis( | ||
"increasedAccessibilityOfMethods/SuperClassOfSuperClass.class", | ||
"increasedAccessibilityOfMethods/SuperClass.class", | ||
"increasedAccessibilityOfMethods/SubClassFromSamePackage.class"); | ||
|
||
assertNumberOfIAOMBugs(6); | ||
|
||
assertIAOMBug("SubClassFromSamePackage", "protectedMethodToPublic"); | ||
assertIAOMBug("SubClassFromSamePackage", "packagePrivateMethodToPublic"); | ||
assertIAOMBug("SubClassFromSamePackage", "packagePrivateMethodToProtected"); | ||
assertIAOMBug("SubClassFromSamePackage", "superProtectedMethodToPublic"); | ||
assertIAOMBug("SubClassFromSamePackage", "superPackagePrivateMethodToPublic"); | ||
assertIAOMBug("SubClassFromSamePackage", "superPackagePrivateMethodToProtected"); | ||
} | ||
|
||
@Test | ||
public void findIAOMBugInClass_IncreasedAccessibilityOfMethods_SubClassFromAnotherPackage() { | ||
performAnalysis( | ||
"increasedAccessibilityOfMethods/SuperClassOfSuperClass.class", | ||
"increasedAccessibilityOfMethods/SuperClass.class", | ||
"increasedAccessibilityOfMethods/anotherPackage/SubClassFromAnotherPackage.class"); | ||
|
||
assertNumberOfIAOMBugs(1); | ||
|
||
assertIAOMBug("SubClassFromAnotherPackage", "protectedMethodToPublic"); | ||
} | ||
|
||
@Test | ||
public void findNoIAOMBugInClass_IncreasedAccessibilityOfMethods_SubClassFromAnotherPackage() { | ||
performAnalysis( | ||
"increasedAccessibilityOfMethods/SuperClassOfSuperClass.class", | ||
"increasedAccessibilityOfMethods/SuperClass.class", | ||
"increasedAccessibilityOfMethods/anotherPackage/CorrectSubClass.class"); | ||
|
||
assertNumberOfIAOMBugs(0); | ||
} | ||
|
||
@Test | ||
public void findNoIAOMBugInClass_IncreasedAccessibilityOfMethods_CloneableImplementation() { | ||
performAnalysis( | ||
"increasedAccessibilityOfMethods/CloneableImplementation.class"); | ||
|
||
assertNumberOfIAOMBugs(0); | ||
} | ||
|
||
@Test | ||
public void findNoIAOMBugInClass_IncreasedAccessibilityOfMethods_InterfaceImplementation() { | ||
performAnalysis( | ||
"increasedAccessibilityOfMethods/InterfaceImplementation.class"); | ||
|
||
assertNumberOfIAOMBugs(0); | ||
} | ||
|
||
@Test | ||
public void findNoIAOMBugInClass_IncreasedAccessibilityOfMethods_GenericImplementation() { | ||
performAnalysis( | ||
"increasedAccessibilityOfMethods/GenericSuperClass.class", | ||
"increasedAccessibilityOfMethods/GenericSubClass.class"); | ||
|
||
assertNumberOfIAOMBugs(3); | ||
|
||
assertIAOMBug("GenericSubClass", "protectedMethodToPublicWithGenericParameter"); | ||
assertIAOMBug("GenericSubClass", "packagePrivateMethodToPublicWithGenericParameter"); | ||
assertIAOMBug("GenericSubClass", "packagePrivateMethodToProtectedWithGenericParameter"); | ||
} | ||
|
||
private void assertNumberOfIAOMBugs(int numberOfBugs) { | ||
BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType("IAOM_DO_NOT_INCREASE_METHOD_ACCESSIBILITY").build(); | ||
assertThat(getBugCollection(), containsExactly(numberOfBugs, bugTypeMatcher)); | ||
} | ||
|
||
private void assertIAOMBug(String className, String methodName) { | ||
BugInstanceMatcher bugInstanceMatcher = new BugInstanceMatcherBuilder() | ||
.bugType("IAOM_DO_NOT_INCREASE_METHOD_ACCESSIBILITY") | ||
.inClass(className) | ||
.inMethod(methodName) | ||
.build(); | ||
assertThat(getBugCollection(), hasItem(bugInstanceMatcher)); | ||
} | ||
|
||
} |
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
123 changes: 123 additions & 0 deletions
123
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindIncreasedAccessibilityOfMethods.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,123 @@ | ||
/* | ||
* SpotBugs - Find bugs in Java programs | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
package edu.umd.cs.findbugs.detect; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.Detector; | ||
import edu.umd.cs.findbugs.ba.AnalysisContext; | ||
import edu.umd.cs.findbugs.ba.ClassContext; | ||
import org.apache.bcel.Repository; | ||
import org.apache.bcel.classfile.JavaClass; | ||
import org.apache.bcel.classfile.Method; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class FindIncreasedAccessibilityOfMethods implements Detector { | ||
|
||
private final BugReporter bugReporter; | ||
|
||
public FindIncreasedAccessibilityOfMethods(BugReporter bugReporter) { | ||
this.bugReporter = bugReporter; | ||
} | ||
|
||
@Override | ||
public void visitClassContext(ClassContext classContext) { | ||
JavaClass[] superClasses; | ||
JavaClass directSuperClass; | ||
JavaClass subClass = classContext.getJavaClass(); | ||
try { | ||
directSuperClass = subClass.getSuperClass(); | ||
superClasses = subClass.getSuperClasses(); | ||
} catch (ClassNotFoundException e) { | ||
AnalysisContext.reportMissingClass(e); | ||
return; | ||
} | ||
|
||
Method[] subClassMethods = subClass.getMethods(); | ||
|
||
for (JavaClass javaClass : superClasses) { | ||
for (Method superClassMethod : getMethodsExceptConstructors(javaClass)) { | ||
if (isAccessibleFromSubClass(superClassMethod, subClass, directSuperClass)) { | ||
for (Method subClassMethod : subClassMethods) { | ||
if (isOverridable(superClassMethod, subClassMethod) && !isCloneMethodFromCloneable(subClass, superClassMethod)) { | ||
if (superClassMethod.isProtected() && subClassMethod.isPublic()) { | ||
bugReporter.reportBug(new BugInstance(this, "IAOM_DO_NOT_INCREASE_METHOD_ACCESSIBILITY", NORMAL_PRIORITY) | ||
.addClassAndMethod(subClass, subClassMethod) | ||
.addString("protected") | ||
.addString("public")); | ||
} | ||
if (isPackagePrivate(superClassMethod)) { | ||
if (subClassMethod.isPublic()) { | ||
bugReporter.reportBug(new BugInstance(this, "IAOM_DO_NOT_INCREASE_METHOD_ACCESSIBILITY", NORMAL_PRIORITY) | ||
.addClassAndMethod(subClass, subClassMethod) | ||
.addString("package private") | ||
.addString("public")); | ||
} else if (subClassMethod.isProtected()) { | ||
bugReporter.reportBug(new BugInstance(this, "IAOM_DO_NOT_INCREASE_METHOD_ACCESSIBILITY", NORMAL_PRIORITY) | ||
.addClassAndMethod(subClass, subClassMethod) | ||
.addString("package private") | ||
.addString("protected")); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void report() { | ||
} | ||
|
||
private boolean isPackagePrivate(Method method) { | ||
return !(method.isPublic() || method.isProtected() || method.isPrivate()); | ||
} | ||
|
||
private boolean isAccessibleFromSubClass(Method method, JavaClass subClass, JavaClass directSuperClass) { | ||
return method.isProtected() || (subClass.getPackageName().equals(directSuperClass.getPackageName()) && isPackagePrivate(method)); | ||
} | ||
|
||
private boolean isOverridable(Method original, Method overrider) { | ||
return Objects.equals(original, overrider) && !original.isFinal(); | ||
} | ||
|
||
private List<Method> getMethodsExceptConstructors(JavaClass javaClass) { | ||
ArrayList<Method> methods = new ArrayList<>(); | ||
for (Method method : javaClass.getMethods()) { | ||
if (!method.getName().equals("<init>")) { | ||
methods.add(method); | ||
} | ||
} | ||
return methods; | ||
} | ||
|
||
private boolean isCloneMethodFromCloneable(JavaClass javaClass, Method method) { | ||
try { | ||
return javaClass.implementationOf(Repository.lookupClass(Cloneable.class)) && method.getName().equals("clone"); | ||
} catch (ClassNotFoundException e) { | ||
AnalysisContext.reportMissingClass(e); | ||
return false; | ||
} | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
spotbugsTestCases/src/java/increasedAccessibilityOfMethods/CloneableImplementation.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,14 @@ | ||
package increasedAccessibilityOfMethods; | ||
|
||
public class CloneableImplementation implements Cloneable { | ||
|
||
@Override | ||
public CloneableImplementation clone() { | ||
try { | ||
return (CloneableImplementation) super.clone(); | ||
} catch (CloneNotSupportedException e) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
spotbugsTestCases/src/java/increasedAccessibilityOfMethods/GenericSubClass.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,35 @@ | ||
package increasedAccessibilityOfMethods; | ||
|
||
public class GenericSubClass<K> extends GenericSuperClass<K> { | ||
|
||
@Override | ||
public String publicMethodWithGenericParameter(K parameter) { | ||
return "GenericSubClass.publicMethodWithGenericParameter"; | ||
} | ||
|
||
@Override | ||
public String protectedMethodToPublicWithGenericParameter(K parameter) { | ||
return "GenericSubClass.protectedMethodToPublicWithGenericParameter"; | ||
} | ||
|
||
@Override | ||
protected String protectedMethodWithGenericParameter(K parameter) { | ||
return "GenericSubClass.protectedMethodWithGenericParameter"; | ||
} | ||
|
||
@Override | ||
public String packagePrivateMethodToPublicWithGenericParameter(K parameter) { | ||
return "GenericSubClass.packagePrivateMethodToPublicWithGenericParameter"; | ||
} | ||
|
||
@Override | ||
protected String packagePrivateMethodToProtectedWithGenericParameter(K parameter) { | ||
return "GenericSubClass.packagePrivateMethodToProtectedWithGenericParameter"; | ||
} | ||
|
||
@Override | ||
String packagePrivateWithGenericParameter(K parameter) { | ||
return "GenericSubClass.packagePrivateWithGenericParameter"; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
spotbugsTestCases/src/java/increasedAccessibilityOfMethods/GenericSuperClass.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,36 @@ | ||
package increasedAccessibilityOfMethods; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
public class GenericSuperClass<T> { | ||
|
||
public String publicMethodWithGenericParameter(T parameter) { | ||
return "GenericSuperClass.publicMethodWithGenericParameter"; | ||
} | ||
|
||
protected String protectedMethodToPublicWithGenericParameter(T parameter) { | ||
return "GenericSuperClass.protectedMethodToPublicWithGenericParameter"; | ||
} | ||
|
||
protected String protectedMethodWithGenericParameter(T parameter) { | ||
return "GenericSuperClass.protectedMethodWithGenericParameter"; | ||
} | ||
|
||
String packagePrivateMethodToPublicWithGenericParameter(T parameter) { | ||
return "GenericSuperClass.packagePrivateMethodToPublicWithGenericParameter"; | ||
} | ||
|
||
String packagePrivateMethodToProtectedWithGenericParameter(T parameter) { | ||
return "GenericSuperClass.packagePrivateMethodToProtectedWithGenericParameter"; | ||
} | ||
|
||
String packagePrivateWithGenericParameter(T parameter) { | ||
return "GenericSuperClass.packagePrivateWithGenericParameter"; | ||
} | ||
|
||
@SuppressFBWarnings("UPM") | ||
private String privateMethodWithGenericParameter(T parameter) { | ||
return "GenericSuperClass.privateMethodWithGenericParameter"; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
spotbugsTestCases/src/java/increasedAccessibilityOfMethods/Interface.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,7 @@ | ||
package increasedAccessibilityOfMethods; | ||
|
||
public interface Interface { | ||
|
||
String packagePrivateInterfaceMethod(); | ||
|
||
} |
Oops, something went wrong.