Skip to content

Commit

Permalink
moditect#7 Check constructor references
Browse files Browse the repository at this point in the history
nilshartmann committed Jan 2, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9b036df commit a08c1cf
Showing 4 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
Original file line number Diff line number Diff line change
@@ -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() {
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
@@ -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;
@@ -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) {
@@ -49,6 +50,7 @@ public BarRetVal doSomething(BarParameter bar) {
for (BarLoopVar oneBar : new ArrayList<BarLoopVar>()) {
}


new BarCtorCall();

return null;

0 comments on commit a08c1cf

Please sign in to comment.