-
Notifications
You must be signed in to change notification settings - Fork 578
/
FindIncreasedAccessibilityOfMethods.java
123 lines (107 loc) · 5.19 KB
/
FindIncreasedAccessibilityOfMethods.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;
}
}
}