Skip to content

Commit

Permalink
moditect#7 Check constructor references
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshartmann committed Jan 2, 2019
1 parent ae87c55 commit 8d25a01
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.moditect.deptective.internal.model.PackageDependencies;

import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TypeParameterTree;
Expand Down Expand Up @@ -133,6 +133,14 @@ public Void visitAnnotation(AnnotationTree node, Void p) {
return super.visitAnnotation(node, p);
}



@Override
public Void visitNewClass(NewClassTree node, Void p) {
checkPackageAccess(node, getQualifiedName(node));
return super.visitNewClass(node, p);
}

protected String getQualifiedName(Tree tree) {
com.sun.tools.javac.tree.JCTree jcTree = (com.sun.tools.javac.tree.JCTree)tree;
Type type = jcTree.type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,25 @@ private Compilation compile() {
return compilation;
}

// @Test
// public void shouldDetectInvalidConstructorParameters() {
// Compilation compilation = compile();
// assertThat(compilation).failed();
//
// // TODO https://github.com/moditect/deptective/issues/7
// assertThat(compilation).hadErrorContaining(
// "package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barctorcall"
// );
// }
@Test
public void shouldDetectInvalidConstructorParameters() {
Compilation compilation = compile();
assertThat(compilation).failed();

assertThat(compilation).hadErrorContaining(
"package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barctorparam"
);
}

@Test
public void shouldDetectInvalidConstructorCalls() {
Compilation compilation = compile();
assertThat(compilation).failed();

assertThat(compilation).hadErrorContaining(
"package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barctorcall"
);
}

@Test
public void shouldDetectInvalidFieldReferences() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.moditect.deptective.plugintest.basic.barctorparam;

public class BarCtorParam {

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.moditect.deptective.plugintest.basic.barclazzan.BarClazzAnnotation;
import org.moditect.deptective.plugintest.basic.barctorcall.BarCtorCall;
import org.moditect.deptective.plugintest.basic.barctorparam.BarCtorParam;
import org.moditect.deptective.plugintest.basic.barfield.BarField;
import org.moditect.deptective.plugintest.basic.barfieldan.BarFieldAnnotation;
import org.moditect.deptective.plugintest.basic.bargen.BarGeneric;
Expand All @@ -38,7 +39,7 @@ public class Foo {
private String s;
private final BarField bar = new BarField();

public Foo(BarParameter bar) {
public Foo(BarCtorParam bar) {
}

public BarRetVal doSomething(BarParameter bar) {
Expand All @@ -48,6 +49,7 @@ public BarRetVal doSomething(BarParameter bar) {

for (BarLoopVar oneBar : new ArrayList<BarLoopVar>()) {
}


new BarCtorCall();

Expand Down

0 comments on commit 8d25a01

Please sign in to comment.