Skip to content

Commit 95d3587

Browse files
committed
Fix quality flaw: improve coverage and testcases
1 parent a668171 commit 95d3587

3 files changed

Lines changed: 10 additions & 23 deletions

File tree

java-squid/src/main/java/org/sonar/java/cfg/LiveVariables.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.sonar.plugins.java.api.tree.VariableTree;
3333

3434
import javax.annotation.Nullable;
35-
import java.io.PrintStream;
3635
import java.util.Collections;
3736
import java.util.Deque;
3837
import java.util.HashMap;
@@ -166,14 +165,4 @@ private static List<Symbol> getUsedVariables(@Nullable Tree syntaxNode, Symbol.M
166165
return extractorFromClass.usedVariables();
167166
}
168167

169-
public void debugTo(PrintStream outStream) {
170-
for (CFG.Block block : cfg.reversedBlocks()) {
171-
outStream.println("B" + block.id + " Live:");
172-
for (Symbol live : out.get(block)) {
173-
outStream.print(" " + live.name() + "@" + Integer.toHexString(live.hashCode()));
174-
}
175-
outStream.println();
176-
}
177-
}
178-
179168
}

java-squid/src/test/java/org/sonar/java/cfg/LiveVariablesTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public void test_simple_live() {
5454
}
5555

5656
@Test
57-
public void test_simple_death() throws Exception {
57+
public void test_simple_death() {
5858
CFG cfg = buildCFG("void foo(int a) { int i; /* should not be live here */ if (false) ; i = 0; }");
5959
LiveVariables liveVariables = LiveVariables.analyze(cfg);
6060
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(3))).isEmpty();
6161
}
6262

6363
@Test
64-
public void test_field_not_tracked() throws Exception {
64+
public void test_field_not_tracked() {
6565
CFG cfg = buildCFG("void foo(int a) { field = 0; /* fields should not be tracked */ if (false) ; foo(field); }");
6666
LiveVariables liveVariables = LiveVariables.analyze(cfg);
6767
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(3))).isEmpty();
@@ -72,15 +72,15 @@ public void test_field_not_tracked() throws Exception {
7272
}
7373

7474
@Test
75-
public void test_while_loop() throws Exception {
75+
public void test_while_loop() {
7676
CFG cfg = buildCFG("void foo(boolean condition) { while (condition) { int x = 0; use(x); x = 1; /* x should not be live here*/}}");
7777
LiveVariables liveVariables = LiveVariables.analyze(cfg);
7878
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(3))).hasSize(1);
7979
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(4))).hasSize(1);
8080
}
8181

8282
@Test
83-
public void in_of_first_block_should_be_empty() throws Exception {
83+
public void in_of_first_block_should_be_empty() {
8484
CFG cfg = buildCFG("boolean foo(int a) { foo(a);}");
8585
LiveVariables liveVariables = LiveVariables.analyze(cfg);
8686
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(0))).isEmpty();
@@ -103,23 +103,21 @@ public void lambdas_read_liveness() {
103103
}
104104

105105
@Test
106-
public void anonymous_class_liveness() throws Exception {
106+
public void anonymous_class_liveness() {
107107
CFG cfg = buildCFG("boolean foo(int a) { if(true) { System.out.println(''); } new Object() { String toString(){return a;} }; }");
108108
LiveVariables liveVariables = LiveVariables.analyze(cfg);
109109
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(0))).isEmpty();
110110
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(2))).hasSize(1);
111111
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(3))).hasSize(1);
112112

113-
cfg = buildCFG("boolean foo(int a) { if(true) { System.out.println(''); } new Object() { String toString(){return a = 2;} }; }");
113+
cfg = buildCFG("boolean foo(int a) { if(true) { new A().field = 2;System.out.println(''); } new Object() { String toString(){return a = 2;} }; }");
114114
liveVariables = LiveVariables.analyze(cfg);
115+
cfg.debugTo(System.out);
115116
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(0))).isEmpty();
116117
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(2))).isEmpty();
117118
assertThat(liveVariables.getOut(cfg.reversedBlocks().get(3))).isEmpty();
118119
}
120+
121+
119122

120-
@Test
121-
public void assignement_with_parenthesis() throws Exception {
122-
123-
124-
}
125123
}

java-squid/src/test/java/org/sonar/java/cfg/LocalVariableReadExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void should_extract_local_vars_read() {
5757

5858
@Test
5959
public void should_not_extract_local_vars_written() throws Exception {
60-
MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { a = false;} }; }");
60+
MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { new A().field = 0; a = false;} }; }");
6161
StatementTree statementTree = methodTree.block().body().get(0);
6262
LocalVariableReadExtractor extractor = new LocalVariableReadExtractor(methodTree.symbol());
6363
statementTree.accept(extractor);

0 commit comments

Comments
 (0)