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