Skip to content

Commit 79aacdf

Browse files
committed
Using sharp(er) classpath when rename refactoring.
1 parent b4c38d8 commit 79aacdf

File tree

5 files changed

+318
-72
lines changed

5 files changed

+318
-72
lines changed

java/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenameRefactoringPlugin.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -367,23 +367,8 @@ protected JavaSource getJavaSource(Phase p) {
367367
if (treePathHandle == null && docTreePathHandle == null) {
368368
return null;
369369
}
370-
switch (p) {
371-
case PRECHECK:
372-
case FASTCHECKPARAMETERS:
373-
return JavaSource.forFileObject(docTreePathHandle != null? docTreePathHandle.getTreePathHandle().getFileObject()
374-
: treePathHandle.getFileObject());
375-
case PREPARE:
376-
case CHECKPARAMETERS:
377-
if(treePathHandle != null && treePathHandle.getKind() == Tree.Kind.LABELED_STATEMENT) {
378-
return JavaSource.forFileObject(treePathHandle.getFileObject());
379-
}
380-
ClasspathInfo cpInfo = getClasspathInfo(refactoring);
381-
JavaSource source = JavaSource.create(cpInfo, docTreePathHandle != null? docTreePathHandle.getTreePathHandle().getFileObject()
382-
: treePathHandle.getFileObject());
383-
return source;
384-
385-
}
386-
throw new IllegalStateException();
370+
return JavaSource.forFileObject(docTreePathHandle != null? docTreePathHandle.getTreePathHandle().getFileObject()
371+
: treePathHandle.getFileObject());
387372
}
388373

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

