Skip to content

Commit 46da79c

Browse files
committed
Fix quality flaw
1 parent 5599290 commit 46da79c

2 files changed

Lines changed: 74 additions & 85 deletions

File tree

java-squid/src/main/java/org/sonar/java/se/ConstraintManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public SymbolicValue createSymbolicValue(Tree syntaxNode) {
5353
result = new SymbolicValue.InstanceOfSymbolicValue(counter);
5454
break;
5555
default:
56-
result = new SymbolicValue.ObjectSymbolicValue(counter);
56+
result = new SymbolicValue(counter);
5757
}
5858
counter++;
5959
return result;

java-squid/src/main/java/org/sonar/java/se/SymbolicValue.java

Lines changed: 73 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -27,115 +27,102 @@
2727
import java.util.ArrayList;
2828
import java.util.List;
2929

30-
public interface SymbolicValue {
30+
public class SymbolicValue {
3131

32-
SymbolicValue NULL_LITERAL = new ObjectSymbolicValue(0) {
32+
public static final SymbolicValue NULL_LITERAL = new SymbolicValue(0) {
3333
@Override
3434
public String toString() {
3535
return super.toString() + "_NULL";
3636
}
3737
};
38-
SymbolicValue TRUE_LITERAL = new ObjectSymbolicValue(1) {
38+
39+
public static final SymbolicValue TRUE_LITERAL = new SymbolicValue(1) {
3940
@Override
4041
public String toString() {
4142
return super.toString() + "_TRUE";
4243
}
4344
};
44-
SymbolicValue FALSE_LITERAL = new ObjectSymbolicValue(2) {
45+
46+
public static final SymbolicValue FALSE_LITERAL = new SymbolicValue(2) {
4547
@Override
4648
public String toString() {
4749
return super.toString() + "_FALSE";
4850
}
4951
};
5052

51-
void computedFrom(List<SymbolicValue> symbolicValues);
52-
53-
ProgramState setSingleConstraint(ProgramState state, NullConstraint notNull);
54-
55-
List<ProgramState> setConstraint(ProgramState programState, BooleanConstraint booleanConstraint);
56-
57-
List<ProgramState> setConstraint(ProgramState programState, NullConstraint nullConstraint);
58-
59-
class ObjectSymbolicValue implements SymbolicValue {
53+
private final int id;
6054

61-
private final int id;
55+
public SymbolicValue(int id) {
56+
this.id = id;
57+
}
6258

63-
public ObjectSymbolicValue(int id) {
64-
this.id = id;
59+
@Override
60+
public boolean equals(Object o) {
61+
if (this == o) {
62+
return true;
6563
}
66-
67-
@Override
68-
public boolean equals(Object o) {
69-
if (this == o) {
70-
return true;
71-
}
72-
if (o == null || getClass() != o.getClass()) {
73-
return false;
74-
}
75-
ObjectSymbolicValue that = (ObjectSymbolicValue) o;
76-
return id == that.id;
64+
if (o == null || getClass() != o.getClass()) {
65+
return false;
7766
}
67+
SymbolicValue that = (SymbolicValue) o;
68+
return id == that.id;
69+
}
7870

79-
@Override
80-
public int hashCode() {
81-
return id;
82-
}
71+
@Override
72+
public int hashCode() {
73+
return id;
74+
}
8375

84-
@Override
85-
public String toString() {
86-
return "SV#" + id;
87-
}
76+
@Override
77+
public String toString() {
78+
return "SV#" + id;
79+
}
8880

89-
@Override
90-
public void computedFrom(List<SymbolicValue> symbolicValues) {
91-
// no op in general case
92-
}
81+
public void computedFrom(List<SymbolicValue> symbolicValues) {
82+
// no op in general case
83+
}
9384

94-
@Override
95-
public List<ProgramState> setConstraint(ProgramState programState, NullConstraint nullConstraint) {
96-
Object data = programState.getConstraint(this);
97-
if (data instanceof NullConstraint) {
98-
NullConstraint nc = (NullConstraint) data;
99-
if (!nc.equals(nullConstraint)) {
100-
// setting null where value is known to be non null or the contrary
101-
return ImmutableList.of();
102-
}
103-
}
104-
if (data == null || !data.equals(nullConstraint)) {
105-
return ImmutableList.of(programState.addConstraint(this, nullConstraint));
85+
public List<ProgramState> setConstraint(ProgramState programState, NullConstraint nullConstraint) {
86+
Object data = programState.getConstraint(this);
87+
if (data instanceof NullConstraint) {
88+
NullConstraint nc = (NullConstraint) data;
89+
if (!nc.equals(nullConstraint)) {
90+
// setting null where value is known to be non null or the contrary
91+
return ImmutableList.of();
10692
}
107-
return ImmutableList.of(programState);
10893
}
94+
if (data == null || !data.equals(nullConstraint)) {
95+
return ImmutableList.of(programState.addConstraint(this, nullConstraint));
96+
}
97+
return ImmutableList.of(programState);
98+
}
10999

110-
@Override
111-
public List<ProgramState> setConstraint(ProgramState programState, BooleanConstraint booleanConstraint) {
112-
Object data = programState.getConstraint(this);
113-
// update program state only for a different constraint
114-
if (data instanceof BooleanConstraint) {
115-
BooleanConstraint bc = (BooleanConstraint) data;
116-
if (!bc.equals(booleanConstraint)) {
117-
// setting null where value is known to be non null or the contrary
118-
return ImmutableList.of();
119-
}
120-
}
121-
if ((data == null || !data.equals(booleanConstraint)) && programState.canReach(this)) {
122-
// store constraint only if symbolic value can be reached by a symbol.
123-
return ImmutableList.of(programState.addConstraint(this, booleanConstraint));
100+
public List<ProgramState> setConstraint(ProgramState programState, BooleanConstraint booleanConstraint) {
101+
Object data = programState.getConstraint(this);
102+
// update program state only for a different constraint
103+
if (data instanceof BooleanConstraint) {
104+
BooleanConstraint bc = (BooleanConstraint) data;
105+
if (!bc.equals(booleanConstraint)) {
106+
// setting null where value is known to be non null or the contrary
107+
return ImmutableList.of();
124108
}
125-
return ImmutableList.of(programState);
126109
}
110+
if ((data == null || !data.equals(booleanConstraint)) && programState.canReach(this)) {
111+
// store constraint only if symbolic value can be reached by a symbol.
112+
return ImmutableList.of(programState.addConstraint(this, booleanConstraint));
113+
}
114+
return ImmutableList.of(programState);
115+
}
127116

128-
@Override
129-
public ProgramState setSingleConstraint(ProgramState programState, NullConstraint nullConstraint) {
130-
final List<ProgramState> states = setConstraint(programState, nullConstraint);
131-
if (states.size() != 1) {
132-
throw new IllegalStateException("Only a single program state is expected at this location");
133-
}
134-
return states.get(0);
117+
public ProgramState setSingleConstraint(ProgramState programState, NullConstraint nullConstraint) {
118+
final List<ProgramState> states = setConstraint(programState, nullConstraint);
119+
if (states.size() != 1) {
120+
throw new IllegalStateException("Only a single program state is expected at this location");
135121
}
122+
return states.get(0);
136123
}
137124

138-
abstract class BinarySymbolicValue extends ObjectSymbolicValue {
125+
abstract static class BinarySymbolicValue extends SymbolicValue {
139126

140127
SymbolicValue leftOp;
141128
SymbolicValue rightOp;
@@ -200,7 +187,8 @@ private List<ProgramState> copyConstraint(SymbolicValue from, SymbolicValue to,
200187
}
201188

202189
}
203-
class NotEqualToSymbolicValue extends BinarySymbolicValue {
190+
191+
static class NotEqualToSymbolicValue extends BinarySymbolicValue {
204192

205193
public NotEqualToSymbolicValue(int id) {
206194
super(id);
@@ -216,7 +204,8 @@ BooleanConstraint shouldNotInverse() {
216204
return BooleanConstraint.FALSE;
217205
}
218206
}
219-
class EqualToSymbolicValue extends BinarySymbolicValue {
207+
208+
static class EqualToSymbolicValue extends BinarySymbolicValue {
220209

221210
public EqualToSymbolicValue(int id) {
222211
super(id);
@@ -229,7 +218,7 @@ BooleanConstraint shouldNotInverse() {
229218

230219
}
231220

232-
abstract class UnarySymbolicValue extends ObjectSymbolicValue {
221+
abstract static class UnarySymbolicValue extends SymbolicValue {
233222
protected SymbolicValue operand;
234223

235224
public UnarySymbolicValue(int id) {
@@ -244,7 +233,7 @@ public void computedFrom(List<SymbolicValue> symbolicValues) {
244233

245234
}
246235

247-
class NotSymbolicValue extends UnarySymbolicValue {
236+
static class NotSymbolicValue extends UnarySymbolicValue {
248237

249238
public NotSymbolicValue(int id) {
250239
super(id);
@@ -256,7 +245,7 @@ public List<ProgramState> setConstraint(ProgramState programState, BooleanConstr
256245
}
257246
}
258247

259-
class InstanceOfSymbolicValue extends UnarySymbolicValue {
248+
static class InstanceOfSymbolicValue extends UnarySymbolicValue {
260249
public InstanceOfSymbolicValue(int id) {
261250
super(id);
262251
}
@@ -281,7 +270,7 @@ public List<ProgramState> setConstraint(ProgramState programState, BooleanConstr
281270
}
282271
}
283272

284-
abstract class BooleanExpressionSymbolicValue extends BinarySymbolicValue {
273+
abstract static class BooleanExpressionSymbolicValue extends BinarySymbolicValue {
285274

286275
protected BooleanExpressionSymbolicValue(int id) {
287276
super(id);
@@ -293,7 +282,7 @@ BooleanConstraint shouldNotInverse() {
293282
}
294283
}
295284

296-
class AndSymbolicValue extends BooleanExpressionSymbolicValue {
285+
static class AndSymbolicValue extends BooleanExpressionSymbolicValue {
297286

298287
public AndSymbolicValue(int id) {
299288
super(id);
@@ -327,7 +316,7 @@ public String toString() {
327316
}
328317
}
329318

330-
class OrSymbolicValue extends BooleanExpressionSymbolicValue {
319+
static class OrSymbolicValue extends BooleanExpressionSymbolicValue {
331320

332321
public OrSymbolicValue(int id) {
333322
super(id);
@@ -361,7 +350,7 @@ public String toString() {
361350
}
362351
}
363352

364-
class XorSymbolicValue extends BooleanExpressionSymbolicValue {
353+
static class XorSymbolicValue extends BooleanExpressionSymbolicValue {
365354

366355
public XorSymbolicValue(int id) {
367356
super(id);

0 commit comments

Comments
 (0)