Java: Handle generic parameters in the inspection "Non-accessible type is exposed" (IDEA-162768)

This commit is contained in:
Pavel Dolgov
2016-11-11 15:22:36 +03:00
parent 020d2e48ad
commit fbb404203f
2 changed files with 140 additions and 24 deletions
@@ -31,6 +31,7 @@ class Java9NonAccessibleTypeExposedTest : LightJava9ModulesCodeInsightFixtureTes
super.setUp()
myFixture.enableInspections(Java9NonAccessibleTypeExposedInspection())
addFile("module-info.java", "module MAIN { exports apiPkg; exports otherPkg; requires M2; }", MAIN)
addFile("module-info.java", "module M2 { exports m2Pkg; }", M2)
add("apiPkg", "PublicApi", "public class PublicApi {}")
add("apiPkg", "PackageLocal", "class PackageLocal {}")
add("otherPkg", "PublicOther", "public class PublicOther {}")
@@ -40,13 +41,18 @@ class Java9NonAccessibleTypeExposedTest : LightJava9ModulesCodeInsightFixtureTes
highlight("""package apiPkg;
public class Highlighted {
public int i;
protected int getInt() {return 1;}
public int getInt() {return i;}
public void setInt(int n) {i=n;}
protected Long l;
protected Long getLong() {return l;}
protected void setLong(Long n) {l=n;}
public void run() {}
}""")
}
fun testImported() {
addFile("module-info.java", "module M2 { exports m2Pkg; }", M2)
fun testExported() {
addFile("m2Pkg/Exported.java", "package m2Pkg; public class Exported {}", M2)
highlight("""package apiPkg;
@@ -271,17 +277,79 @@ public class Highlighted {
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) @interface PackageLocalAnnotation {}
public class C1<@PublicAnnotation T> {
public List<@PublicAnnotation String> text;
public void foo(Set<@PublicAnnotation String> s) {}
protected <@PublicAnnotation X> void bar(X x) {}
protected List<@PublicAnnotation T> baz() { return new ArrayList<@PublicAnnotation T>();}
protected Set<@PublicAnnotation T> baz() {return new HashSet<@PublicAnnotation T>();}
public List<@PublicAnnotation String> text() {return new ArrayList<@PublicAnnotation String>();}
}
public class C2<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> T> {
public List<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> String> text;
public void foo(Set<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> String> s) {}
protected <@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> X> void bar(X x) {}
protected List<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> T> baz() {
return new ArrayList<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> T>();
protected Set<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> T> baz() {
return new HashSet<@PackageLocalAnnotation T>();
}
public List<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> String> text() {
return new ArrayList<@PackageLocalAnnotation String>();
}
}
public interface I1 extends List<@PublicAnnotation Highlighted> {}
public interface I2 extends List<@<warning descr="The class is not exported from the module">PackageLocalAnnotation</warning> Highlighted> {}
}
""")
}
fun testGenericPublic() {
add("apiPkg", "MyInterface", "public interface MyInterface {}")
add("apiPkg", "MyClass", "public class MyClass implements MyInterface {}")
highlight("""package apiPkg;
import java.util.*;
public class Highlighted<T extends MyInterface> {
protected Set<T> get1() { return new HashSet<>();}
public Set<MyClass> get2() { return new HashSet<MyClass>();}
protected Set<? extends MyClass> get3() { return new HashSet<>();}
public <X extends Object&MyInterface> Set<X> get4() { return new HashSet<>();}
public Map<String, Set<MyInterface>> get5() {return new HashMap<>();}
public void copy1(Set<MyInterface> s) {}
public void copy2(Set<? super MyClass> s) {}
public static class Nested1<T extends MyClass&Iterable<MyInterface>> {
public Iterator<MyInterface> iterator() {return null;}
}
public static class Nested2<T extends MyInterface&AutoCloseable> {
public void close(){}
}
public interface Nested3<X extends Iterable<MyInterface>> {}
public interface Nested4<X extends Iterable<? extends MyInterface>> {}
}
""")
}
fun testGenericNotExported() {
add("implPkg", "MyInterface", "public interface MyInterface {}")
add("implPkg", "MyClass", "public class MyClass implements MyInterface {}")
highlight("""package apiPkg;
import java.util.*;
import implPkg.*;
public class Highlighted<T extends <warning descr="The class is not exported from the module">MyInterface</warning>> {
protected Set<T> get1() { return new HashSet<>();}
public Set<<warning descr="The class is not exported from the module">MyClass</warning>> get2() { return new HashSet<MyClass>();}
protected Set<? extends <warning descr="The class is not exported from the module">MyClass</warning>> get3() { return new HashSet<>();}
public <X extends Object&<warning descr="The class is not exported from the module">MyInterface</warning>> Set<X> get4() { return new HashSet<>();}
public Map<String, Set<<warning descr="The class is not exported from the module">MyInterface</warning>>> get5() {return new HashMap<>();}
public void copy1(Set<<warning descr="The class is not exported from the module">MyInterface</warning>> s) {}
public void copy2(Set<? super <warning descr="The class is not exported from the module">MyClass</warning>> s) {}
public static class Nested1<T extends <warning descr="The class is not exported from the module">MyClass</warning>&
Iterable<<warning descr="The class is not exported from the module">MyInterface</warning>>> {
public Iterator<<warning descr="The class is not exported from the module">MyInterface</warning>> iterator() {return null;}
}
public static class Nested2<T extends <warning descr="The class is not exported from the module">MyInterface</warning>&AutoCloseable> {
public void close() {}
}
public interface Nested3<X extends Iterable<<warning descr="The class is not exported from the module">MyInterface</warning>>> {}
public interface Nested4<X extends Iterable<? extends <warning descr="The class is not exported from the module">MyInterface</warning>>> {}
}
""")
}