Skip to content

Commit

Permalink
#7: Check references in return types
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshartmann authored and gunnarmorling committed Jan 2, 2019
1 parent 82da8e4 commit 9d7fe7c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.Tree;
Expand Down Expand Up @@ -137,6 +138,17 @@ public Void visitNewClass(NewClassTree node, Void p) {
return super.visitNewClass(node, p);
}



@Override
public Void visitMethod(MethodTree node, Void p) {
Tree returnType = node.getReturnType();
if (returnType != null) {
checkPackageAccess(returnType, getQualifiedName(returnType));
}
return super.visitMethod(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 @@ -115,17 +115,20 @@ public void shouldDetectInvalidAnnotationReferences() {
"package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barfieldan");
}

// @Test
// public void shouldDetectInvalidReturnValueReferences() {
// 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.barretval"
// );
//
// }
@Test
public void shouldDetectInvalidReturnValueReferences() {
Compilation compilation = compile();
assertThat(compilation).failed();

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

// Invalid Reference in Type Parameter
assertThat(compilation).hadErrorContaining(
"package org.moditect.deptective.plugintest.basic.foo does not read org.moditect.deptective.plugintest.basic.barretvalgen"
);
}

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

public class RetValGen {

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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.bartypearg.BarTypeArg;

@FooAnnotation
Expand Down Expand Up @@ -56,6 +57,11 @@ public BarRetVal doSomething(BarParameter bar) {
return null;
}

private boolean isAllowed() { return true; }
private void isAlsoAllowed() { }

private List<RetValGen> isNotAllowed() { return null; }

static class InvalidFooGeneric<T extends BarGeneric> {}

static class InvalidFooImplementation extends FooContainer<BarGenType> {}
Expand Down

0 comments on commit 9d7fe7c

Please sign in to comment.