fixed bugs with anonymous classes and record classes
This commit is contained in:
@@ -39,6 +39,7 @@ test {
|
|||||||
sourceSets["test"].resources.srcDirs
|
sourceSets["test"].resources.srcDirs
|
||||||
systemProperty("test.compilation.classpath",
|
systemProperty("test.compilation.classpath",
|
||||||
String.join(File.pathSeparator, testCompilationClassPath.collect { it.toString() }))
|
String.join(File.pathSeparator, testCompilationClassPath.collect { it.toString() }))
|
||||||
|
jvmArgs('--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+14
-4
@@ -21,6 +21,8 @@ import com.sun.source.util.TaskListener;
|
|||||||
import com.sun.source.util.TreePath;
|
import com.sun.source.util.TreePath;
|
||||||
import com.sun.source.util.TreePathScanner;
|
import com.sun.source.util.TreePathScanner;
|
||||||
import com.sun.source.util.Trees;
|
import com.sun.source.util.Trees;
|
||||||
|
import com.sun.tools.javac.tree.JCTree;
|
||||||
|
import com.sun.tools.javac.tree.TreeInfo;
|
||||||
|
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
@@ -42,6 +44,8 @@ import java.util.Optional;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static com.sun.tools.javac.code.Flags.RECORD;
|
||||||
|
|
||||||
public class FinalGuardPlugin implements Plugin {
|
public class FinalGuardPlugin implements Plugin {
|
||||||
public static final String DEFAULT_LEVEL_KEY = "default.level";
|
public static final String DEFAULT_LEVEL_KEY = "default.level";
|
||||||
public static final String EXCLUDE_KEY = "exclude";
|
public static final String EXCLUDE_KEY = "exclude";
|
||||||
@@ -192,11 +196,18 @@ public class FinalGuardPlugin implements Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitMethod(MethodTree node, Void p) {
|
public Void visitMethod(MethodTree node, Void p) {
|
||||||
variableInfoMap.clear();
|
if (node.getName().contentEquals("<init>")) {
|
||||||
reassignedVariables.clear();
|
TreePath classPath = getCurrentPath().getParentPath();
|
||||||
|
if (classPath != null && classPath.getParentPath() != null
|
||||||
|
&& classPath.getParentPath().getLeaf().getKind() == Tree.Kind.NEW_CLASS) {
|
||||||
|
return null; // skip implicit constructor of anonymous class
|
||||||
|
}
|
||||||
|
}
|
||||||
super.visitMethod(node, p);
|
super.visitMethod(node, p);
|
||||||
// Check for variables that could be final
|
// Check for variables that could be final
|
||||||
checkForFinalCandidates();
|
checkForFinalCandidates();
|
||||||
|
variableInfoMap.clear();
|
||||||
|
reassignedVariables.clear();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,8 +233,7 @@ public class FinalGuardPlugin implements Plugin {
|
|||||||
} else {
|
} else {
|
||||||
type = VariableType.METHOD_PARAM;
|
type = VariableType.METHOD_PARAM;
|
||||||
if (isJava17OrHigher && ((MethodTree) parent).getName().contentEquals("<init>")) {
|
if (isJava17OrHigher && ((MethodTree) parent).getName().contentEquals("<init>")) {
|
||||||
final TreePath grandParentPath = parentPath.getParentPath();
|
if(TreeInfo.isCanonicalConstructor((JCTree) parent)) {
|
||||||
if (grandParentPath.getLeaf().getKind() == Tree.Kind.RECORD) {
|
|
||||||
return super.visitVariable(node, p);
|
return super.visitVariable(node, p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-1
@@ -154,7 +154,24 @@ public class PluginTest {
|
|||||||
)),
|
)),
|
||||||
Arguments.of(prefix + "TestCase12.java", Collections.emptyList()),
|
Arguments.of(prefix + "TestCase12.java", Collections.emptyList()),
|
||||||
Arguments.of(prefix + "TestCase13.java", Arrays.asList(ABSTRACT_METHOD_PARAM.getMessage("x"), ABSTRACT_METHOD_PARAM.getMessage("y"))),
|
Arguments.of(prefix + "TestCase13.java", Arrays.asList(ABSTRACT_METHOD_PARAM.getMessage("x"), ABSTRACT_METHOD_PARAM.getMessage("y"))),
|
||||||
Arguments.of(prefix + "TestCase14.java", Arrays.asList(ABSTRACT_METHOD_PARAM.getMessage("x"), ABSTRACT_METHOD_PARAM.getMessage("y")))
|
Arguments.of(prefix + "TestCase14.java", Arrays.asList(ABSTRACT_METHOD_PARAM.getMessage("x"), ABSTRACT_METHOD_PARAM.getMessage("y"))),
|
||||||
|
Arguments.of(prefix + "TestCase15.java",
|
||||||
|
Arrays.asList(
|
||||||
|
LOCAL_VAR.getMessage("a"),
|
||||||
|
METHOD_PARAM.getMessage("source"))),
|
||||||
|
Arguments.of(prefix + "TestCase16.java",
|
||||||
|
Arrays.asList(
|
||||||
|
METHOD_PARAM.getMessage("c"),
|
||||||
|
LOCAL_VAR.getMessage("d"),
|
||||||
|
METHOD_PARAM.getMessage("e"),
|
||||||
|
LOCAL_VAR.getMessage("f")
|
||||||
|
)
|
||||||
|
),
|
||||||
|
Arguments.of(prefix + "TestCase17.java",
|
||||||
|
Arrays.asList(
|
||||||
|
METHOD_PARAM.getMessage("a")
|
||||||
|
)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
import java.io.FilterInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class TestCase15 {
|
||||||
|
public static void foo() {
|
||||||
|
final InputStream source = null;
|
||||||
|
final InputStream is = new FilterInputStream(source) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read() throws IOException {
|
||||||
|
int a = 5;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Bar extends FilterInputStream {
|
||||||
|
|
||||||
|
public Bar(InputStream source) {
|
||||||
|
super(source);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
import java.io.FilterInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public record TestCase16(double a, double b) {
|
||||||
|
TestCase16(double c) {
|
||||||
|
this(c, 2.0);
|
||||||
|
|
||||||
|
int d = 42;
|
||||||
|
System.out.println(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
double foo(double e) {
|
||||||
|
int f = 42;
|
||||||
|
System.out.println(f);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
import java.io.FilterInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public record TestCase17(String s) {
|
||||||
|
TestCase17(double a) {
|
||||||
|
this("");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -76,6 +76,7 @@ public class FinalGuardPlugin implements Plugin<Project> {
|
|||||||
appendOption(xpluginArg, EXCLUDE_KEY, excludedPrefix);
|
appendOption(xpluginArg, EXCLUDE_KEY, excludedPrefix);
|
||||||
}
|
}
|
||||||
options.getCompilerArgs().add(xpluginArg.toString());
|
options.getCompilerArgs().add(xpluginArg.toString());
|
||||||
|
options.getAllCompilerArgs().add("-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user