diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2021-04-18 05:45:28 +0200 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2021-04-18 05:45:28 +0200 |
commit | ba9ffce89841e6fdbec7c2d234c3a70a2db7f80d (patch) | |
tree | 9b2040710997bb1270a9c03b0a49b9f830621b86 /test/core/src | |
parent | 01702f70d063b7c7685f8c4de94e6d1718047390 (diff) | |
parent | 6782aceeb0bc082357bbd3ff20b166c379afb380 (diff) | |
download | lombok-ba9ffce89841e6fdbec7c2d234c3a70a2db7f80d.tar.gz lombok-ba9ffce89841e6fdbec7c2d234c3a70a2db7f80d.tar.bz2 lombok-ba9ffce89841e6fdbec7c2d234c3a70a2db7f80d.zip |
Merge branch 'enter-types-methods-fields' of git://github.com/Rawi01/lombok into Rawi01-enter-types-methods-fields
Diffstat (limited to 'test/core/src')
-rw-r--r-- | test/core/src/lombok/RunTestsViaDelombok.java | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/test/core/src/lombok/RunTestsViaDelombok.java b/test/core/src/lombok/RunTestsViaDelombok.java index 46eb54a0..c10ef4d3 100644 --- a/test/core/src/lombok/RunTestsViaDelombok.java +++ b/test/core/src/lombok/RunTestsViaDelombok.java @@ -35,19 +35,21 @@ import java.util.Deque; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.Stack; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; import com.sun.source.util.TreePath; import com.sun.source.util.Trees; import com.sun.tools.javac.code.Flags; +import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCAssign; import com.sun.tools.javac.tree.JCTree.JCClassDecl; @@ -57,11 +59,12 @@ import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCModifiers; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.tree.JCTree.TypeBoundKind; +import com.sun.tools.javac.tree.TreeScanner; import lombok.delombok.Delombok; import lombok.javac.CapturingDiagnosticListener; -import lombok.javac.Javac; import lombok.javac.CapturingDiagnosticListener.CompilerMessage; +import lombok.javac.Javac; public class RunTestsViaDelombok extends AbstractRunTests { private Delombok delombok = new Delombok(); @@ -78,6 +81,7 @@ public class RunTestsViaDelombok extends AbstractRunTests { delombok.setDiagnosticsListener(new CapturingDiagnosticListener(file, messages)); if (checkPositions) delombok.addAdditionalAnnotationProcessor(new ValidatePositionProcessor(version)); + delombok.addAdditionalAnnotationProcessor(new ValidateTypesProcessor()); delombok.addFile(file.getAbsoluteFile().getParentFile(), file.getName()); delombok.setSourcepath(file.getAbsoluteFile().getParent()); @@ -178,6 +182,57 @@ public class RunTestsViaDelombok extends AbstractRunTests { } } + public static class ValidateTypesProcessor extends TreeProcessor { + @Override void processCompilationUnit(final JCCompilationUnit unit) { + final Stack<JCTree> parents = new Stack<JCTree>(); + parents.add(unit); + + unit.accept(new TreeScanner() { + private JCTree parent; + @Override public void scan(JCTree tree) { + parent = parents.peek(); + + parents.push(tree); + super.scan(tree); + parents.pop(); + } + + @Override public void visitClassDef(JCClassDecl tree) { + // Skip anonymous or local classes, they have no symbol + if (!(parent instanceof JCClassDecl || parent instanceof JCCompilationUnit)) return; + + validateSymbol(tree, tree.sym); + super.visitClassDef(tree); + }; + + @Override public void visitMethodDef(JCMethodDecl tree) { + validateSymbol(tree, tree.sym); + super.visitMethodDef(tree); + } + + @Override public void visitVarDef(JCVariableDecl tree) { + // Skip non-field variables + if (!(parent instanceof JCClassDecl)) return; + + validateSymbol(tree, tree.sym); + super.visitVarDef(tree); + } + + private void validateSymbol(JCTree tree, Symbol sym) { + if (sym == null) { + fail("Missing symbol for " + tree); + } + // Skip top level classes + if (sym.owner.getKind() == ElementKind.PACKAGE) return; + + if (!sym.owner.getEnclosedElements().contains(sym)) { + fail(tree + " not added to parent"); + } + } + }); + } + } + public static abstract class TreeProcessor extends AbstractProcessor { private Trees trees; @Override public synchronized void init(ProcessingEnvironment processingEnv) { |