-
Notifications
You must be signed in to change notification settings - Fork 22
/
ConstantStringExpressionEvaluator.java
69 lines (59 loc) · 2.75 KB
/
ConstantStringExpressionEvaluator.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
package de.uka.ilkd.key.java.recoderext;
import de.uka.ilkd.key.java.Recoder2KeY;
import recoder.CrossReferenceServiceConfiguration;
import recoder.abstraction.Type;
import recoder.java.Expression;
import recoder.java.NonTerminalProgramElement;
import recoder.java.ProgramElement;
import recoder.java.SourceElement;
import recoder.java.declaration.TypeDeclaration;
import recoder.java.expression.literal.NullLiteral;
import recoder.java.expression.literal.StringLiteral;
import recoder.service.ConstantEvaluator;
public class ConstantStringExpressionEvaluator extends RecoderModelTransformer {
public ConstantStringExpressionEvaluator(CrossReferenceServiceConfiguration services,
TransformerCache cache) {
super(services, cache);
}
private void evaluateConstantStringExpressions(NonTerminalProgramElement td) {
for (int i = 0; i < td.getChildCount(); i++) {
ProgramElement pe = td.getChildAt(i);
if (pe instanceof Expression) {
ConstantEvaluator cee = services.getConstantEvaluator();
ConstantEvaluator.EvaluationResult res = new ConstantEvaluator.EvaluationResult();
Type expType = null;
try {
expType = services.getSourceInfo().getType((Expression) pe);
} catch (Exception e) {
// on failure: report with position information
SourceElement.Position pos = pe.getStartPosition();
int[] posArr = new int[] { pos.getLine(), pos.getColumn() };
String file = td.compilationUnit().getDataLocation().toString();
Recoder2KeY.reportErrorWithPositionInFile(
"failed to perform constant evaluation", e,
posArr, file);
}
if (!(pe instanceof NullLiteral) && expType != null
&& expType.getFullName().equals("java.lang.String")) {
boolean isCTC = false;
try {
isCTC = cee.isCompileTimeConstant((Expression) pe, res);
} catch (java.lang.ArithmeticException t) {
//
}
if (isCTC && res.getTypeCode() == ConstantEvaluator.STRING_TYPE) {
replace(pe, new StringLiteral("\"" + res.getString() + "\""));
continue;
}
}
}
if (pe instanceof NonTerminalProgramElement) {
evaluateConstantStringExpressions((NonTerminalProgramElement) pe);
}
}
}
@Override
protected void makeExplicit(TypeDeclaration td) {
evaluateConstantStringExpressions(td);
}
}