Skip to content

Commit 6ebe59b

Browse files
Amend testcases of bugfinder.
1 parent 8e5db8b commit 6ebe59b

10 files changed

Lines changed: 110 additions & 114 deletions

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

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,75 +25,62 @@
2525
import pascal.taie.language.classes.JClass;
2626
import pascal.taie.language.classes.JMethod;
2727

28+
import javax.annotation.Nonnull;
2829
import java.util.Objects;
2930

30-
// TODO: refactor it with more precise context information.
31+
//TODO: refactor it with more precise context information.
3132
public class BugInstance implements Comparable<BugInstance> {
3233

3334
private final BugType type;
3435

3536
private final Severity severity;
3637

37-
private final JClass jClass;
38+
private JClass jClass;
3839

39-
private final JMethod jMethod;
40+
private JMethod jMethod;
4041

41-
private int sourceLineStart = -1;
42+
private int sourceLineStart = -1, sourceLineEnd = -2;
4243

43-
private int sourceLineEnd = -1;
44-
45-
public BugInstance(BugType type, Severity severity, JClass jClass) {
46-
this.type = type;
47-
this.severity = severity;
48-
this.jClass = jClass;
49-
this.jMethod = null;
50-
}
51-
52-
public BugInstance(BugType type, Severity severity, JMethod jMethod) {
44+
public BugInstance(@Nonnull BugType type, Severity severity) {
5345
this.type = type;
5446
this.severity = severity;
55-
this.jClass = jMethod.getDeclaringClass();
56-
this.jMethod = jMethod;
57-
}
58-
59-
public BugType getType() {
60-
return type;
6147
}
6248

6349
public Severity getSeverity() {
6450
return severity;
6551
}
6652

67-
public BugInstance setSourceLine(int start, int end) {
68-
sourceLineStart = start;
69-
sourceLineEnd = end;
70-
return this;
53+
private static String getString(Object o) {
54+
return o == null ? "empty" : o.toString();
7155
}
7256

73-
public BugInstance setSourceLine(int num) {
74-
return setSourceLine(num, num);
57+
public static BugInstance newBugInstance(BugType type, Severity severity, JMethod method, int lineNum) {
58+
return new BugInstance(type, severity).setClassAndMethod(method).setSourceLine(lineNum);
7559
}
7660

77-
public int getSourceLineStart() {
78-
return sourceLineStart;
61+
@Override
62+
public String toString() {
63+
String sourcelineRange = "empty";
64+
if (sourceLineStart >= 0) {
65+
sourcelineRange = sourceLineStart == sourceLineEnd ? String.valueOf(sourceLineStart) :
66+
sourceLineStart + "---" + sourceLineEnd;
67+
}
68+
return String.format("Class: %s, Method: %s, LineNumber: %s. bug type: %s, severity: %s",
69+
getString(jClass), getString(jMethod), sourcelineRange, type, severity
70+
);
7971
}
8072

81-
public int getSourceLineEnd() {
82-
return sourceLineEnd;
73+
public BugType getType() {
74+
return type;
8375
}
8476

8577
@Override
8678
public boolean equals(Object o) {
87-
if (this == o) {
88-
return true;
89-
}
90-
if (!(o instanceof BugInstance bugInstance)) {
91-
return false;
92-
}
93-
return type.equals(bugInstance.type)
94-
&& Objects.equals(jClass, bugInstance.jClass)
95-
&& Objects.equals(jMethod, bugInstance.jMethod)
96-
&& sourceLineStart == bugInstance.sourceLineStart
79+
if (o == this) return true;
80+
if (!(o instanceof BugInstance bugInstance)) return false;
81+
82+
return type.equals(bugInstance.type) && Objects.equals(jClass, bugInstance.jClass)
83+
&& Objects.equals(jMethod, bugInstance.jMethod) && sourceLineStart == bugInstance.sourceLineStart
9784
&& sourceLineEnd == bugInstance.sourceLineEnd;
9885
}
9986

@@ -103,23 +90,43 @@ public int hashCode() {
10390
}
10491

10592
@Override
106-
public String toString() {
107-
String sourceLineRange = "null";
108-
if (sourceLineStart >= 0) {
109-
sourceLineRange = sourceLineStart == sourceLineEnd
110-
? String.valueOf(sourceLineStart)
111-
: sourceLineStart + "---" + sourceLineEnd;
112-
}
113-
return String.format("Class: %s, Method: %s, LineNumber: %s, BugType: %s, Severity: %s",
114-
jClass, jMethod, sourceLineRange, type, severity);
93+
public int compareTo(BugInstance o) {
94+
return Integer.compare(sourceLineStart, o.sourceLineStart);
11595
}
11696

117-
@Override
118-
public int compareTo(BugInstance o) {
119-
if (jClass.equals(o.jClass)) {
120-
return Integer.compare(sourceLineStart, o.sourceLineStart);
121-
} else {
122-
return jClass.toString().compareTo(o.jClass.toString());
123-
}
97+
public BugInstance setClassAndMethod(JMethod method) {
98+
setMethod(method);
99+
setClass(method.getDeclaringClass());
100+
return this;
101+
}
102+
103+
public BugInstance setClass(JClass clazz) {
104+
jClass = clazz;
105+
return this;
106+
}
107+
108+
public BugInstance setMethod(JMethod method) {
109+
jMethod = method;
110+
return this;
111+
}
112+
113+
public BugInstance setSourceLine(int num) {
114+
sourceLineStart = sourceLineEnd = num;
115+
return this;
124116
}
117+
118+
public BugInstance setSourceLine(int start, int end) {
119+
sourceLineStart = start;
120+
sourceLineEnd = end;
121+
return this;
122+
}
123+
124+
public int getSourceLineStart() {
125+
return sourceLineStart;
126+
}
127+
128+
public int getSourceLineEnd() {
129+
return sourceLineEnd;
130+
}
131+
125132
}
Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
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-
241
package pascal.taie.analysis.bugfinder;
252

26-
import org.junit.jupiter.api.Test;
27-
import org.junit.jupiter.params.ParameterizedTest;
28-
import org.junit.jupiter.params.provider.ValueSource;
3+
import org.junit.Test;
294
import pascal.taie.analysis.Tests;
305
import pascal.taie.analysis.bugfinder.nullpointer.IsNullAnalysis;
316

327
public class IsNullTest {
338

349
private static final String folderPath = "src/test/resources/bugfinder";
3510

36-
@ParameterizedTest
37-
@ValueSource(strings = {
38-
"NullDeref",
39-
"NullDeref2",
40-
})
4111
void testIsNullValue(String inputClass) {
42-
Tests.testInput(inputClass, folderPath, IsNullAnalysis.ID);
12+
Tests.test(inputClass, folderPath, IsNullAnalysis.ID);
13+
}
14+
15+
@Test
16+
public void test() {
17+
testIsNullValue("NullDeref");
18+
}
19+
20+
@Test
21+
public void test2() {
22+
testIsNullValue("NullDeref2");
4323
}
4424

25+
// @Test
26+
// public void testAnnotation() {
27+
// List<String> args = new ArrayList<>();
28+
// Collections.addAll(args, "-cp", folderPath + ";" + folderPath + "/jsr305-3.0.2.jar");
29+
// Collections.addAll(args, "-m", "NullAnnotation");
30+
// Collections.addAll(args, "-a", IsNullAnalysis.ID);
31+
// Main.main(args.toArray(new String[0]));
32+
//// Tests.test("NullAnnotation", folderPath + ";" + folderPath + "/jsr305-3.0.2.jar", IsNullAnalysis.ID);
33+
// }
4534
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-------------------- CloneIdiom1 (clone-idiom) --------------------
2-
Class: CloneIdiom1, Method: null, LineNumber: null, BugType: CN_IDIOM, Severity: MINOR
2+
Class: CloneIdiom1, Method: empty, LineNumber: empty. bug type: CN_IDIOM, severity: MINOR
33

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-------------------- CloneIdiom2 (clone-idiom) --------------------
2-
Class: CloneIdiom2, Method: null, LineNumber: null, BugType: CN_IDIOM_NO_SUPER_CALL, Severity: MINOR
2+
Class: CloneIdiom2, Method: empty, LineNumber: empty. bug type: CN_IDIOM_NO_SUPER_CALL, severity: MINOR
33

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-------------------- CloneIdiom3 (clone-idiom) --------------------
2-
Class: CloneIdiom3, Method: null, LineNumber: null, BugType: CN_IDIOM_NO_SUPER_CALL, Severity: MINOR
2+
Class: CloneIdiom3, Method: empty, LineNumber: empty. bug type: CN_IDIOM_NO_SUPER_CALL, severity: MINOR
33

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-------------------- CloneIdiom4 (clone-idiom) --------------------
2-
Class: CloneIdiom4, Method: null, LineNumber: null, BugType: CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE, Severity: MINOR
2+
Class: CloneIdiom4, Method: empty, LineNumber: empty. bug type: CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE, severity: MINOR
33

src/test/resources/bugfinder/DroppedException-dropped-exception-expected.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
-------------------- <DroppedException: DroppedException cloneException()> (dropped-exception) --------------------
1010

1111
-------------------- <DroppedException: void emptyCatchBlock1()> (dropped-exception) --------------------
12-
Class: DroppedException, Method: <DroppedException: void emptyCatchBlock1()>, LineNumber: 43, BugType: DE_MIGHT_IGNORE, Severity: CRITICAL
12+
Class: DroppedException, Method: <DroppedException: void emptyCatchBlock1()>, LineNumber: 43. bug type: DE_MIGHT_IGNORE, severity: CRITICAL
1313

1414
-------------------- <DroppedException: void emptyCatchBlock2()> (dropped-exception) --------------------
15-
Class: DroppedException, Method: <DroppedException: void emptyCatchBlock2()>, LineNumber: 50, BugType: DE_MIGHT_IGNORE, Severity: CRITICAL
16-
Class: DroppedException, Method: <DroppedException: void emptyCatchBlock2()>, LineNumber: 51, BugType: DE_MIGHT_IGNORE, Severity: CRITICAL
15+
Class: DroppedException, Method: <DroppedException: void emptyCatchBlock2()>, LineNumber: 50. bug type: DE_MIGHT_IGNORE, severity: CRITICAL
16+
Class: DroppedException, Method: <DroppedException: void emptyCatchBlock2()>, LineNumber: 51. bug type: DE_MIGHT_IGNORE, severity: CRITICAL
1717

1818
-------------------- <DroppedException: void emptyCatchBlockWithFinally1()> (dropped-exception) --------------------
1919

2020
-------------------- <DroppedException: void emptyCatchBlockWithFinally2()> (dropped-exception) --------------------
21-
Class: DroppedException, Method: <DroppedException: void emptyCatchBlockWithFinally2()>, LineNumber: 72, BugType: DE_MIGHT_IGNORE, Severity: CRITICAL
21+
Class: DroppedException, Method: <DroppedException: void emptyCatchBlockWithFinally2()>, LineNumber: 72. bug type: DE_MIGHT_IGNORE, severity: CRITICAL
2222

2323
-------------------- <DroppedException: int exitInTryBlock()> (dropped-exception) --------------------
24-
Class: DroppedException, Method: <DroppedException: int exitInTryBlock()>, LineNumber: 81, BugType: DE_MIGHT_IGNORE, Severity: CRITICAL
25-
Class: DroppedException, Method: <DroppedException: int exitInTryBlock()>, LineNumber: 86, BugType: DE_MIGHT_IGNORE, Severity: CRITICAL
24+
Class: DroppedException, Method: <DroppedException: int exitInTryBlock()>, LineNumber: 81. bug type: DE_MIGHT_IGNORE, severity: CRITICAL
25+
Class: DroppedException, Method: <DroppedException: int exitInTryBlock()>, LineNumber: 86. bug type: DE_MIGHT_IGNORE, severity: CRITICAL
2626

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-------------------- <NullDeref: void nullObject()> (null-pointer) --------------------
2-
Class: NullDeref, Method: <NullDeref: void nullObject()>, LineNumber: 9, BugType: NP_ALWAYS_NULL, Severity: BLOCKER
2+
Class: NullDeref, Method: <NullDeref: void nullObject()>, LineNumber: 8. bug type: NP_ALWAYS_NULL, severity: BLOCKER
33

44
-------------------- <NullDeref: void main(java.lang.String[])> (null-pointer) --------------------
55

@@ -12,16 +12,16 @@ Class: NullDeref, Method: <NullDeref: void nullObject()>, LineNumber: 9, BugType
1212
-------------------- <NullDeref: void allStmts(java.lang.Object)> (null-pointer) --------------------
1313

1414
-------------------- <NullDeref: void nullArray()> (null-pointer) --------------------
15-
Class: NullDeref, Method: <NullDeref: void nullArray()>, LineNumber: 47, BugType: NP_ALWAYS_NULL, Severity: BLOCKER
15+
Class: NullDeref, Method: <NullDeref: void nullArray()>, LineNumber: 46. bug type: NP_ALWAYS_NULL, severity: BLOCKER
1616

1717
-------------------- <NullDeref: boolean equals(java.lang.Object)> (null-pointer) --------------------
1818

1919
-------------------- <NullDeref: void throwNull()> (null-pointer) --------------------
20-
Class: NullDeref, Method: <NullDeref: void throwNull()>, LineNumber: 57, BugType: NP_ALWAYS_NULL, Severity: BLOCKER
20+
Class: NullDeref, Method: <NullDeref: void throwNull()>, LineNumber: 56. bug type: NP_ALWAYS_NULL, severity: BLOCKER
2121

2222
-------------------- <NullDeref: void nullComparison()> (null-pointer) --------------------
23-
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 63, BugType: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, Severity: MAJOR
24-
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 66, BugType: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, Severity: MAJOR
25-
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 69, BugType: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, Severity: MAJOR
26-
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 73, BugType: RCN_REDUNDANT_COMPARISON_OF_NULL_AND_NONNULL_VALUE, Severity: MAJOR
23+
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 72. bug type: RCN_REDUNDANT_COMPARISON_OF_NULL_AND_NONNULL_VALUE, severity: MAJOR
24+
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 62. bug type: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, severity: MAJOR
25+
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 65. bug type: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, severity: MAJOR
26+
Class: NullDeref, Method: <NullDeref: void nullComparison()>, LineNumber: 68. bug type: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, severity: MAJOR
2727

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
-------------------- <NullDeref2: void <init>()> (null-pointer) --------------------
22

33
-------------------- <NullDeref2: void possibleNullOnSplit()> (null-pointer) --------------------
4-
Class: NullDeref2, Method: <NullDeref2: void possibleNullOnSplit()>, LineNumber: 5, BugType: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, Severity: MAJOR
5-
Class: NullDeref2, Method: <NullDeref2: void possibleNullOnSplit()>, LineNumber: 12, BugType: NP_MAY_NULL, Severity: CRITICAL
4+
Class: NullDeref2, Method: <NullDeref2: void possibleNullOnSplit()>, LineNumber: 12. bug type: NP_MAY_NULL, severity: CRITICAL
5+
Class: NullDeref2, Method: <NullDeref2: void possibleNullOnSplit()>, LineNumber: 5. bug type: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, severity: MAJOR
66

77
-------------------- <NullDeref2: boolean same0(java.lang.Object,java.lang.Object)> (null-pointer) --------------------
88

99
-------------------- <NullDeref2: boolean same1(int[],int[])> (null-pointer) --------------------
10-
Class: NullDeref2, Method: <NullDeref2: boolean same1(int[],int[])>, LineNumber: 32, BugType: NP_MAY_NULL, Severity: CRITICAL
10+
Class: NullDeref2, Method: <NullDeref2: boolean same1(int[],int[])>, LineNumber: 32. bug type: NP_MAY_NULL, severity: CRITICAL
1111

1212
-------------------- <NullDeref2: boolean same2(java.lang.Object,java.lang.Object)> (null-pointer) --------------------
13-
Class: NullDeref2, Method: <NullDeref2: boolean same2(java.lang.Object,java.lang.Object)>, LineNumber: 38, BugType: NP_MAY_NULL, Severity: CRITICAL
13+
Class: NullDeref2, Method: <NullDeref2: boolean same2(java.lang.Object,java.lang.Object)>, LineNumber: 38. bug type: NP_MAY_NULL, severity: CRITICAL
1414

1515
-------------------- <NullDeref2: boolean same3(java.lang.Object,java.lang.Object)> (null-pointer) --------------------
16-
Class: NullDeref2, Method: <NullDeref2: boolean same3(java.lang.Object,java.lang.Object)>, LineNumber: 45, BugType: NP_MAY_NULL, Severity: CRITICAL
16+
Class: NullDeref2, Method: <NullDeref2: boolean same3(java.lang.Object,java.lang.Object)>, LineNumber: 45. bug type: NP_MAY_NULL, severity: CRITICAL
1717

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
-------------------- <NullDeref3: void <init>()> (null-pointer) --------------------
22

33
-------------------- <NullDeref3: void foo()> (null-pointer) --------------------
4-
Class: NullDeref3, Method: <NullDeref3: void foo()>, LineNumber: 7, BugType: RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE, Severity: MAJOR
4+
Class: NullDeref3, Method: <NullDeref3: void foo()>, LineNumber: 6. bug type: RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE, severity: MAJOR
55

66
-------------------- <NullDeref3: java.lang.Object foo2(java.lang.Object)> (null-pointer) --------------------
7-
Class: NullDeref3, Method: <NullDeref3: java.lang.Object foo2(java.lang.Object)>, LineNumber: 20, BugType: RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE, Severity: MAJOR
7+
Class: NullDeref3, Method: <NullDeref3: java.lang.Object foo2(java.lang.Object)>, LineNumber: 19. bug type: RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE, severity: MAJOR
88

99
-------------------- <NullDeref3: java.lang.Object bar(java.lang.Object)> (null-pointer) --------------------
10-
Class: NullDeref3, Method: <NullDeref3: java.lang.Object bar(java.lang.Object)>, LineNumber: 31, BugType: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, Severity: MAJOR
11-
Class: NullDeref3, Method: <NullDeref3: java.lang.Object bar(java.lang.Object)>, LineNumber: 35, BugType: NP_ALWAYS_NULL, Severity: BLOCKER
10+
Class: NullDeref3, Method: <NullDeref3: java.lang.Object bar(java.lang.Object)>, LineNumber: 34. bug type: NP_ALWAYS_NULL, severity: BLOCKER
11+
Class: NullDeref3, Method: <NullDeref3: java.lang.Object bar(java.lang.Object)>, LineNumber: 30. bug type: RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE, severity: MAJOR
1212

0 commit comments

Comments
 (0)