410395
try {
411-
source.runUserActionTask(new CancellableTask<CompilationController>() {
412-
413-
@Override
414-
public void cancel() {
415-
throw new UnsupportedOperationException("Not supported yet."); // NOI18N
416-
}
417-
396+
source.runUserActionTask(new Task<CompilationController>() {
418397
@Override
419398
public void run(CompilationController info) throws Exception {
420-
final ClassIndex idx = info.getClasspathInfo().getClassIndex();
399+
final ClassIndex idx = getClasspathInfo(refactoring).getClassIndex();
421400
info.toPhase(JavaSource.Phase.RESOLVED);
422401
Element el = docTreePathHandle != null? ((DocTrees)info.getTrees()).getElement(docTreePathHandle.resolve(info))
423402
: treePathHandle.resolveElement(info);
@@ -631,7 +610,7 @@ public Problem prepare(RefactoringElementsBag elements) {
631610
Set<FileObject> a = getRelevantFiles();
632611
fireProgressListenerStart(AbstractRefactoring.PREPARE, a.size());
633612
TransformTask transform = new TransformTask(new RenameTransformer(treePathHandle, docTreePathHandle, refactoring, allMethods, recordLinkedDeclarations, refactoring.isSearchInComments()), treePathHandle != null && treePathHandle.getKind() == Tree.Kind.LABELED_STATEMENT ? null : treePathHandle);
634-
Problem problem = createAndAddElements(a, transform, elements, refactoring,getClasspathInfo(refactoring));
613+
Problem problem = createAndAddElements(a, transform, elements, null);
635614
fireProgressListenerStop();
636615
return problem;
637616
}

java/refactoring.java/src/org/netbeans/modules/refactoring/java/spi/JavaRefactoringPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,15 @@ private Collection<ModificationResult> processFiles(Set<FileObject> sourceFiles,
268268
return results;
269269
}
270270

271-
private void processFiles(Map<FileObject, List<FileObject>> work, ClasspathInfo info, boolean modification, Collection<ModificationResult> results, CancellableTask<? extends CompilationController> task) throws IOException, IllegalArgumentException {
271+
//the meaning of overrideInfo (used to be info) is very unclear, and is suspicious. Possibly, it should be eliminated:
272+
private void processFiles(Map<FileObject, List<FileObject>> work, ClasspathInfo overrideInfo, boolean modification, Collection<ModificationResult> results, CancellableTask<? extends CompilationController> task) throws IOException, IllegalArgumentException {
272273
for (Map.Entry<FileObject, List<FileObject>> entry : work.entrySet()) {
273274
if (cancelRequested.get()) {
274275
results.clear();
275276
return;
276277
}
277278
final FileObject root = entry.getKey();
279+
ClasspathInfo info = overrideInfo;
278280
if (info == null) {
279281
ClassPath bootPath = ClassPath.getClassPath(root, ClassPath.BOOT);
280282
if (bootPath == null) {

java/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/plugins/Bundle_test.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,5 @@ ERR_InvertMethodInInterface=ERR_InvertMethodInInterface
108108
#SafeDelete
109109
ERR_VarNotInBlockOrMethod=ERR_VarNotInBlockOrMethod
110110
WRN_Implements=WRN_Implements
111-
ERR_ReferencesFound=ERR_ReferencesFound
111+
ERR_ReferencesFound=ERR_ReferencesFound
112+
ERR_Overrides=ERR_Overrides
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.netbeans.modules.refactoring.java.test;
20+
21+
import com.sun.source.util.TreePath;
22+
import java.util.Arrays;
23+
import java.util.LinkedList;
24+
import java.util.List;
25+
import org.netbeans.api.java.source.CompilationController;
26+
import org.netbeans.api.java.source.JavaSource;
27+
import org.netbeans.api.java.source.TreePathHandle;
28+
import org.netbeans.modules.refactoring.api.Problem;
29+
import org.netbeans.modules.refactoring.api.RefactoringSession;
30+
import org.netbeans.modules.refactoring.api.RenameRefactoring;
31+
import org.netbeans.modules.refactoring.java.ui.JavaRenameProperties;
32+
import org.openide.filesystems.FileObject;
33+
import org.openide.util.lookup.Lookups;
34+
35+
public class ComplexProjectHierarchyTest extends RefactoringTestBase {
36+
37+
private final ProjectDesc side = new ProjectDesc("side");
38+
private final ProjectDesc base = new ProjectDesc("base");
39+
private final ProjectDesc main = new ProjectDesc("main", side, base);
40+
41+
public ComplexProjectHierarchyTest(String name) {
42+
super(name, "17");
43+
}
44+
45+
public void testComplexRename1() throws Exception {
46+
writeFilesAndWaitForScan(getSource(side),
47+
new File("side/Side.java",
48+
"""
49+
package side;
50+
public class Side {}
51+
"""));
52+
53+
String baseCode = """
54+
package base;
55+
public class Base<T> {
56+
public void te|st(T t) {}
57+
}
58+
""";
59+
int pos = baseCode.indexOf('|');
60+
61+
writeFilesAndWaitForScan(getSource(base),
62+
new File("base/Base.java",
63+
baseCode.substring(0, pos) + baseCode.substring(pos + 1)));
64+
65+
writeFilesAndWaitForScan(getSource(main),
66+
new File("main/Main.java",
67+
"""
68+
package main;
69+
import base.Base;
70+
import side.Side;
71+
public class Main extends Base<Side> {
72+
public void test(Side t) {}
73+
public void test(String t) {}
74+
public void run() {
75+
test(new Side());
76+
}
77+
}
78+
"""));
79+
performRename(getSource(base).getFileObject("base/Base.java"), pos, "test2", null, false);
80+
verifyContent(getSource(main),
81+
new File("main/Main.java",
82+
"""
83+
package main;
84+
import base.Base;
85+
import side.Side;
86+
public class Main extends Base<Side> {
87+
public void test2(Side t) {}
88+
public void test(String t) {}
89+
public void run() {
90+
test2(new Side());
91+
}
92+
}
93+
"""));
94+
}
95+
96+
public void testComplexRename2() throws Exception {
97+
writeFilesAndWaitForScan(getSource(side),
98+
new File("side/Side.java",
99+
"""
100+
package side;
101+
public class Side {}
102+
"""));
103+
writeFilesAndWaitForScan(getSource(base),
104+
new File("base/Base.java",
105+
"""
106+
package base;
107+
public class Base<T> {
108+
public void test(T t) {}
109+
}
110+
"""));
111+
112+
String mainCode = """
113+
package main;
114+
import base.Base;
115+
import side.Side;
116+
public class Main extends Base<Side> {
117+
public void test(Side t) {}
118+
public void test(String t) {}
119+
public void run() {
120+
te|st(new Side());
121+
}
122+
}
123+
""";
124+
125+
int pos = mainCode.indexOf('|');
126+
127+
writeFilesAndWaitForScan(getSource(main),
128+
new File("main/Main.java",
129+
mainCode.substring(0, pos) + mainCode.substring(pos + 1)));
130+
performRename(getSource(main).getFileObject("main/Main.java"), pos, "test2", null, false, new Problem(false, "ERR_Overrides"));
131+
verifyContent(getSource(main),
132+
new File("main/Main.java",
133+
"""
134+
package main;
135+
import base.Base;
136+
import side.Side;
137+
public class Main extends Base<Side> {
138+
public void test2(Side t) {}
139+
public void test(String t) {}
140+
public void run() {
141+
test2(new Side());
142+
}
143+
}
144+
"""));
145+
}
146+
147+
@Override
148+
protected List<ProjectDesc> projects() {
149+
return List.of(main, base, side);
150+
}
151+
152+
private void performRename(FileObject source, final int absPos, final String newname, final JavaRenameProperties props, final boolean searchInComments, Problem... expectedProblems) throws Exception {
153+
final RenameRefactoring[] r = new RenameRefactoring[1];
154+
JavaSource.forFileObject(source)
155+
.runUserActionTask(javac -> {
156+
javac.toPhase(JavaSource.Phase.RESOLVED);
157+
TreePath tp = javac.getTreeUtilities().pathFor(absPos);
158+
159+
r[0] = new RenameRefactoring(Lookups.singleton(TreePathHandle.create(tp, javac)));
160+
r[0].setNewName(newname);
161+
r[0].setSearchInComments(searchInComments);
162+
if(props != null) {
163+
r[0].getContext().add(props);
164+
}
165+
}, true);
166+
167+
RefactoringSession rs = RefactoringSession.create("Rename");
168+
List<Problem> problems = new LinkedList<>();
169+
170+
addAllProblems(problems, r[0].preCheck());
171+
if (!problemIsFatal(problems)) {
172+
addAllProblems(problems, r[0].prepare(rs));
173+
}
174+
if (!problemIsFatal(problems)) {
175+
addAllProblems(problems, rs.doRefactoring(true));
176+
}
177+
178+
assertProblems(Arrays.asList(expectedProblems), problems);
179+
}
180+
}

0 commit comments

Comments
 (0)