summaryrefslogtreecommitdiff
path: root/src/main/java/org/javacs/WarnUnused.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/javacs/WarnUnused.java')
-rw-r--r--src/main/java/org/javacs/WarnUnused.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/org/javacs/WarnUnused.java b/src/main/java/org/javacs/WarnUnused.java
new file mode 100644
index 0000000..451a524
--- /dev/null
+++ b/src/main/java/org/javacs/WarnUnused.java
@@ -0,0 +1,63 @@
+package org.javacs;
+
+import com.sun.source.tree.*;
+import com.sun.source.util.*;
+import java.util.*;
+import javax.lang.model.element.*;
+
+class WarnUnused extends TreePathScanner<Void, Void> {
+ private final Trees trees;
+ private final Set<Element> declared = new HashSet<>(), used = new HashSet<>();
+
+ WarnUnused(JavacTask task) {
+ this.trees = Trees.instance(task);
+ }
+
+ Set<Element> notUsed() {
+ declared.removeAll(used);
+ return declared;
+ }
+
+ Element current() {
+ return trees.getElement(getCurrentPath());
+ }
+
+ boolean isPrivate(VariableTree t) {
+ return t.getModifiers().getFlags().contains(Modifier.PRIVATE);
+ }
+
+ boolean isLocal(VariableTree t) {
+ var parent = getCurrentPath().getParentPath().getLeaf();
+ return !(parent instanceof ClassTree);
+ }
+
+ boolean isPrivate(MethodTree t) {
+ return t.getModifiers().getFlags().contains(Modifier.PRIVATE);
+ }
+
+ @Override
+ public Void visitVariable(VariableTree t, Void __) {
+ if (isPrivate(t) || isLocal(t)) {
+ declared.add(current());
+ }
+ return super.visitVariable(t, null);
+ }
+
+ @Override
+ public Void visitIdentifier(IdentifierTree t, Void __) {
+ used.add(current());
+ return super.visitIdentifier(t, null);
+ }
+
+ @Override
+ public Void visitMemberSelect(MemberSelectTree t, Void __) {
+ used.add(current());
+ return super.visitMemberSelect(t, null);
+ }
+
+ @Override
+ public Void visitMemberReference(MemberReferenceTree t, Void __) {
+ used.add(current());
+ return super.visitMemberReference(t, null);
+ }
+}