-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathWarnNotThrown.java
More file actions
117 lines (107 loc) · 4.12 KB
/
WarnNotThrown.java
File metadata and controls
117 lines (107 loc) · 4.12 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package org.javacs.markup;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ThrowTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
class WarnNotThrown extends TreePathScanner<Void, Map<TreePath, String>> {
private final JavacTask task;
private CompilationUnitTree root;
private Map<String, TreePath> declaredExceptions = new HashMap<>();
private Set<String> observedExceptions = new HashSet<>();
WarnNotThrown(JavacTask task) {
this.task = task;
}
@Override
public Void visitCompilationUnit(CompilationUnitTree t, Map<TreePath, String> notThrown) {
root = t;
return super.visitCompilationUnit(t, notThrown);
}
@Override
public Void visitMethod(MethodTree t, Map<TreePath, String> notThrown) {
// Skip on abstract or native
if (t.getBody() == null) {
return null;
}
// Create a new method scope
var pushDeclared = declaredExceptions;
var pushObserved = observedExceptions;
declaredExceptions = declared(t);
observedExceptions = new HashSet<>();
// Recursively scan for 'throw' and method calls
super.visitMethod(t, notThrown);
// Check for exceptions that were never thrown
for (var exception : declaredExceptions.keySet()) {
if (!observedExceptions.contains(exception)) {
notThrown.put(declaredExceptions.get(exception), exception);
}
}
declaredExceptions = pushDeclared;
observedExceptions = pushObserved;
return null;
}
private Map<String, TreePath> declared(MethodTree t) {
var trees = Trees.instance(task);
var names = new HashMap<String, TreePath>();
for (var e : t.getThrows()) {
var path = new TreePath(getCurrentPath(), e);
var to = trees.getElement(path);
if (!(to instanceof TypeElement)) continue;
var type = (TypeElement) to;
var name = type.getQualifiedName().toString();
names.put(name, path);
}
return names;
}
@Override
public Void visitThrow(ThrowTree t, Map<TreePath, String> notThrown) {
var path = new TreePath(getCurrentPath(), t.getExpression());
var type = Trees.instance(task).getTypeMirror(path);
addThrown(type);
return super.visitThrow(t, notThrown);
}
@Override
public Void visitNewClass(NewClassTree t, Map<TreePath, String> notThrown) {
var trees = Trees.instance(task);
var target = trees.getElement(getCurrentPath());
if (target instanceof ExecutableElement) {
var method = (ExecutableElement) target;
for (var type : method.getThrownTypes()) {
addThrown(type);
}
}
return super.visitNewClass(t, notThrown);
}
@Override
public Void visitMethodInvocation(MethodInvocationTree t, Map<TreePath, String> notThrown) {
var trees = Trees.instance(task);
var target = trees.getElement(getCurrentPath());
if (target instanceof ExecutableElement) {
var method = (ExecutableElement) target;
for (var type : method.getThrownTypes()) {
addThrown(type);
}
}
return super.visitMethodInvocation(t, notThrown);
}
private void addThrown(TypeMirror type) {
if (type instanceof DeclaredType) {
var declared = (DeclaredType) type;
var el = (TypeElement) declared.asElement();
var name = el.getQualifiedName().toString();
observedExceptions.add(name);
}
}
}