From 7decc41a7c6d961f07a18c9f6ced03fb2fa3b9ad Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Fri, 20 Jan 2017 20:27:03 +0300 Subject: [PATCH] Java: Merged two inspections ClassEscapesItsScopeInspection and Java9NonAccessibleTypeExposedInspection (IDEA-166535) --- ...va9NonAccessibleTypeExposedInspection.java | 137 ------------------ .../Java9NonAccessibleTypeExposedTest.kt | 86 ++++++----- .../src/messages/InspectionsBundle.properties | 2 - .../siyeh/InspectionGadgetsBundle.properties | 6 +- .../ClassEscapesItsScopeInspection.java | 133 +++++++++++++++-- .../ClassEscapesItsScope.html | 17 ++- .../ClassEscapesItsScope.java | 10 +- .../GenericParameterEscapesItsScope.java | 14 +- .../ClassEscapesItsScopeInspectionTest.java | 5 +- .../Java9NonAccessibleTypeExposed.html | 10 -- resources/src/META-INF/IdeaPlugin.xml | 4 - 11 files changed, 196 insertions(+), 228 deletions(-) delete mode 100644 java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9NonAccessibleTypeExposedInspection.java delete mode 100644 resources-en/src/inspectionDescriptions/Java9NonAccessibleTypeExposed.html 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 deleted file mode 100644 index 2ea2eff41b85..000000000000 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9NonAccessibleTypeExposedInspection.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.codeInspection.java19modules; - -import com.intellij.codeInsight.daemon.impl.analysis.JavaModuleGraphUtil; -import com.intellij.codeInspection.BaseJavaLocalInspectionTool; -import com.intellij.codeInspection.InspectionsBundle; -import com.intellij.codeInspection.ProblemsHolder; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.roots.ModuleFileIndex; -import com.intellij.openapi.roots.ModuleRootManager; -import com.intellij.openapi.roots.ProjectFileIndex; -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.util.containers.ContainerUtil; -import gnu.trove.THashSet; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Set; - -/** - * @author Pavel.Dolgov - */ -public class Java9NonAccessibleTypeExposedInspection extends BaseJavaLocalInspectionTool { - - @NotNull - @Override - public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { - PsiFile file = holder.getFile(); - if (file instanceof PsiJavaFile) { - PsiJavaFile javaFile = (PsiJavaFile)file; - if (javaFile.getLanguageLevel().isAtLeast(LanguageLevel.JDK_1_9)) { - PsiJavaModule psiModule = JavaModuleGraphUtil.findDescriptorByElement(file); - if (psiModule != null) { - VirtualFile vFile = file.getVirtualFile(); - if (vFile != null) { - Module module = ProjectFileIndex.SERVICE.getInstance(holder.getProject()).getModuleForFile(vFile); - if (module != null) { - Set exportedPackageNames = new THashSet<>(ContainerUtil.mapNotNull(psiModule.getExports(), p -> p.getPackageName())); - if (exportedPackageNames.contains(javaFile.getPackageName())) { - return new NonAccessibleTypeExposedVisitor(holder, module, exportedPackageNames); - } - } - } - } - } - } - return PsiElementVisitor.EMPTY_VISITOR; - } - - private static class NonAccessibleTypeExposedVisitor extends JavaElementVisitor { - private final ProblemsHolder myHolder; - private final ModuleFileIndex myModuleFileIndex; - private final Set myExportedPackageNames; - - public NonAccessibleTypeExposedVisitor(@NotNull ProblemsHolder holder, - @NotNull Module module, - @NotNull Set exportedPackageNames) { - myHolder = holder; - myModuleFileIndex = ModuleRootManager.getInstance(module).getFileIndex(); - myExportedPackageNames = exportedPackageNames; - } - - @Override - public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { - super.visitReferenceElement(reference); - PsiElement parent = reference.getParent(); - 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); - } - } - } - } - } - - @Contract("null -> false") - private boolean isModulePublicApi(@Nullable PsiMember member) { - if (member != null && - !(member instanceof PsiTypeParameter) && - (member.hasModifierProperty(PsiModifier.PUBLIC) || member.hasModifierProperty(PsiModifier.PROTECTED))) { - PsiElement parent = member.getParent(); - if (parent instanceof PsiClass) { - return isModulePublicApi((PsiClass)parent); - } - if (parent instanceof PsiJavaFile) { - String packageName = ((PsiJavaFile)parent).getPackageName(); - return myExportedPackageNames.contains(packageName); - } - } - return false; - } - - private boolean isInModuleSource(@NotNull PsiClass psiClass) { - PsiFile psiFile = psiClass.getContainingFile(); - if (psiFile != null) { - VirtualFile vFile = psiFile.getVirtualFile(); - if (vFile != null) { - return myModuleFileIndex.isInSourceContent(vFile); - } - } - return false; - } - - private void registerProblem(PsiElement element) { - myHolder.registerProblem(element, InspectionsBundle.message("inspection.non.accessible.type.exposed.name")); - } - } -} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt index e67a44c64b2c..f884904afbc8 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInspection/Java9NonAccessibleTypeExposedTest.kt @@ -15,10 +15,10 @@ */ package com.intellij.codeInspection -import com.intellij.codeInspection.java19modules.Java9NonAccessibleTypeExposedInspection import com.intellij.testFramework.fixtures.LightJava9ModulesCodeInsightFixtureTestCase import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor.M2 import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor.MAIN +import com.siyeh.ig.visibility.ClassEscapesItsScopeInspection import org.intellij.lang.annotations.Language import org.jetbrains.annotations.NonNls import org.jetbrains.annotations.NotNull @@ -29,7 +29,7 @@ import org.jetbrains.annotations.NotNull class Java9NonAccessibleTypeExposedTest : LightJava9ModulesCodeInsightFixtureTestCase() { override fun setUp() { super.setUp() - myFixture.enableInspections(Java9NonAccessibleTypeExposedInspection()) + myFixture.enableInspections(ClassEscapesItsScopeInspection()) 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 {}") @@ -73,13 +73,13 @@ public class Highlighted { highlight("""package apiPkg; public class Highlighted { static { PackageLocal tmp = new PackageLocal(); System.out.println(tmp);} - public PackageLocal myVar; + public PackageLocal myVar; protected Highlighted() {} - public Highlighted(PackageLocal var) { + public Highlighted(PackageLocal var) { PackageLocal tmp = new PackageLocal(); myVar = var!= null ? var : tmp; } - public PackageLocal getVar() {return myVar;} - protected void setVar(PackageLocal var) {myVar = var;} + public PackageLocal getVar() {return myVar;} + protected void setVar(PackageLocal var) {myVar = var;} } """) } @@ -96,6 +96,14 @@ public class Highlighted { PackageLocal getVar() {return myVar;} private void setVar(PackageLocal var) {myVar = var;} } +""") + } + fun testPackageLocalEncapsulated2() { + highlight("""package apiPkg; +public class Highlighted { + Highlighted(PackageLocal var) { + } +} """) } @@ -166,13 +174,13 @@ public class Highlighted { public class Highlighted { class PackageLocalNested {} { PackageLocalNested tmp = new PackageLocalNested(); System.out.println(tmp);} - public PackageLocalNested myVar; + public PackageLocalNested myVar; protected Highlighted() {} - public Highlighted(PackageLocalNested var) { + public Highlighted(PackageLocalNested var) { PackageLocalNested tmp = new PackageLocalNested(); myVar = var!= null ? var : tmp; } - public PackageLocalNested getVar() {return myVar;} - protected void setVar(PackageLocalNested var) {myVar = var;} + public PackageLocalNested getVar() {return myVar;} + protected void setVar(PackageLocalNested var) {myVar = var;} } """) } @@ -180,9 +188,9 @@ public class Highlighted { fun testPackageLocalInInterface() { highlight("""package apiPkg; public interface Highlighted { - PackageLocal myVar = new PackageLocal(); - PackageLocal getVar(); - void setVar(PackageLocal var); + PackageLocal myVar = new PackageLocal(); + PackageLocal getVar(); + void setVar(PackageLocal var); } """) } @@ -202,11 +210,11 @@ public interface Highlighted { highlight("""package apiPkg; import implPkg.NotExported; public class Highlighted { - public NotExported myVar; + public NotExported myVar; protected Highlighted() {} - public Highlighted(NotExported var) {setVar(var);} - public NotExported getVar() {return myVar;} - protected void setVar(NotExported var) {myVar = var;} + public Highlighted(NotExported var) {setVar(var);} + public NotExported getVar() {return myVar;} + protected void setVar(NotExported var) {myVar = var;} } """) } @@ -221,8 +229,8 @@ public class Highlighted { import apiPkg.PublicOuter.PackageLocal; public class Highlighted { private PackageLocal.DoubleNested myVar; - public PackageLocal.DoubleNested getVar() {return myVar;} - protected void setVar(PackageLocal.DoubleNested var) {myVar = var;} + public PackageLocal.DoubleNested getVar() {return myVar;} + protected void setVar(PackageLocal.DoubleNested var) {myVar = var;} } """) } @@ -247,11 +255,11 @@ public class Highlighted { import implPkg.NotExported; import java.util.*; public class Highlighted { - public NotExported[] myVar; - protected Highlighted(List<NotExported[]> list) {Iterator it = list.iterator(); myVar = it.next();} - public Highlighted(NotExported[] var) {myVar = var;} - public NotExported[] getVar() {return myVar;} - protected void setVar(NotExported[][] var) {myVar = var[0];} + public NotExported[] myVar; + protected Highlighted(List<NotExported[]> list) {Iterator it = list.iterator(); myVar = it.next();} + public Highlighted(NotExported[] var) {myVar = var;} + public NotExported[] getVar() {return myVar;} + protected void setVar(NotExported[][] var) {myVar = var[0];} }""") } @@ -265,9 +273,9 @@ import otherPkg.*; import implPkg.*; public class Highlighted { public void throwsPublic() throws PublicException {} - public void throwsPackageLocal() throws PackageLocalException {} + public void throwsPackageLocal() throws PackageLocalException {} public void throwsOther() throws OtherException {} - public void throwsNotExported() throws NotExportedException {} + public void throwsNotExported() throws NotExportedException {} } """) } @@ -304,24 +312,24 @@ public class Highlighted { highlight("""package apiPkg; import java.util.*; import implPkg.*; -public class HighlightedMyInterface> { +public class Highlighted { protected Set get1() { return new HashSet<>();} - public Set<MyClass> get2() { return new HashSet();} - protected SetMyClass> get3() { return new HashSet<>();} - public MyInterface> Set get4() { return new HashSet<>();} - public MapMyInterface>> get5() {return new HashMap<>();} - public void copy1(Set<MyInterface> s) {} - public void copy2(SetMyClass> s) {} + public Set<MyClass> get2() { return new HashSet();} + protected SetMyClass> get3() { return new HashSet<>();} + public MyInterface> Set get4() { return new HashSet<>();} + public MapMyInterface>> get5() {return new HashMap<>();} + public void copy1(Set<MyInterface> s) {} + public void copy2(SetMyClass> s) {} - public static class Nested1MyClass& - Iterable<MyInterface>> { - public Iterator<MyInterface> iterator() {return null;} + public static class Nested1> { + public Iterator<MyInterface> iterator() {return null;} } - public static class Nested2MyInterface&AutoCloseable> { + public static class Nested2 { public void close() {} } - public interface Nested3MyInterface>> {} - public interface Nested4MyInterface>> {} + public interface Nested3> {} + public interface Nested4> {} } """) } diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties index 21ed071c92d9..60085e86357f 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -467,8 +467,6 @@ inspection.compiler.javac.quirks.anno.array.comma.fix=Remove trailing comma inspection.compiler.javac.quirks.qualifier.type.args.problem=Generics in qualifier reference may cause compilation error in some Javac versions (e.g. JDK 5 and JDK 6). inspection.compiler.javac.quirks.qualifier.type.args.fix=Remove generic parameter -inspection.non.accessible.type.exposed.name=The class is not exported from the module - inspection.redundant.requires.statement.name=Redundant 'requires' statement in module-info inspection.redundant.requires.statement.description=Redundant statement ''requires {0}'' inspection.redundant.requires.statement.fix.family=Delete redundant 'requires' statement diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index eaaad5406655..fc40339d26a4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -337,8 +337,10 @@ use.stringtokenizer.display.name=Use of StringTokenizer use.stringtokenizer.problem.descriptor=#ref in an internationalized context #loc time.tostring.call.display.name=Call to 'Time.toString()' time.tostring.call.problem.descriptor=Time.#ref() in an internationalized context #loc -class.escapes.defined.scope.display.name=Class escapes defined scope -class.escapes.defined.scope.problem.descriptor=Class #ref is made visible outside its defined scope #loc +class.escapes.defined.scope.display.name=Non-accessible class is exposed +class.escapes.defined.scope.problem.descriptor=Class #ref is exposed outside its defined scope #loc +class.escapes.defined.scope.java9.modules.option=Check only the Java 9 module visibility rules +class.escapes.defined.scope.java9.modules.descriptor=Class is not exported from the module field.name.hides.in.superclass.display.name=Field name hides field in superclass field.name.hides.in.superclass.problem.descriptor=Field #ref hides field in superclass #loc field.name.hides.in.superclass.ignore.option=Ignore superclass fields not visible from subclass diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/ClassEscapesItsScopeInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/ClassEscapesItsScopeInspection.java index 04e047379599..8001a82afaf3 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/ClassEscapesItsScopeInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/ClassEscapesItsScopeInspection.java @@ -15,15 +15,35 @@ */ package com.siyeh.ig.visibility; +import com.intellij.codeInsight.daemon.impl.analysis.JavaModuleGraphUtil; +import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.ModuleFileIndex; +import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.roots.ProjectFileIndex; +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.util.containers.ContainerUtil; import com.siyeh.InspectionGadgetsBundle; -import com.siyeh.ig.BaseInspection; -import com.siyeh.ig.BaseInspectionVisitor; +import gnu.trove.THashSet; import org.intellij.lang.annotations.Pattern; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public class ClassEscapesItsScopeInspection extends BaseInspection { +import javax.swing.*; +import java.util.Set; + +public class ClassEscapesItsScopeInspection extends BaseJavaBatchLocalInspectionTool { + + /** + * @noinspection PublicField + */ + public boolean onlyJava9Modules = true; @Pattern(VALID_ID_PATTERN) @Override @@ -38,18 +58,46 @@ public class ClassEscapesItsScopeInspection extends BaseInspection { return InspectionGadgetsBundle.message("class.escapes.defined.scope.display.name"); } + @Nullable @Override + public JComponent createOptionsPanel() { + return new SingleCheckboxOptionsPanel( + InspectionGadgetsBundle.message("class.escapes.defined.scope.java9.modules.option"), this, "onlyJava9Modules"); + } + @NotNull - public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message("class.escapes.defined.scope.problem.descriptor"); - } - @Override - public BaseInspectionVisitor buildVisitor() { - return new ClassEscapesItsScopeVisitor(); + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { + PsiFile file = holder.getFile(); + if (file instanceof PsiJavaFile) { + PsiJavaFile javaFile = (PsiJavaFile)file; + if (javaFile.getLanguageLevel().isAtLeast(LanguageLevel.JDK_1_9)) { + PsiJavaModule psiModule = JavaModuleGraphUtil.findDescriptorByElement(file); + if (psiModule != null) { + VirtualFile vFile = file.getVirtualFile(); + if (vFile != null) { + Module module = ProjectFileIndex.SERVICE.getInstance(holder.getProject()).getModuleForFile(vFile); + if (module != null) { + Set exportedPackageNames = + new THashSet<>(ContainerUtil.mapNotNull(psiModule.getExports(), PsiExportsStatement::getPackageName)); + if (exportedPackageNames.contains(javaFile.getPackageName())) { + return new Java9NonAccessibleTypeExposedVisitor(holder, module, exportedPackageNames, onlyJava9Modules); + } + } + } + } + } + } + return onlyJava9Modules ? PsiElementVisitor.EMPTY_VISITOR : new ClassEscapesItsScopeVisitor(holder); } - private static class ClassEscapesItsScopeVisitor extends BaseInspectionVisitor { + private static class ClassEscapesItsScopeVisitor extends JavaElementVisitor { + final ProblemsHolder myHolder; + + public ClassEscapesItsScopeVisitor(ProblemsHolder holder) { + myHolder = holder; + } + @Override public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { super.visitReferenceElement(reference); @@ -65,15 +113,19 @@ public class ClassEscapesItsScopeInspection extends BaseInspection { PsiElement resolved = reference.resolve(); if (resolved instanceof PsiClass && !(resolved instanceof PsiTypeParameter)) { PsiClass psiClass = (PsiClass)resolved; - if (isLessRestrictiveScope(member, psiClass)) { - registerError(reference); - } + checkVisibility(member, psiClass, reference); } } } } } + void checkVisibility(PsiMember member, PsiClass psiClass, PsiJavaCodeReferenceElement reference) { + if (isLessRestrictiveScope(member, psiClass)) { + myHolder.registerProblem(reference, InspectionGadgetsBundle.message("class.escapes.defined.scope.problem.descriptor")); + } + } + private static boolean isPrivate(@NotNull PsiMember member) { if (member.hasModifierProperty(PsiModifier.PRIVATE)) { return true; @@ -113,4 +165,59 @@ public class ClassEscapesItsScopeInspection extends BaseInspection { } } } + + private static class Java9NonAccessibleTypeExposedVisitor extends ClassEscapesItsScopeVisitor { + + private final ModuleFileIndex myModuleFileIndex; + private final Set myExportedPackageNames; + private boolean myOnlyJava9Modules; + + public Java9NonAccessibleTypeExposedVisitor(@NotNull ProblemsHolder holder, + @NotNull Module module, + @NotNull Set exportedPackageNames, + boolean onlyJava9Modules) { + super(holder); + myModuleFileIndex = ModuleRootManager.getInstance(module).getFileIndex(); + myExportedPackageNames = exportedPackageNames; + myOnlyJava9Modules = onlyJava9Modules; + } + + @Override + void checkVisibility(PsiMember member, PsiClass psiClass, PsiJavaCodeReferenceElement reference) { + if (!myOnlyJava9Modules) { + super.checkVisibility(member, psiClass, reference); + } + if (isModulePublicApi(member) && !isModulePublicApi(psiClass) && isInModuleSource(psiClass)) { + myHolder.registerProblem(reference, InspectionGadgetsBundle.message("class.escapes.defined.scope.java9.modules.descriptor")); + } + } + + @Contract("null -> false") + private boolean isModulePublicApi(@Nullable PsiMember member) { + if (member != null && + !(member instanceof PsiTypeParameter) && + (member.hasModifierProperty(PsiModifier.PUBLIC) || member.hasModifierProperty(PsiModifier.PROTECTED))) { + PsiElement parent = member.getParent(); + if (parent instanceof PsiClass) { + return isModulePublicApi((PsiClass)parent); + } + if (parent instanceof PsiJavaFile) { + String packageName = ((PsiJavaFile)parent).getPackageName(); + return myExportedPackageNames.contains(packageName); + } + } + return false; + } + + private boolean isInModuleSource(@NotNull PsiClass psiClass) { + PsiFile psiFile = psiClass.getContainingFile(); + if (psiFile != null) { + VirtualFile vFile = psiFile.getVirtualFile(); + if (vFile != null) { + return myModuleFileIndex.isInSourceContent(vFile); + } + } + return false; + } + } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ClassEscapesItsScope.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassEscapesItsScope.html index fa759f75a79b..9dd4e57bc64a 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ClassEscapesItsScope.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassEscapesItsScope.html @@ -1,12 +1,15 @@ -Reports any references to classes which allow the class name to -be used outside the class's stated scope. For instance, this inspection would report -a public method which returns a private inner class, or a protected field whose -type is a package-visible class. While legal Java, such references can be very -confusing, and make reuse difficult. - +Reports references to classes in field types and method signatures where a field or a method exposes a class +with more restricted visibility than the field or the method itself.

