mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
service to detect java version;
highlight access to private class members according to java version used (IDEA-83045)
This commit is contained in:
+2
-1
@@ -27,6 +27,7 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.IndexNotReadyException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersion;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersionUtil;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
@@ -508,7 +509,7 @@ public class GenericsHighlightUtil {
|
||||
final PsiType retErasure2 = TypeConversionUtil.erasure(superMethod.getReturnType());
|
||||
|
||||
boolean differentReturnTypeErasure = !Comparing.equal(retErasure1, retErasure2);
|
||||
if (checkEqualsSuper && JavaSdkVersion.isAtLeast(checkMethod, JavaSdkVersion.JDK_1_7)) {
|
||||
if (checkEqualsSuper && JavaSdkVersionUtil.isAtLeast(checkMethod, JavaSdkVersion.JDK_1_7)) {
|
||||
if (retErasure1 != null && retErasure2 != null) {
|
||||
differentReturnTypeErasure = !TypeConversionUtil.isAssignable(retErasure1, retErasure2);
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2000-2012 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.openapi.projectRoots;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 3/28/12
|
||||
*/
|
||||
public class JavaVersionServiceImpl extends JavaVersionService {
|
||||
@Override
|
||||
public boolean isAtLeast(PsiElement element, JavaSdkVersion version) {
|
||||
return JavaSdkVersionUtil.isAtLeast(element, version);
|
||||
}
|
||||
}
|
||||
-15
@@ -15,11 +15,7 @@
|
||||
*/
|
||||
package com.intellij.openapi.projectRoots;
|
||||
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtil;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -75,15 +71,4 @@ public enum JavaSdkVersion {
|
||||
);
|
||||
}
|
||||
|
||||
public static boolean isAtLeast(PsiElement element, JavaSdkVersion minVersion) {
|
||||
final Module module = ModuleUtil.findModuleForPsiElement(element);
|
||||
if (module != null) {
|
||||
final Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
|
||||
if (sdk != null && sdk.getSdkType() instanceof JavaSdk) {
|
||||
final JavaSdkVersion version = JavaSdk.getInstance().getVersion(sdk);
|
||||
return version != null && version.isAtLeast(minVersion);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2000-2012 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @author max
|
||||
*/
|
||||
package com.intellij.openapi.projectRoots;
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
|
||||
public class JavaVersionService {
|
||||
public static JavaVersionService getInstance() {
|
||||
return ServiceManager.getService(JavaVersionService.class);
|
||||
}
|
||||
|
||||
public boolean isAtLeast(PsiElement element, JavaSdkVersion version) {
|
||||
return PsiUtil.getLanguageLevel(element).isAtLeast(version.getMaxLanguageLevel());
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.lang.java.JavaParserDefinition;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersion;
|
||||
import com.intellij.openapi.projectRoots.JavaVersionService;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.openapi.roots.PackageIndex;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
@@ -86,6 +88,7 @@ public class JavaCoreEnvironment extends CoreEnvironment {
|
||||
|
||||
myApplication.registerService(EmptySubstitutor.class, new EmptySubstitutorImpl());
|
||||
myApplication.registerService(JavaDirectoryService.class, new CoreJavaDirectoryService());
|
||||
myApplication.registerService(JavaVersionService.class, new JavaVersionService());
|
||||
}
|
||||
|
||||
public void addToClasspath(File path) {
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
*/
|
||||
package com.intellij.psi.impl.source.resolve;
|
||||
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersion;
|
||||
import com.intellij.openapi.projectRoots.JavaVersionService;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiImplUtil;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
@@ -160,15 +162,20 @@ public class JavaResolveUtil {
|
||||
|
||||
private static PsiClass getTopLevelClass(@NotNull PsiElement place, PsiClass memberClass) {
|
||||
PsiClass lastClass = null;
|
||||
Boolean isAtLeast17 = null;
|
||||
for (PsiElement placeParent = place; placeParent != null; placeParent = placeParent.getContext()) {
|
||||
if (placeParent instanceof PsiClass &&
|
||||
!(placeParent instanceof PsiAnonymousClass) &&
|
||||
!(placeParent instanceof PsiTypeParameter)) {
|
||||
PsiClass aClass = (PsiClass)placeParent;
|
||||
|
||||
if (memberClass != null && aClass.isInheritor(memberClass, true)) return aClass;
|
||||
|
||||
lastClass = aClass;
|
||||
if (placeParent instanceof PsiClass && !(placeParent instanceof PsiAnonymousClass)) {
|
||||
final boolean isTypeParameter = placeParent instanceof PsiTypeParameter;
|
||||
if (isTypeParameter && isAtLeast17 == null) {
|
||||
isAtLeast17 = JavaVersionService.getInstance().isAtLeast(place, JavaSdkVersion.JDK_1_7);
|
||||
}
|
||||
if (!isTypeParameter || (isAtLeast17 != null && isAtLeast17)) {
|
||||
PsiClass aClass = (PsiClass)placeParent;
|
||||
|
||||
if (memberClass != null && aClass.isInheritor(memberClass, true)) return aClass;
|
||||
|
||||
lastClass = aClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class A {
|
||||
private int value = 1;
|
||||
|
||||
static class B<T extends A> {
|
||||
void print(T t) {
|
||||
System.out.println(t.<error descr="'value' has private access in 'A'">value</error>);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class A {
|
||||
private int value = 1;
|
||||
|
||||
static class B<T extends A> {
|
||||
void print(T t) {
|
||||
System.out.println(t.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-1
@@ -32,7 +32,16 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
LanguageLevel level = getTestName(false).contains("Level6") ? LanguageLevel.JDK_1_6 : LanguageLevel.JDK_1_5;
|
||||
LanguageLevel level;
|
||||
final String testName = getTestName(false);
|
||||
if (testName.contains("Level17")) {
|
||||
level = LanguageLevel.JDK_1_7;
|
||||
} else if (testName.contains("Level6")) {
|
||||
level = LanguageLevel.JDK_1_6;
|
||||
}
|
||||
else {
|
||||
level = LanguageLevel.JDK_1_5;
|
||||
}
|
||||
LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(level);
|
||||
}
|
||||
|
||||
@@ -107,6 +116,8 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
|
||||
public void testPrivateInnerClassRef() throws Exception { doTest(false); }
|
||||
public void testWideningCastToTypeParam() throws Exception { doTest(false); }
|
||||
public void testCapturedWildcardAssignments() throws Exception { doTest(false);}
|
||||
public void testTypeParameterBoundVisibility() throws Exception { doTest(false);}
|
||||
public void testTypeParameterBoundVisibilityJdk14() throws Exception { doTest(false);}
|
||||
|
||||
public void testJavaUtilCollections_NoVerify() throws Exception {
|
||||
PsiClass collectionsClass = getJavaFacade().findClass("java.util.Collections", GlobalSearchScope.moduleWithLibrariesScope(getModule()));
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2000-2012 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.openapi.projectRoots;
|
||||
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtil;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 3/28/12
|
||||
*/
|
||||
public class JavaSdkVersionUtil {
|
||||
public static boolean isAtLeast(PsiElement element, JavaSdkVersion minVersion) {
|
||||
final Module module = ModuleUtil.findModuleForPsiElement(element);
|
||||
if (module != null) {
|
||||
final Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
|
||||
if (sdk != null && sdk.getSdkType() instanceof JavaSdk) {
|
||||
final JavaSdkVersion version = JavaSdk.getInstance().getVersion(sdk);
|
||||
return version != null && version.isAtLeast(minVersion);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -297,6 +297,9 @@
|
||||
|
||||
<applicationService serviceInterface="com.intellij.psi.JavaDirectoryService"
|
||||
serviceImplementation="com.intellij.psi.impl.file.JavaDirectoryServiceImpl"/>
|
||||
|
||||
<applicationService serviceInterface="com.intellij.openapi.projectRoots.JavaVersionService"
|
||||
serviceImplementation="com.intellij.openapi.projectRoots.JavaVersionServiceImpl"/>
|
||||
|
||||
<applicationService serviceInterface="com.intellij.refactoring.JavaRefactoringActionHandlerFactory"
|
||||
serviceImplementation="com.intellij.refactoring.openapi.impl.JavaRefactoringActionHandlerFactoryImpl"/>
|
||||
|
||||
Reference in New Issue
Block a user