diff --git a/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenameRefactoringPlugin.java b/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenameRefactoringPlugin.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenameRefactoringPlugin.java +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenameRefactoringPlugin.java @@ -45,6 +45,8 @@ import com.sun.source.tree.Tree; import com.sun.source.util.TreePath; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.text.MessageFormat; import java.util.*; import javax.lang.model.element.*; @@ -52,11 +54,18 @@ import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; import org.netbeans.api.java.source.*; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.api.project.SourceGroup; +import org.netbeans.api.project.Sources; import org.netbeans.modules.refactoring.api.*; import org.netbeans.modules.refactoring.java.RetoucheUtils; import org.netbeans.modules.refactoring.java.spi.JavaRefactoringPlugin; import org.openide.filesystems.FileObject; import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; +import org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation; +import org.openide.filesystems.URLMapper; +import org.openide.util.Exceptions; import org.openide.util.NbBundle; import org.openide.util.Utilities; @@ -267,6 +276,15 @@ // String fqn = "".equals(pkgname) ? newName : pkgname + '.' + newName; // ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE); + String conflictingSource=checkClassRenameInComplementarySource(primFile,newFqn+newName); + if(conflictingSource!=null) { + String msg = new MessageFormat(getString("ERR_ClassClash")).format( + new Object[] {newName, conflictingSource} + ); + fastCheckProblem = createProblem(fastCheckProblem, true, msg); + return fastCheckProblem; + } + if (RetoucheUtils.typeExist(treePathHandle, newFqn)) { String msg = new MessageFormat(getString("ERR_ClassClash")).format( new Object[] {newName, pkgname} @@ -308,7 +326,42 @@ } return fastCheckProblem; } - + + /** + * Check if file rename is ok looking in a complementary source.
+ * For normal sources it checks the rename in test and form test sources in normal sources + * @param oldFile The file to rename + * @param newFQNClassName New FQN of the class + * @return null if the rename is correct, conflicting path otherwise + */ + private String checkClassRenameInComplementarySource(FileObject oldFile,String newFQNClassName) { + Project project=FileOwnerQuery.getOwner(oldFile); + Sources sources=project.getLookup().lookup(Sources.class); + + MultipleRootsUnitTestForSourceQueryImplementation mrufqi=project.getLookup().lookup(MultipleRootsUnitTestForSourceQueryImplementation.class); + for(SourceGroup sg:sources.getSourceGroups(org.netbeans.api.java.project.JavaProjectConstants.SOURCES_TYPE_JAVA)) { + URL urlut[]=mrufqi.findUnitTests(sg.getRootFolder()); + URL urlso[]=mrufqi.findSources(sg.getRootFolder()); + + assert !(urlut!=null && urlso!=null); + URL[] urlstolook=urlut!=null?urlut:urlso; + + if(urlstolook!=null) { + for(URL u:urlstolook) { + try { + URL newURL = new URL(u, newFQNClassName.replaceAll("\\.", "/")+"."+oldFile.getExt()); + FileObject file=URLMapper.findFileObject(newURL); + if(file!=null) + return file.getParent().getPath(); + } catch (MalformedURLException ex) { + Exceptions.printStackTrace(ex); + } + } + } + } + return null; + } + @Override protected Problem checkParameters(CompilationController info) throws IOException {