From c323980d20e1221cbaa795dd30d0b873c197d88d Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Fri, 18 Nov 2016 17:52:12 +0300 Subject: [PATCH] Java: Handle classes in the inspection "Non-accessible type is exposed", added tests for initializer blocks (IDEA-162768) --- ...va9NonAccessibleTypeExposedInspection.java | 119 +++--------------- .../Java9NonAccessibleTypeExposedTest.kt | 8 ++ 2 files changed, 22 insertions(+), 105 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9NonAccessibleTypeExposedInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9NonAccessibleTypeExposedInspection.java index fba3789276ae..2ea2eff41b85 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9NonAccessibleTypeExposedInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9NonAccessibleTypeExposedInspection.java @@ -27,7 +27,6 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.psi.util.PsiUtil; import com.intellij.util.containers.ContainerUtil; import gnu.trove.THashSet; import org.jetbrains.annotations.Contract; @@ -83,76 +82,21 @@ public class Java9NonAccessibleTypeExposedInspection extends BaseJavaLocalInspec public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { super.visitReferenceElement(reference); PsiElement parent = reference.getParent(); - PsiElement grandParent = null; - if (parent instanceof PsiTypeElement) { - grandParent = PsiTreeUtil.skipParentsOfType(reference, PsiTypeElement.class, - PsiParameter.class, PsiParameterList.class, - PsiReferenceParameterList.class, PsiJavaCodeReferenceElement.class); - } - else if (parent instanceof PsiReferenceList) { - grandParent = PsiTreeUtil.skipParentsOfType(reference, PsiReferenceList.class, - PsiTypeParameter.class, PsiTypeParameterList.class); - } - if ((grandParent instanceof PsiField || grandParent instanceof PsiMethod) && isModulePublicApi((PsiMember)grandParent)) { - PsiElement resolved = reference.resolve(); - if (resolved instanceof PsiClass) { - checkType((PsiClass)resolved, reference); - } - } - } - - @Override - public void visitClass(PsiClass aClass) { - super.visitClass(aClass); - if (isModulePublicApi(aClass)) { - checkTypeParameters(aClass.getTypeParameterList()); - } - } - - private void checkType(@Nullable PsiType type, @Nullable PsiTypeElement typeElement) { - if (typeElement != null) { - if (type instanceof PsiWildcardType) { - type = ((PsiWildcardType)type).getBound(); - PsiElement lastChild = typeElement.getLastChild(); - if (lastChild instanceof PsiTypeElement) { - typeElement = (PsiTypeElement)lastChild; - } - } - PsiClass psiClass = PsiUtil.resolveClassInType(type); - checkType(psiClass, typeElement); - if (type instanceof PsiClassType && !(psiClass instanceof PsiTypeParameter)) { - PsiJavaCodeReferenceElement referenceElement = typeElement.getInnermostComponentReferenceElement(); - if (referenceElement != null) { - checkTypeParameters(referenceElement.getParameterList()); - } - } - } - } - - private void checkType(@Nullable PsiClass psiClass, @NotNull PsiElement typeElement) { - if (psiClass != null && !(psiClass instanceof PsiTypeParameter) && isInModuleSource(psiClass) && !isModulePublicApi(psiClass)) { - registerProblem(typeElement); - } - } - - private void checkTypeParameters(@Nullable PsiReferenceParameterList parameterList) { - if (parameterList != null) { - PsiTypeElement[] typeParameterElements = parameterList.getTypeParameterElements(); - for (PsiTypeElement typeParameterElement : typeParameterElements) { - checkType(typeParameterElement.getType(), typeParameterElement); - } - } - } - - private void checkTypeParameters(@Nullable PsiTypeParameterList parameterList) { - if (parameterList != null) { - for (PsiTypeParameter typeParameter : parameterList.getTypeParameters()) { - for (PsiJavaCodeReferenceElement referenceElement : typeParameter.getExtendsList().getReferenceElements()) { - PsiElement resolved = referenceElement.resolve(); - if (resolved instanceof PsiClass) { - checkType((PsiClass)resolved, referenceElement); + if (parent instanceof PsiTypeElement || parent instanceof PsiReferenceList) { + PsiElement grandParent = PsiTreeUtil.skipParentsOfType(reference, PsiTypeElement.class, PsiReferenceList.class, + PsiParameter.class, PsiParameterList.class, + PsiReferenceParameterList.class, PsiJavaCodeReferenceElement.class, + PsiTypeParameter.class, PsiTypeParameterList.class); + if ((grandParent instanceof PsiField || + grandParent instanceof PsiMethod || + grandParent instanceof PsiClass) && + isModulePublicApi((PsiMember)grandParent)) { + PsiElement resolved = reference.resolve(); + if (resolved instanceof PsiClass && !(resolved instanceof PsiTypeParameter)) { + PsiClass psiClass = (PsiClass)resolved; + if (!isModulePublicApi(psiClass) && isInModuleSource(psiClass)) { + registerProblem(reference); } - checkTypeParameters(referenceElement.getParameterList()); } } } @@ -175,41 +119,6 @@ public class Java9NonAccessibleTypeExposedInspection extends BaseJavaLocalInspec return false; } - @Contract("null -> false") - private boolean isModulePublicApi(@Nullable PsiAnnotationOwner owner) { - if (owner instanceof PsiModifierList) { - PsiElement parent = ((PsiModifierList)owner).getParent(); - if (parent instanceof PsiMember) { // class or field or method - return isModulePublicApi((PsiMember)parent); - } - if (parent instanceof PsiParameter) { // method parameter - PsiElement declarationScope = ((PsiParameter)parent).getDeclarationScope(); - if (declarationScope instanceof PsiMethod) { - return isModulePublicApi((PsiMethod)declarationScope); - } - } - } - else if (owner instanceof PsiTypeElement) { // type argument (aka type_use) - PsiElement grandParent = PsiTreeUtil.skipParentsOfType(((PsiTypeElement)owner), - PsiParameter.class, PsiParameterList.class, PsiTypeElement.class, - PsiReferenceList.class, PsiReferenceParameterList.class, - PsiJavaCodeReferenceElement.class); - if (grandParent instanceof PsiMember) { - return isModulePublicApi((PsiMember)grandParent); - } - } - else if (owner instanceof PsiTypeParameter) { // type parameter declaration - PsiElement parent = ((PsiTypeParameter)owner).getParent(); - if (parent instanceof PsiTypeParameterList) { - PsiElement grandParent = parent.getParent(); - if (grandParent instanceof PsiMember) { - return isModulePublicApi((PsiMember)grandParent); - } - } - } - return false; - } - private boolean isInModuleSource(@NotNull PsiClass psiClass) { PsiFile psiFile = psiClass.getContainingFile(); if (psiFile != null) { diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt index 51894b72967d..e67a44c64b2c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt @@ -58,6 +58,7 @@ public class Highlighted { highlight("""package apiPkg; import m2Pkg.Exported; public class Highlighted { + static { Exported tmp = new Exported(); System.out.println(tmp);} public Exported myVar; protected Highlighted() {} public Highlighted(Exported var) { @@ -71,6 +72,7 @@ public class Highlighted { fun testPackageLocalExposed() { highlight("""package apiPkg; public class Highlighted { + static { PackageLocal tmp = new PackageLocal(); System.out.println(tmp);} public PackageLocal myVar; protected Highlighted() {} public Highlighted(PackageLocal var) { @@ -85,6 +87,7 @@ public class Highlighted { fun testPackageLocalEncapsulated() { highlight("""package apiPkg; public class Highlighted { + static { PackageLocal tmp = new PackageLocal(); System.out.println(tmp);} private PackageLocal myVar; private Highlighted() {} Highlighted(PackageLocal var) { @@ -99,6 +102,7 @@ public class Highlighted { fun testPackageLocalUsedLocally() { highlight("""package apiPkg; class Highlighted { + static { PackageLocal tmp = new PackageLocal(); System.out.println(tmp);} public PackageLocal myVar; protected Highlighted() {} public Highlighted(PackageLocal var) { @@ -113,6 +117,7 @@ class Highlighted { fun testPublicApi() { highlight("""package apiPkg; public class Highlighted { + static { PublicApi tmp = new PublicApi(); System.out.println(tmp);} public PublicApi myVar; protected Highlighted() {} public Highlighted(PublicApi var) { @@ -128,6 +133,7 @@ public class Highlighted { highlight("""package apiPkg; import otherPkg.PublicOther; public class Highlighted { + static { PublicOther tmp = new PublicOther(); System.out.println(tmp);} public PublicOther myVar; protected Highlighted() {} public Highlighted(PublicOther var) { @@ -143,6 +149,7 @@ public class Highlighted { highlight("""package apiPkg; public class Highlighted { public class PublicNested {} + { PublicNested tmp = new PublicNested(); System.out.println(tmp);} public PublicNested myVar; protected Highlighted() {} public Highlighted(PublicNested var) { @@ -158,6 +165,7 @@ public class Highlighted { highlight("""package apiPkg; public class Highlighted { class PackageLocalNested {} + { PackageLocalNested tmp = new PackageLocalNested(); System.out.println(tmp);} public PackageLocalNested myVar; protected Highlighted() {} public Highlighted(PackageLocalNested var) {