Java: Handle 'throws' clause in the inspection "Non-accessible type is exposed" (IDEA-162768)

This commit is contained in:
Pavel Dolgov
2016-11-10 16:50:58 +03:00
parent dedb86f59e
commit 5f52d0539a
2 changed files with 30 additions and 3 deletions
@@ -86,6 +86,12 @@ public class Java9NonAccessibleTypeExposedInspection extends BaseJavaLocalInspec
for (PsiParameter parameter : method.getParameterList().getParameters()) {
checkType(parameter.getType(), parameter.getTypeElement());
}
for (PsiJavaCodeReferenceElement referenceElement : method.getThrowsList().getReferenceElements()) {
PsiElement resolved = referenceElement.resolve();
if (resolved instanceof PsiClass) {
checkType((PsiClass)resolved, referenceElement);
}
}
}
}
@@ -121,9 +127,13 @@ public class Java9NonAccessibleTypeExposedInspection extends BaseJavaLocalInspec
private void checkType(@Nullable PsiType type, @Nullable PsiTypeElement typeElement) {
if (typeElement != null) {
PsiClass psiClass = PsiUtil.resolveClassInType(type);
if (psiClass != null && isInModuleSource(psiClass) && !isModulePublicApi(psiClass)) {
myHolder.registerProblem(typeElement, CLASS_IS_NOT_EXPORTED);
}
checkType(psiClass, typeElement);
}
}
private void checkType(PsiClass psiClass, @NotNull PsiElement typeElement) {
if (psiClass != null && isInModuleSource(psiClass) && !isModulePublicApi(psiClass)) {
myHolder.registerProblem(typeElement, CLASS_IS_NOT_EXPORTED);
}
}
@@ -197,6 +197,23 @@ public class Highlighted {
""")
}
fun testThrows() {
add("apiPkg", "PublicException", "public class PublicException extends Exception {}")
add("apiPkg", "PackageLocalException", "class PackageLocalException extends Exception {}")
add("otherPkg", "OtherException", "public class OtherException extends Exception {}")
add("implPkg", "NotExportedException", "public class NotExportedException extends Exception {}")
highlight("""package apiPkg;
import otherPkg.*;
import implPkg.*;
public class Highlighted {
public void throwsPublic() throws PublicException {}
public void throwsPackageLocal() throws <warning descr="The class is not exported from the module">PackageLocalException</warning> {}
public void throwsOther() throws OtherException {}
public void throwsNotExported() throws <warning descr="The class is not exported from the module">NotExportedException</warning> {}
}
""")
}
fun testPublicAnnotation() {
add("apiPkg", "MyAnnotation", "public @interface MyAnnotation {}")
highlight("""package apiPkg;