summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2019-01-05 14:09:59 -0800
committerGeorge Fraser <george@fivetran.com>2019-01-05 14:09:59 -0800
commitec011d2dfa3f510f763561e94f50f1f509981f46 (patch)
tree1b47ee0e03b9ec6970f22b75865e0e64bf0cff43
parentb0c116dced0be0ab8ea7b2def7ae244c03236d62 (diff)
downloadjava-language-server-ec011d2dfa3f510f763561e94f50f1f509981f46.zip
Hint unused classes
-rw-r--r--src/main/java/org/javacs/JavaCompilerService.java2
-rw-r--r--src/main/java/org/javacs/JavaLanguageServer.java1
-rw-r--r--src/main/java/org/javacs/WarnUnused.java20
-rw-r--r--src/test/java/org/javacs/WarningsTest.java1
-rw-r--r--src/test/test-project/workspace/src/org/javacs/warn/Unused.java2
5 files changed, 20 insertions, 6 deletions
diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java
index 6c7dd8b..d251222 100644
--- a/src/main/java/org/javacs/JavaCompilerService.java
+++ b/src/main/java/org/javacs/JavaCompilerService.java
@@ -172,7 +172,7 @@ public class JavaCompilerService {
var path = trees.getPath(unusedEl);
var message = String.format("`%s` is not used", unusedEl.getSimpleName());
Diagnostic.Kind kind;
- if (unusedEl instanceof ExecutableElement) {
+ if (unusedEl instanceof ExecutableElement || unusedEl instanceof TypeElement) {
kind = Diagnostic.Kind.OTHER;
} else {
kind = Diagnostic.Kind.WARNING;
diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java
index a8e79e6..af8ba41 100644
--- a/src/main/java/org/javacs/JavaLanguageServer.java
+++ b/src/main/java/org/javacs/JavaLanguageServer.java
@@ -801,7 +801,6 @@ class JavaLanguageServer extends LanguageServer {
if (!isJavaFile(toUri)) return Optional.empty();
var toLine = position.position.line + 1;
var toColumn = position.position.character + 1;
- var toContent = contents(toUri);
// Compile from-file and identify element under cursor
LOG.warning(String.format("Looking for references to %s(%d,%d)...", toUri.getPath(), toLine, toColumn));
diff --git a/src/main/java/org/javacs/WarnUnused.java b/src/main/java/org/javacs/WarnUnused.java
index 35c7165..dc2646c 100644
--- a/src/main/java/org/javacs/WarnUnused.java
+++ b/src/main/java/org/javacs/WarnUnused.java
@@ -26,6 +26,14 @@ class WarnUnused extends TreePathScanner<Void, Void> {
return t.getModifiers().getFlags().contains(Modifier.PRIVATE);
}
+ boolean isPrivate(MethodTree t) {
+ return t.getModifiers().getFlags().contains(Modifier.PRIVATE);
+ }
+
+ boolean isPrivate(ClassTree t) {
+ return t.getModifiers().getFlags().contains(Modifier.PRIVATE);
+ }
+
boolean isLocal(VariableTree t) {
var parent = getCurrentPath().getParentPath().getLeaf();
return !(parent instanceof ClassTree)
@@ -33,10 +41,6 @@ class WarnUnused extends TreePathScanner<Void, Void> {
&& !(parent instanceof LambdaExpressionTree);
}
- boolean isPrivate(MethodTree t) {
- return t.getModifiers().getFlags().contains(Modifier.PRIVATE);
- }
-
boolean isEmptyConstructor(MethodTree t) {
return t.getParameters().isEmpty() && trees.getElement(getCurrentPath()).getKind() == ElementKind.CONSTRUCTOR;
}
@@ -58,6 +62,14 @@ class WarnUnused extends TreePathScanner<Void, Void> {
}
@Override
+ public Void visitClass(ClassTree t, Void __) {
+ if (isPrivate(t)) {
+ declared.add(current());
+ }
+ return super.visitClass(t, null);
+ }
+
+ @Override
public Void visitIdentifier(IdentifierTree t, Void __) {
used.add(current());
return super.visitIdentifier(t, null);
diff --git a/src/test/java/org/javacs/WarningsTest.java b/src/test/java/org/javacs/WarningsTest.java
index 09154c2..c4f9747 100644
--- a/src/test/java/org/javacs/WarningsTest.java
+++ b/src/test/java/org/javacs/WarningsTest.java
@@ -36,6 +36,7 @@ public class WarningsTest {
assertThat(errors, hasItem("unused(13)")); // int unusedLocalInLambda
assertThat(errors, hasItem("unused(16)")); // int unusedMethod() { ... }
assertThat(errors, hasItem("unused(22)")); // private Unused(int i) { }
+ assertThat(errors, hasItem("unused(24)")); // private class UnusedClass { }
assertThat(errors, not(hasItem("unused(6)"))); // test(int unusedParam)
assertThat(errors, not(hasItem("unused(12)"))); // unusedLambdaParam -> {};
assertThat(errors, not(hasItem("unused(20)"))); // private Unused() { }
diff --git a/src/test/test-project/workspace/src/org/javacs/warn/Unused.java b/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
index c8d0be3..09e87ce 100644
--- a/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
+++ b/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
@@ -20,4 +20,6 @@ class Unused {
private Unused() { }
private Unused(int i) { }
+
+ private class UnusedClass { }
} \ No newline at end of file