Skip to content

Commit ba06365

Browse files
Some refactorings on detectors
1 parent e2f8d3d commit ba06365

17 files changed

Lines changed: 390 additions & 164 deletions

java-benchmarks

Submodule java-benchmarks updated 164 files

src/main/java/pascal/taie/analysis/bugfinder/BugInstance.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
/*
2+
* Tai-e: A Static Analysis Framework for Java
3+
*
4+
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
5+
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
6+
*
7+
* This file is part of Tai-e.
8+
*
9+
* Tai-e is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License
11+
* as published by the Free Software Foundation, either version 3
12+
* of the License, or (at your option) any later version.
13+
*
14+
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17+
* Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
123
package pascal.taie.analysis.bugfinder;
224

3-
import pascal.taie.ir.IR;
425
import pascal.taie.language.classes.JClass;
5-
import pascal.taie.language.classes.JField;
626
import pascal.taie.language.classes.JMethod;
727

828
import javax.annotation.Nonnull;
9-
1029
import java.util.Objects;
1130

12-
import static java.util.Objects.requireNonNull;
13-
1431
//TODO: refactor it with more precise context information.
1532
public class BugInstance {
1633

@@ -40,29 +57,29 @@ public String getType() {
4057
}
4158

4259
private String getString(Object o) {
43-
return o == null ? "empty" :o.toString();
60+
return o == null ? "empty" : o.toString();
4461
}
4562

4663
@Override
4764
public String toString() {
4865
String sourcelineRange = "empty";
49-
if(sourceLineStart >= 0){
66+
if (sourceLineStart >= 0) {
5067
sourcelineRange = sourceLineStart == sourceLineEnd ? String.valueOf(sourceLineStart) :
51-
sourceLineStart + "---" + sourceLineEnd;
68+
sourceLineStart + "---" + sourceLineEnd;
5269
}
5370
return String.format("Class: %s, Method: %s, LineNumber: %s. \nbug type: %s, severity: %s",
54-
getString(jClass), getString(jMethod), sourcelineRange, type, severity
55-
);
71+
getString(jClass), getString(jMethod), sourcelineRange, type, severity
72+
);
5673
}
5774

5875
@Override
5976
public boolean equals(Object o) {
60-
if(o == this) return true;
61-
if(!(o instanceof BugInstance bugInstance)) return false;
77+
if (o == this) return true;
78+
if (!(o instanceof BugInstance bugInstance)) return false;
6279

6380
return type.equals(bugInstance.type) && jClass == bugInstance.jClass
64-
&& jMethod == bugInstance.jMethod && sourceLineStart == bugInstance.sourceLineStart
65-
&& sourceLineEnd == bugInstance.sourceLineEnd;
81+
&& jMethod == bugInstance.jMethod && sourceLineStart == bugInstance.sourceLineStart
82+
&& sourceLineEnd == bugInstance.sourceLineEnd;
6683
}
6784

6885
@Override
@@ -75,7 +92,7 @@ public int hashCode() {
7592
return result;
7693
}
7794

78-
public static BugInstance newBugInstance(String type, Severity severity, JMethod method, int lineNum){
95+
public static BugInstance newBugInstance(String type, Severity severity, JMethod method, int lineNum) {
7996
return new BugInstance(type, severity).setClassAndMethod(method).setSourceLine(lineNum);
8097
}
8198

src/main/java/pascal/taie/analysis/bugfinder/detector/CloneIdiom.java renamed to src/main/java/pascal/taie/analysis/bugfinder/CloneIdiom.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
package pascal.taie.analysis.bugfinder.detector;
1+
/*
2+
* Tai-e: A Static Analysis Framework for Java
3+
*
4+
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
5+
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
6+
*
7+
* This file is part of Tai-e.
8+
*
9+
* Tai-e is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License
11+
* as published by the Free Software Foundation, either version 3
12+
* of the License, or (at your option) any later version.
13+
*
14+
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17+
* Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package pascal.taie.analysis.bugfinder;
224

325
import pascal.taie.World;
426
import pascal.taie.analysis.ClassAnalysis;
5-
import pascal.taie.analysis.bugfinder.BugInstance;
6-
import pascal.taie.analysis.bugfinder.Severity;
727
import pascal.taie.config.AnalysisConfig;
8-
import pascal.taie.ir.IR;
928
import pascal.taie.ir.exp.InvokeExp;
1029
import pascal.taie.ir.exp.InvokeSpecial;
1130
import pascal.taie.ir.stmt.Invoke;
1231
import pascal.taie.ir.stmt.Stmt;
13-
import pascal.taie.language.annotation.Annotation;
1432
import pascal.taie.language.classes.ClassHierarchy;
1533
import pascal.taie.language.classes.ClassNames;
1634
import pascal.taie.language.classes.JClass;
@@ -21,7 +39,7 @@
2139

2240
public class CloneIdiom extends ClassAnalysis<Set<BugInstance>> {
2341

24-
public static String ID = "cloneidiom";
42+
public static String ID = "clone-idiom";
2543

2644
public CloneIdiom(AnalysisConfig config) {
2745
super(config);
@@ -78,11 +96,11 @@ public Set<BugInstance> analyze(JClass jclass) {
7896
}
7997

8098
//generate analysis result
81-
if(implementsCloneableDirectly && !hasCloneMethod){
99+
if (implementsCloneableDirectly && !hasCloneMethod) {
82100
bugInstances.add(new BugInstance("CN_IDIOM", Severity.MINOR).setClass(jclass));
83101
}
84102

85-
if(hasCloneMethod && isCloneable && !invokesSuperClone && !isFinal && jclass.isPublic()) {
103+
if (hasCloneMethod && isCloneable && !invokesSuperClone && !isFinal && jclass.isPublic()) {
86104
bugInstances.add(new BugInstance("CN_IDIOM_NO_SUPER_CALL", Severity.MINOR).setClass(jclass));
87105
} else if (hasCloneMethod && !isCloneable && !cloneIsDeprecated && !jclass.isAbstract()) {
88106
bugInstances.add(new BugInstance("CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE", Severity.MINOR).setClass(jclass));

src/main/java/pascal/taie/analysis/bugfinder/detector/DroppedException.java renamed to src/main/java/pascal/taie/analysis/bugfinder/DroppedException.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
package pascal.taie.analysis.bugfinder.detector;
1+
/*
2+
* Tai-e: A Static Analysis Framework for Java
3+
*
4+
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
5+
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
6+
*
7+
* This file is part of Tai-e.
8+
*
9+
* Tai-e is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License
11+
* as published by the Free Software Foundation, either version 3
12+
* of the License, or (at your option) any later version.
13+
*
14+
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17+
* Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package pascal.taie.analysis.bugfinder;
224

325
import pascal.taie.analysis.MethodAnalysis;
4-
import pascal.taie.analysis.bugfinder.Severity;
5-
import pascal.taie.analysis.bugfinder.BugInstance;
626
import pascal.taie.config.AnalysisConfig;
727
import pascal.taie.ir.IR;
828
import pascal.taie.ir.proginfo.ExceptionEntry;
@@ -15,7 +35,7 @@
1535

1636
import java.util.Set;
1737

18-
public class DroppedException extends MethodAnalysis {
38+
public class DroppedException extends MethodAnalysis<Set<BugInstance>> {
1939

2040
public static final String ID = "dropped-exception";
2141

@@ -24,7 +44,7 @@ public DroppedException(AnalysisConfig config) {
2444
}
2545

2646
@Override
27-
public Object analyze(IR ir) {
47+
public Set<BugInstance> analyze(IR ir) {
2848
Set<BugInstance> bugInstanceSet = Sets.newHybridSet();
2949
for (ExceptionEntry entry : ir.getExceptionEntries()) {
3050
String exceptionName = entry.catchType().getName();
@@ -35,22 +55,22 @@ public Object analyze(IR ir) {
3555
Stmt catchHandler = entry.handler();
3656
int nextStmt = catchHandler.getIndex() + 1;
3757

38-
while(nextStmt < ir.getStmts().size() && ir.getStmt(nextStmt) instanceof Nop){
58+
while (nextStmt < ir.getStmts().size() && ir.getStmt(nextStmt) instanceof Nop) {
3959
nextStmt++;
4060
}
4161

42-
if(nextStmt < ir.getStmts().size() &&
43-
(ir.getStmt(nextStmt) instanceof Goto || ir.getStmt(nextStmt) instanceof Return)){
62+
if (nextStmt < ir.getStmts().size() &&
63+
(ir.getStmt(nextStmt) instanceof Goto || ir.getStmt(nextStmt) instanceof Return)) {
4464
boolean exitInTryBlock = false;
45-
for(int i = entry.start().getIndex(); i <= entry.end().getIndex(); ++i){
46-
if(ir.getStmt(i) instanceof Return){
65+
for (int i = entry.start().getIndex(); i <= entry.end().getIndex(); ++i) {
66+
if (ir.getStmt(i) instanceof Return) {
4767
exitInTryBlock = true;
4868
break;
4969
}
5070
}
5171
Severity severity = Severity.MINOR;
52-
if(exceptionName.equals(ClassNames.ERROR) || exceptionName.equals(ClassNames.EXCEPTION)
53-
|| exceptionName.equals(ClassNames.THROWABLE) || exceptionName.equals(ClassNames.RUNTIME_EXCEPTION)){
72+
if (exceptionName.equals(ClassNames.ERROR) || exceptionName.equals(ClassNames.EXCEPTION)
73+
|| exceptionName.equals(ClassNames.THROWABLE) || exceptionName.equals(ClassNames.RUNTIME_EXCEPTION)) {
5474
severity = Severity.CRITICAL;
5575
}
5676
BugInstance bugInstance = new BugInstance(exitInTryBlock ? "DE_MIGHT_DROP" : "DE_MIGHT_IGNORE", severity)

src/main/java/pascal/taie/analysis/bugfinder/Severity.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/*
2+
* Tai-e: A Static Analysis Framework for Java
3+
*
4+
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
5+
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
6+
*
7+
* This file is part of Tai-e.
8+
*
9+
* Tai-e is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License
11+
* as published by the Free Software Foundation, either version 3
12+
* of the License, or (at your option) any later version.
13+
*
14+
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17+
* Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
123
package pascal.taie.analysis.bugfinder;
224

325
public enum Severity {

0 commit comments

Comments
 (0)