- + Examples are a public method which returns a private inner class, or a protected field + whose type is a package-visible class.
+ While legal Java, such fields and methods aren't useful outside of the visibility scope of the classes used in the field or the method. +

+ In addition to that, in Java 9 a module may hide some of its classes by not exporting their packages.
+ If the public API of a class in an exported package references a class from non-exported package, + such API isn't useful outside of the module. +
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/ClassEscapesItsScope.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/ClassEscapesItsScope.java index b389ffbc07b9..c2fb066c3f1b 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/ClassEscapesItsScope.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/ClassEscapesItsScope.java @@ -2,15 +2,15 @@ public class ClassEscapesItsScope { public T t; - public A giveMeA() { + public A giveMeA() { return new A(); } - void printA(A a) { + void printA(A a) { System.out.println(a); } private class A {} - void throwsE() throws E { + void throwsE() throws E { throw new E(); } private static class E extends Exception {} @@ -28,8 +28,8 @@ class BarInside { } class InnerClass implements F { - public Bar bar; - public Bar apply(String s) { + public Bar bar; + public Bar apply(String s) { throw new UnsupportedOperationException(); } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/GenericParameterEscapesItsScope.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/GenericParameterEscapesItsScope.java index ac43bfcf4fc9..99cddd817db8 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/GenericParameterEscapesItsScope.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/class_escapes_its_scope/GenericParameterEscapesItsScope.java @@ -16,26 +16,26 @@ import java.util.*; public class GenericParameterEscapesItsScope { public List as; - public List<B> bs; + public List<B> bs; - public List<B> getBs() { return bs; } - public void setBs(List<B> bs) { this.bs = bs; } + public List<B> getBs() { return bs; } + public void setBs(List<B> bs) { this.bs = bs; } public List getAs() { return as; } public void setAs(List as) { this.as = as; } public class Inner extends B implements Getter, Setter { - public B b; + public B b; @Override - public B get() { + public B get() { return b; } @Override - public void set(B b) { + public void set(B b) { this.b = b; } } - public Data<B> foo() { + public Data<B> foo() { class Local extends B implements Data { public B b; @Override diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/visibility/ClassEscapesItsScopeInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/visibility/ClassEscapesItsScopeInspectionTest.java index 65d8775382b2..56551611ccd9 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/visibility/ClassEscapesItsScopeInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/visibility/ClassEscapesItsScopeInspectionTest.java @@ -17,7 +17,6 @@ package com.siyeh.ig.visibility; import com.intellij.codeInspection.InspectionProfileEntry; import com.siyeh.ig.LightInspectionTestCase; -import junit.framework.TestCase; import org.jetbrains.annotations.Nullable; /** @@ -32,6 +31,8 @@ public class ClassEscapesItsScopeInspectionTest extends LightInspectionTestCase @Nullable @Override protected InspectionProfileEntry getInspection() { - return new ClassEscapesItsScopeInspection(); + ClassEscapesItsScopeInspection inspection = new ClassEscapesItsScopeInspection(); + inspection.onlyJava9Modules = false; + return inspection; } } \ No newline at end of file diff --git a/resources-en/src/inspectionDescriptions/Java9NonAccessibleTypeExposed.html b/resources-en/src/inspectionDescriptions/Java9NonAccessibleTypeExposed.html deleted file mode 100644 index 4b120ac6356f..000000000000 --- a/resources-en/src/inspectionDescriptions/Java9NonAccessibleTypeExposed.html +++ /dev/null @@ -1,10 +0,0 @@ - - -A module in Java 9 may hide some of its classes by not exporting their packages. -

-If the public API of a class in an exported package references a class from non-exported package, -such API isn't useful outside of the module. -

-New in 2017.1 - - \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 45ebdc460981..1f2e38594f54 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -901,10 +901,6 @@ groupKey="group.names.language.level.specific.issues.and.migration.aids" enabledByDefault="true" level="WARNING" implementationClass="com.intellij.codeInspection.OptionalIsPresentInspection" displayName="Replace Optional.isPresent() checks with functional-style expressions"/> -