diff --git a/javac-plugin/src/main/java/org/moditect/deptective/internal/DeptectiveTreeVisitor.java b/javac-plugin/src/main/java/org/moditect/deptective/internal/DeptectiveTreeVisitor.java index d9267c2..9e998e7 100644 --- a/javac-plugin/src/main/java/org/moditect/deptective/internal/DeptectiveTreeVisitor.java +++ b/javac-plugin/src/main/java/org/moditect/deptective/internal/DeptectiveTreeVisitor.java @@ -22,6 +22,7 @@ import org.moditect.deptective.internal.model.PackageDependencies; import com.sun.source.tree.AnnotationTree; +import com.sun.source.tree.ClassTree; import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodTree; @@ -72,6 +73,8 @@ public Void visitCompilationUnit(CompilationUnitTree tree, Void p) { return super.visitCompilationUnit(tree, p); } + + // @Override // public Void visitImport(ImportTree node, Void p) { // TODO: Deal with "on-demand-imports" (com.foo.*) @@ -86,6 +89,18 @@ public Void visitCompilationUnit(CompilationUnitTree tree, Void p) { // return super.visitImport(node, p); // } + @Override + public Void visitClass(ClassTree node, Void p) { + Tree extendsClause = node.getExtendsClause(); + if (extendsClause != null) { + checkPackageAccess(extendsClause, getQualifiedName(extendsClause)); + } + + node.getImplementsClause().forEach(implementsClause -> checkPackageAccess(implementsClause, getQualifiedName(implementsClause))); + + return super.visitClass(node, p); + } + @Override public Void visitVariable(VariableTree node, Void p) { com.sun.tools.javac.tree.JCTree jcTree = (com.sun.tools.javac.tree.JCTree)node; @@ -138,8 +153,6 @@ public Void visitNewClass(NewClassTree node, Void p) { return super.visitNewClass(node, p); } - - @Override public Void visitMethod(MethodTree node, Void p) { Tree returnType = node.getReturnType(); diff --git a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/BasicPluginTest.java b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/BasicPluginTest.java index 4445c86..07fbd40 100644 --- a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/BasicPluginTest.java +++ b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/BasicPluginTest.java @@ -50,6 +50,32 @@ private Compilation compile() { return compilation; } + @Test + public void shouldDetectInvalidSuperClass() { + Compilation compilation = compile(); + assertThat(compilation).failed(); + + assertThat(compilation).hadErrorContaining( + "package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barsuper"); + + // inner class + assertThat(compilation).hadErrorContaining( + "package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barinnersuper"); + } + + @Test + public void shouldDetectInvalidImplementedInterface() { + Compilation compilation = compile(); + assertThat(compilation).failed(); + + assertThat(compilation).hadErrorContaining( + "package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barinter"); + + // inner interface + assertThat(compilation).hadErrorContaining("package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barinnerinner"); + } + + @Test public void shouldDetectInvalidConstructorParameters() { Compilation compilation = compile(); diff --git a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinnerinner/BarInnerInterface.java b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinnerinner/BarInnerInterface.java new file mode 100644 index 0000000..c3fcad4 --- /dev/null +++ b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinnerinner/BarInnerInterface.java @@ -0,0 +1,5 @@ +package org.moditect.deptective.plugintest.basic.barinnerinner; + +public interface BarInnerInterface { + +} diff --git a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinnersuper/BarInnerSuperClass.java b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinnersuper/BarInnerSuperClass.java new file mode 100644 index 0000000..220a7f7 --- /dev/null +++ b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinnersuper/BarInnerSuperClass.java @@ -0,0 +1,5 @@ +package org.moditect.deptective.plugintest.basic.barinnersuper; + +public class BarInnerSuperClass { + +} diff --git a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinter/BarInterface.java b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinter/BarInterface.java new file mode 100644 index 0000000..08cf8e8 --- /dev/null +++ b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barinter/BarInterface.java @@ -0,0 +1,5 @@ +package org.moditect.deptective.plugintest.basic.barinter; + +public interface BarInterface { + +} diff --git a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barsuper/BarSuper.java b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barsuper/BarSuper.java new file mode 100644 index 0000000..5896559 --- /dev/null +++ b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/barsuper/BarSuper.java @@ -0,0 +1,5 @@ +package org.moditect.deptective.plugintest.basic.barsuper; + +public class BarSuper { + +} diff --git a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/foo/Foo.java b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/foo/Foo.java index 6c727ac..60c4ed6 100644 --- a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/foo/Foo.java +++ b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/foo/Foo.java @@ -25,16 +25,20 @@ import org.moditect.deptective.plugintest.basic.barfieldan.BarFieldAnnotation; import org.moditect.deptective.plugintest.basic.bargen.BarGeneric; import org.moditect.deptective.plugintest.basic.bargentype.BarGenType; +import org.moditect.deptective.plugintest.basic.barinnerinner.BarInnerInterface; +import org.moditect.deptective.plugintest.basic.barinnersuper.BarInnerSuperClass; +import org.moditect.deptective.plugintest.basic.barinter.BarInterface; import org.moditect.deptective.plugintest.basic.barlocalvar.BarLocalVar; import org.moditect.deptective.plugintest.basic.barloopvar.BarLoopVar; import org.moditect.deptective.plugintest.basic.barparameter.BarParameter; import org.moditect.deptective.plugintest.basic.barretval.BarRetVal; import org.moditect.deptective.plugintest.basic.barretvalgen.RetValGen; +import org.moditect.deptective.plugintest.basic.barsuper.BarSuper; import org.moditect.deptective.plugintest.basic.bartypearg.BarTypeArg; @FooAnnotation @BarClazzAnnotation -public class Foo { +public class Foo extends BarSuper implements BarInterface, /* allowed: */ IFoo { @BarFieldAnnotation private String s; @@ -65,4 +69,12 @@ private void isAlsoAllowed() { } static class InvalidFooGeneric {} static class InvalidFooImplementation extends FooContainer {} + + static interface InnerFoo extends BarInnerInterface { + + } + + static class InnerFooClass extends BarInnerSuperClass { + + } } diff --git a/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/foo/IFoo.java b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/foo/IFoo.java new file mode 100644 index 0000000..0dfa45f --- /dev/null +++ b/javac-plugin/src/test/java/org/moditect/deptective/plugintest/basic/foo/IFoo.java @@ -0,0 +1,5 @@ +package org.moditect.deptective.plugintest.basic.foo; + +public interface IFoo { + +}