Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,23 +367,8 @@ protected JavaSource getJavaSource(Phase p) {
if (treePathHandle == null && docTreePathHandle == null) {
return null;
}
switch (p) {
case PRECHECK:
case FASTCHECKPARAMETERS:
return JavaSource.forFileObject(docTreePathHandle != null? docTreePathHandle.getTreePathHandle().getFileObject()
: treePathHandle.getFileObject());
case PREPARE:
case CHECKPARAMETERS:
if(treePathHandle != null && treePathHandle.getKind() == Tree.Kind.LABELED_STATEMENT) {
return JavaSource.forFileObject(treePathHandle.getFileObject());
}
ClasspathInfo cpInfo = getClasspathInfo(refactoring);
JavaSource source = JavaSource.create(cpInfo, docTreePathHandle != null? docTreePathHandle.getTreePathHandle().getFileObject()
: treePathHandle.getFileObject());
return source;

}
throw new IllegalStateException();
return JavaSource.forFileObject(docTreePathHandle != null? docTreePathHandle.getTreePathHandle().getFileObject()
: treePathHandle.getFileObject());
}

@Override
Expand All @@ -408,16 +393,10 @@ private Set<FileObject> getRelevantFiles() {
JavaSource source = getJavaSource(Phase.PREPARE);

try {
source.runUserActionTask(new CancellableTask<CompilationController>() {

@Override
public void cancel() {
throw new UnsupportedOperationException("Not supported yet."); // NOI18N
}

source.runUserActionTask(new Task<CompilationController>() {
@Override
public void run(CompilationController info) throws Exception {
final ClassIndex idx = info.getClasspathInfo().getClassIndex();
final ClassIndex idx = getClasspathInfo(refactoring).getClassIndex();
info.toPhase(JavaSource.Phase.RESOLVED);
Element el = docTreePathHandle != null? ((DocTrees)info.getTrees()).getElement(docTreePathHandle.resolve(info))
: treePathHandle.resolveElement(info);
Expand Down Expand Up @@ -631,7 +610,7 @@ public Problem prepare(RefactoringElementsBag elements) {
Set<FileObject> a = getRelevantFiles();
fireProgressListenerStart(AbstractRefactoring.PREPARE, a.size());
TransformTask transform = new TransformTask(new RenameTransformer(treePathHandle, docTreePathHandle, refactoring, allMethods, recordLinkedDeclarations, refactoring.isSearchInComments()), treePathHandle != null && treePathHandle.getKind() == Tree.Kind.LABELED_STATEMENT ? null : treePathHandle);
Problem problem = createAndAddElements(a, transform, elements, refactoring,getClasspathInfo(refactoring));
Problem problem = createAndAddElements(a, transform, elements, null);
fireProgressListenerStop();
return problem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,15 @@ private Collection<ModificationResult> processFiles(Set<FileObject> sourceFiles,
return results;
}

private void processFiles(Map<FileObject, List<FileObject>> work, ClasspathInfo info, boolean modification, Collection<ModificationResult> results, CancellableTask<? extends CompilationController> task) throws IOException, IllegalArgumentException {
//the meaning of overrideInfo (used to be info) is very unclear, and is suspicious. Possibly, it should be eliminated:
private void processFiles(Map<FileObject, List<FileObject>> work, ClasspathInfo overrideInfo, boolean modification, Collection<ModificationResult> results, CancellableTask<? extends CompilationController> task) throws IOException, IllegalArgumentException {
for (Map.Entry<FileObject, List<FileObject>> entry : work.entrySet()) {
if (cancelRequested.get()) {
results.clear();
return;
}
final FileObject root = entry.getKey();
ClasspathInfo info = overrideInfo;
if (info == null) {
ClassPath bootPath = ClassPath.getClassPath(root, ClassPath.BOOT);
if (bootPath == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ ERR_InvertMethodInInterface=ERR_InvertMethodInInterface
#SafeDelete
ERR_VarNotInBlockOrMethod=ERR_VarNotInBlockOrMethod
WRN_Implements=WRN_Implements
ERR_ReferencesFound=ERR_ReferencesFound
ERR_ReferencesFound=ERR_ReferencesFound
ERR_Overrides=ERR_Overrides
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.refactoring.java.test;

import com.sun.source.util.TreePath;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.netbeans.api.java.source.CompilationController;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.TreePathHandle;
import org.netbeans.modules.refactoring.api.Problem;
import org.netbeans.modules.refactoring.api.RefactoringSession;
import org.netbeans.modules.refactoring.api.RenameRefactoring;
import org.netbeans.modules.refactoring.java.ui.JavaRenameProperties;
import org.openide.filesystems.FileObject;
import org.openide.util.lookup.Lookups;

public class ComplexProjectHierarchyTest extends RefactoringTestBase {

private final ProjectDesc side = new ProjectDesc("side");
private final ProjectDesc base = new ProjectDesc("base");
private final ProjectDesc main = new ProjectDesc("main", side, base);

public ComplexProjectHierarchyTest(String name) {
super(name, "17");
}

public void testComplexRename1() throws Exception {
writeFilesAndWaitForScan(getSource(side),
new File("side/Side.java",
"""
package side;
public class Side {}
"""));

String baseCode = """
package base;
public class Base<T> {
public void te|st(T t) {}
}
""";
int pos = baseCode.indexOf('|');

writeFilesAndWaitForScan(getSource(base),
new File("base/Base.java",
baseCode.substring(0, pos) + baseCode.substring(pos + 1)));

writeFilesAndWaitForScan(getSource(main),
new File("main/Main.java",
"""
package main;
import base.Base;
import side.Side;
public class Main extends Base<Side> {
public void test(Side t) {}
public void test(String t) {}
public void run() {
test(new Side());
}
}
"""));
performRename(getSource(base).getFileObject("base/Base.java"), pos, "test2", null, false);
verifyContent(getSource(main),
new File("main/Main.java",
"""
package main;
import base.Base;
import side.Side;
public class Main extends Base<Side> {
public void test2(Side t) {}
public void test(String t) {}
public void run() {
test2(new Side());
}
}
"""));
}

public void testComplexRename2() throws Exception {
writeFilesAndWaitForScan(getSource(side),
new File("side/Side.java",
"""
package side;
public class Side {}
"""));
writeFilesAndWaitForScan(getSource(base),
new File("base/Base.java",
"""
package base;
public class Base<T> {
public void test(T t) {}
}
"""));

String mainCode = """
package main;
import base.Base;
import side.Side;
public class Main extends Base<Side> {
public void test(Side t) {}
public void test(String t) {}
public void run() {
te|st(new Side());
}
}
""";

int pos = mainCode.indexOf('|');

writeFilesAndWaitForScan(getSource(main),
new File("main/Main.java",
mainCode.substring(0, pos) + mainCode.substring(pos + 1)));
performRename(getSource(main).getFileObject("main/Main.java"), pos, "test2", null, false, new Problem(false, "ERR_Overrides"));
verifyContent(getSource(main),
new File("main/Main.java",
"""
package main;
import base.Base;
import side.Side;
public class Main extends Base<Side> {
public void test2(Side t) {}
public void test(String t) {}
public void run() {
test2(new Side());
}
}
"""));
}

@Override
protected List<ProjectDesc> projects() {
return List.of(main, base, side);
}

private void performRename(FileObject source, final int absPos, final String newname, final JavaRenameProperties props, final boolean searchInComments, Problem... expectedProblems) throws Exception {
final RenameRefactoring[] r = new RenameRefactoring[1];
JavaSource.forFileObject(source)
.runUserActionTask(javac -> {
javac.toPhase(JavaSource.Phase.RESOLVED);
TreePath tp = javac.getTreeUtilities().pathFor(absPos);

r[0] = new RenameRefactoring(Lookups.singleton(TreePathHandle.create(tp, javac)));
r[0].setNewName(newname);
r[0].setSearchInComments(searchInComments);
if(props != null) {
r[0].getContext().add(props);
}
}, true);

RefactoringSession rs = RefactoringSession.create("Rename");
List<Problem> problems = new LinkedList<>();

addAllProblems(problems, r[0].preCheck());
if (!problemIsFatal(problems)) {
addAllProblems(problems, r[0].prepare(rs));
}
if (!problemIsFatal(problems)) {
addAllProblems(problems, rs.doRefactoring(true));
}

assertProblems(Arrays.asList(expectedProblems), problems);
}
}
Loading
Loading