From 6cc60e2543c7c87ca6a1c48c34cac00c818b40b6 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 21 Jan 2015 12:14:13 +0100 Subject: [PATCH] workaround for overload resolution for methods from Object when project has multiple jdks (IDEA-135422) --- .../JavaMethodsConflictResolver.java | 10 +- ...sFirstRunnableSurroundStatement_after.java | 1 + .../multipleJdks/java7/p/GetClass.java | 6 ++ .../multipleJdks/java8/p/GetClass.java | 6 ++ .../multipleJdks/libs/GetClass.jar | Bin 0 -> 297 bytes .../ClsGenericsHighlightingTest.java | 30 +++--- .../MultipleJdksHighlightingTest.java | 101 ++++++++++++++++++ 7 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/multipleJdks/java7/p/GetClass.java create mode 100644 java/java-tests/testData/codeInsight/multipleJdks/java8/p/GetClass.java create mode 100644 java/java-tests/testData/codeInsight/multipleJdks/libs/GetClass.jar create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/MultipleJdksHighlightingTest.java diff --git a/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaMethodsConflictResolver.java b/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaMethodsConflictResolver.java index 9127c8bcaa0a..099678b627b2 100644 --- a/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaMethodsConflictResolver.java +++ b/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaMethodsConflictResolver.java @@ -615,12 +615,12 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{ return Specifics.SECOND; } } + else if (MethodSignatureUtil.areErasedParametersEqual(method1.getSignature(PsiSubstitutor.EMPTY), method2.getSignature(PsiSubstitutor.EMPTY)) && + MethodSignatureUtil.isSubsignature(method2.getSignature(info2.getSubstitutor(false)), method1.getSignature(info1.getSubstitutor(false)))) { + return Specifics.FIRST; + } else if (class1.isInheritor(class2, true) || class2.isInterface()) { - if (MethodSignatureUtil.areErasedParametersEqual(method1.getSignature(PsiSubstitutor.EMPTY), method2.getSignature(PsiSubstitutor.EMPTY)) && - MethodSignatureUtil.isSubsignature(method2.getSignature(info2.getSubstitutor(false)), method1.getSignature(info1.getSubstitutor(false)))) { - return Specifics.FIRST; - } - else if (method1.hasModifierProperty(PsiModifier.STATIC) && method2.hasModifierProperty(PsiModifier.STATIC) && boxingHappened[0] == 0) { + if (method1.hasModifierProperty(PsiModifier.STATIC) && method2.hasModifierProperty(PsiModifier.STATIC) && boxingHappened[0] == 0) { return Specifics.FIRST; } } diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/CommentAsFirstRunnableSurroundStatement_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/CommentAsFirstRunnableSurroundStatement_after.java index 30ae98a335fc..3d5b4b8f12c4 100644 --- a/java/java-tests/testData/codeInsight/generation/surroundWith/java/CommentAsFirstRunnableSurroundStatement_after.java +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/CommentAsFirstRunnableSurroundStatement_after.java @@ -1,6 +1,7 @@ class Test { void foo() { Runnable runnable = new Runnable() { + @Override public void run() { // This is comment" int i = 1; diff --git a/java/java-tests/testData/codeInsight/multipleJdks/java7/p/GetClass.java b/java/java-tests/testData/codeInsight/multipleJdks/java7/p/GetClass.java new file mode 100644 index 000000000000..7b1ddba72c8e --- /dev/null +++ b/java/java-tests/testData/codeInsight/multipleJdks/java7/p/GetClass.java @@ -0,0 +1,6 @@ +package p; +abstract class B extends A implements p.P { + { + final Class aClass = getClass(); + } +} diff --git a/java/java-tests/testData/codeInsight/multipleJdks/java8/p/GetClass.java b/java/java-tests/testData/codeInsight/multipleJdks/java8/p/GetClass.java new file mode 100644 index 000000000000..b413b7f662b3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/multipleJdks/java8/p/GetClass.java @@ -0,0 +1,6 @@ +package p; +public abstract class A implements p.P { + { + final Class aClass = getClass(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/multipleJdks/libs/GetClass.jar b/java/java-tests/testData/codeInsight/multipleJdks/libs/GetClass.jar new file mode 100644 index 0000000000000000000000000000000000000000..95fbb924848b5914f932167863dae4eb33f1c8e3 GIT binary patch literal 297 zcmWIWW@h1HVBlb2uvu*4#()Hv7+4qz^#7e=UUrdrfWAoh&QnO>FFBWNKK|pd!r74YZS;W7D1W*}gy() { @Override public void consume(ModifiableRootModel model) { - LibraryTable libraryTable = model.getModuleLibraryTable(); - Library library = libraryTable.createLibrary("test"); - - Library.ModifiableModel libraryModel = library.getModifiableModel(); - for (String annotationsDir : libraryPath) { - String path = myFixture.getTestDataPath() + "/libs/" + annotationsDir; - VirtualFile libJarLocal = LocalFileSystem.getInstance().findFileByPath(path); - assertNotNull(libJarLocal); - VirtualFile jarRoot = JarFileSystem.getInstance().getJarRootForLocalFile(libJarLocal); - assertNotNull(jarRoot); - libraryModel.addRoot(jarRoot, OrderRootType.CLASSES); - } - libraryModel.commit(); + commitLibraryModel(model, myFixture.getTestDataPath(), libraryPath); String contentUrl = VfsUtilCore.pathToUrl(myFixture.getTempDirPath()); model.addContentEntry(contentUrl).addSourceFolder(contentUrl, false); } }); } + + protected static void commitLibraryModel(ModifiableRootModel model, String testDataPath, @NotNull String... libraryPath) { + LibraryTable libraryTable = model.getModuleLibraryTable(); + Library library = libraryTable.createLibrary("test"); + + Library.ModifiableModel libraryModel = library.getModifiableModel(); + for (String annotationsDir : libraryPath) { + String path = testDataPath + "/libs/" + annotationsDir; + VirtualFile libJarLocal = LocalFileSystem.getInstance().findFileByPath(path); + assertNotNull(libJarLocal); + VirtualFile jarRoot = JarFileSystem.getInstance().getJarRootForLocalFile(libJarLocal); + assertNotNull(jarRoot); + libraryModel.addRoot(jarRoot, OrderRootType.CLASSES); + } + libraryModel.commit(); + } } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/MultipleJdksHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/MultipleJdksHighlightingTest.java new file mode 100644 index 000000000000..9d3ec5986cd9 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/MultipleJdksHighlightingTest.java @@ -0,0 +1,101 @@ +/* + * Copyright 2000-2015 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.codeInsight; + +import com.intellij.openapi.application.ex.PathManagerEx; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.ModuleRootModificationUtil; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.testFramework.IdeaTestCase; +import com.intellij.testFramework.IdeaTestUtil; +import com.intellij.testFramework.UsefulTestCase; +import com.intellij.testFramework.builders.JavaModuleFixtureBuilder; +import com.intellij.testFramework.fixtures.*; +import com.intellij.util.Consumer; + +public class MultipleJdksHighlightingTest extends UsefulTestCase { + + private CodeInsightTestFixture myFixture; + private Module myJava7Module; + private Module myJava8Module; + + @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") + public MultipleJdksHighlightingTest() { + IdeaTestCase.initPlatformPrefix(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + myFixture.tearDown(); + myFixture = null; + myJava7Module = null; + myJava8Module = null; + } + + @Override + public void setUp() throws Exception { + super.setUp(); + TestFixtureBuilder projectBuilder = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName()); + myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(projectBuilder.getFixture()); + myFixture.setTestDataPath(PathManagerEx.getTestDataPath() + "/codeInsight/multipleJdks"); + final JavaModuleFixtureBuilder[] builders = new JavaModuleFixtureBuilder[2]; + builders[0] = projectBuilder.addModule(JavaModuleFixtureBuilder.class); + builders[1] = projectBuilder.addModule(JavaModuleFixtureBuilder.class); + myFixture.setUp(); + myJava7Module = builders[0].getFixture().getModule(); + myJava8Module = builders[1].getFixture().getModule(); + ModuleRootModificationUtil.updateModel(myJava7Module, new Consumer() { + @Override + public void consume(ModifiableRootModel model) { + model.addModuleOrderEntry(myJava8Module); + model.setSdk(IdeaTestUtil.getMockJdk17()); + String contentUrl = VfsUtilCore.pathToUrl(myFixture.getTempDirPath()) + "/java7"; + model.addContentEntry(contentUrl).addSourceFolder(contentUrl, false); + } + }); + + ModuleRootModificationUtil.updateModel(myJava8Module, new Consumer() { + @Override + public void consume(ModifiableRootModel model) { + model.setSdk(IdeaTestUtil.getMockJdk18()); + String contentUrl = VfsUtilCore.pathToUrl(myFixture.getTempDirPath()) + "/java8"; + model.addContentEntry(contentUrl).addSourceFolder(contentUrl, false); + } + }); + } + + public void testGetClass() throws Exception { + doTest(); + } + + protected void doTest() { + final String name = getTestName(false); + for (Module module : new Module[] {myJava7Module, myJava8Module}) { + ModuleRootModificationUtil.updateModel(module, new Consumer() { + @Override + public void consume(ModifiableRootModel model) { + ClsGenericsHighlightingTest.commitLibraryModel(model, myFixture.getTestDataPath(), name + ".jar"); + } + }); + } + + myFixture.configureByFiles("java7/p/" + name + ".java", "java8/p/" + name + ".java"); + myFixture.checkHighlighting(); + } +}