Skip to content

Commit 07a4c63

Browse files
committed
Fix NPE
1 parent a532c05 commit 07a4c63

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

liquidjava-verifier/src/main/java/liquidjava/processor/context/ContextHistory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import spoon.reflect.cu.SourcePosition;
99
import spoon.reflect.declaration.CtElement;
1010
import spoon.reflect.declaration.CtExecutable;
11-
import spoon.reflect.declaration.CtMethod;
1211

1312
public class ContextHistory {
1413
private static ContextHistory instance;
15-
14+
1615
private Map<String, Map<String, Set<RefinedVariable>>> vars; // file -> (scope -> variables in scope)
1716
private Set<RefinedVariable> instanceVars;
1817
private Set<RefinedVariable> globalVars;
@@ -49,10 +48,9 @@ public void saveContext(CtElement element, Context context) {
4948
// add variables in scope for this position
5049
String file = pos.getFile().getAbsolutePath();
5150
String scope = getScopePosition(element);
52-
System.out.println("Saving context for " + file + " in scope " + scope);
5351
vars.putIfAbsent(file, new HashMap<>());
5452
vars.get(file).put(scope, new HashSet<>(context.getCtxVars()));
55-
53+
5654
// add other elements in context
5755
instanceVars.addAll(context.getCtxInstanceVars());
5856
globalVars.addAll(context.getCtxGlobalVars());
@@ -62,8 +60,13 @@ public void saveContext(CtElement element, Context context) {
6260

6361
public String getScopePosition(CtElement element) {
6462
SourcePosition pos = element.getPosition();
65-
SourcePosition innerPosition = element instanceof CtExecutable ? ((CtExecutable<?>) element).getBody().getPosition() : pos;
66-
return String.format("%d:%d-%d:%d", innerPosition.getLine(), innerPosition.getColumn() + 1, pos.getEndLine(), pos.getEndColumn());
63+
SourcePosition innerPosition = pos;
64+
if (element instanceof CtExecutable<?> executable) {
65+
if (executable.getBody() != null)
66+
innerPosition = executable.getBody().getPosition();
67+
}
68+
return String.format("%d:%d-%d:%d", innerPosition.getLine(), innerPosition.getColumn() + 1, pos.getEndLine(),
69+
pos.getEndColumn());
6770
}
6871

6972
public Map<String, Map<String, Set<RefinedVariable>>> getVars() {

0 commit comments

Comments
 (0)