mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ensure no conflicts on move in module with another jdk (IDEA-139356)
This commit is contained in:
@@ -17,6 +17,7 @@ package com.intellij.refactoring.util;
|
||||
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtil;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
@@ -246,7 +247,7 @@ public class RefactoringConflictsUtil {
|
||||
if (scope instanceof PsiPackage) return;
|
||||
}
|
||||
|
||||
final Module targetModule = ModuleUtil.findModuleForFile(vFile, project);
|
||||
final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
|
||||
if (targetModule == null) return;
|
||||
final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
|
||||
final HashSet<PsiElement> reported = new HashSet<PsiElement>();
|
||||
@@ -258,11 +259,11 @@ public class RefactoringConflictsUtil {
|
||||
if (resolved != null &&
|
||||
!reported.contains(resolved) &&
|
||||
!CommonRefactoringUtil.isAncestor(resolved, scopes) &&
|
||||
!PsiSearchScopeUtil.isInScope(resolveScope, resolved) &&
|
||||
!(resolved instanceof LightElement)) {
|
||||
!(resolved instanceof LightElement) &&
|
||||
!haveElementInScope(resolved)) {
|
||||
if (resolved instanceof PsiMethod) {
|
||||
for (PsiMethod superMethod : ((PsiMethod)resolved).findDeepestSuperMethods()) {
|
||||
if (PsiSearchScopeUtil.isInScope (resolveScope, superMethod)) return;
|
||||
if (haveElementInScope(superMethod)) return;
|
||||
}
|
||||
}
|
||||
final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
|
||||
@@ -274,6 +275,39 @@ public class RefactoringConflictsUtil {
|
||||
reported.add(resolved);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean haveElementInScope(PsiElement resolved) {
|
||||
if (PsiSearchScopeUtil.isInScope(resolveScope, resolved)){
|
||||
return true;
|
||||
}
|
||||
if (!resolved.getManager().isInProject(resolved)) {
|
||||
if (resolved instanceof PsiMember) {
|
||||
final PsiClass containingClass = ((PsiMember)resolved).getContainingClass();
|
||||
if (containingClass != null) {
|
||||
final String fqn = containingClass.getQualifiedName();
|
||||
if (fqn != null) {
|
||||
final PsiClass classFromTarget = JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope);
|
||||
if (classFromTarget != null) {
|
||||
if (resolved instanceof PsiMethod) {
|
||||
return classFromTarget.findMethodsBySignature((PsiMethod)resolved, true).length > 0;
|
||||
}
|
||||
if (resolved instanceof PsiField ) {
|
||||
return classFromTarget.findFieldByName(((PsiField)resolved).getName(), false) != null;
|
||||
}
|
||||
if (resolved instanceof PsiClass) {
|
||||
return classFromTarget.findInnerClassByName(((PsiClass)resolved).getName(), false) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (resolved instanceof PsiClass) {
|
||||
final String fqn = ((PsiClass)resolved).getQualifiedName();
|
||||
return fqn != null && JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope) != null;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
public class Main {
|
||||
public static void main(String[] args){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public class Foo {
|
||||
public static void main(String[] args) {}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.refactoring;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.openapi.application.ex.PathManagerEx;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtil;
|
||||
import com.intellij.openapi.roots.ModifiableRootModel;
|
||||
import com.intellij.openapi.roots.ModuleRootModificationUtil;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtilCore;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor;
|
||||
import com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination;
|
||||
import com.intellij.refactoring.util.RefactoringConflictsUtil;
|
||||
import com.intellij.testFramework.*;
|
||||
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
|
||||
import com.intellij.testFramework.fixtures.*;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
|
||||
public class MultipleJdksMoveClassTest extends RefactoringTestCase {
|
||||
|
||||
private CodeInsightTestFixture myFixture;
|
||||
private Module myJava7Module;
|
||||
private Module myJava8Module;
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
try {
|
||||
myFixture.tearDown();
|
||||
}
|
||||
finally {
|
||||
myFixture = null;
|
||||
myJava7Module = null;
|
||||
myJava8Module = null;
|
||||
|
||||
super.tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName());
|
||||
myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(projectBuilder.getFixture());
|
||||
myFixture.setTestDataPath(PathManagerEx.getTestDataPath() + "/refactoring/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, model -> {
|
||||
model.setSdk(IdeaTestUtil.getMockJdk17());
|
||||
String contentUrl = VfsUtilCore.pathToUrl(myFixture.getTempDirPath()) + "/java7";
|
||||
model.addContentEntry(contentUrl).addSourceFolder(contentUrl, false);
|
||||
});
|
||||
|
||||
ModuleRootModificationUtil.updateModel(myJava8Module, model -> {
|
||||
model.setSdk(IdeaTestUtil.getMockJdk18());
|
||||
String contentUrl = VfsUtilCore.pathToUrl(myFixture.getTempDirPath()) + "/java8";
|
||||
model.addContentEntry(contentUrl).addSourceFolder(contentUrl, false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void testConflictStringUsage() throws Exception {
|
||||
final PsiFile[] files = myFixture.configureByFiles("java7/p/Main.java", "java8/p/Foo.java");
|
||||
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
|
||||
RefactoringConflictsUtil.analyzeModuleConflicts(files[0].getProject(), Collections.singletonList(files[0]),
|
||||
UsageInfo.EMPTY_ARRAY, files[1].getVirtualFile(), new MultiMap<>());
|
||||
|
||||
assertEmpty(conflicts.keySet());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user