Skip to content

Commit

Permalink
moditect#7: Check references in 'extends' and 'implements' clause
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshartmann committed Jan 2, 2019
1 parent 7dc15f5 commit e792b1d
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.*)
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.moditect.deptective.plugintest.basic.barinnerinner;

public interface BarInnerInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.moditect.deptective.plugintest.basic.barinnersuper;

public class BarInnerSuperClass {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.moditect.deptective.plugintest.basic.barinter;

public interface BarInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.moditect.deptective.plugintest.basic.barsuper;

public class BarSuper {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,4 +69,12 @@ private void isAlsoAllowed() { }
static class InvalidFooGeneric<T extends BarGeneric> {}

static class InvalidFooImplementation extends FooContainer<BarGenType> {}

static interface InnerFoo extends BarInnerInterface {

}

static class InnerFooClass extends BarInnerSuperClass {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.moditect.deptective.plugintest.basic.foo;

public interface IFoo {

}

0 comments on commit e792b1d

Please sign in to comment.