Lines 44-58
Link Here
|
44 |
package org.netbeans.api.java.source.gen; |
44 |
package org.netbeans.api.java.source.gen; |
45 |
|
45 |
|
46 |
import com.sun.source.tree.BlockTree; |
46 |
import com.sun.source.tree.BlockTree; |
47 |
import com.sun.source.tree.ClassTree; |
|
|
48 |
import com.sun.source.tree.CompilationUnitTree; |
47 |
import com.sun.source.tree.CompilationUnitTree; |
49 |
import com.sun.source.tree.ExpressionTree; |
48 |
import com.sun.source.tree.ExpressionTree; |
50 |
import com.sun.source.tree.ForLoopTree; |
49 |
import com.sun.source.tree.LambdaExpressionTree; |
51 |
import com.sun.source.tree.MethodTree; |
50 |
import com.sun.source.tree.LiteralTree; |
|
|
51 |
import com.sun.source.tree.MemberReferenceTree; |
52 |
import com.sun.source.tree.MemberReferenceTree.ReferenceMode; |
53 |
import com.sun.source.tree.ReturnTree; |
52 |
import com.sun.source.tree.VariableTree; |
54 |
import com.sun.source.tree.VariableTree; |
|
|
55 |
import com.sun.source.util.TreeScanner; |
53 |
import java.io.File; |
56 |
import java.io.File; |
54 |
import java.io.IOException; |
57 |
import java.io.IOException; |
55 |
import javax.lang.model.type.TypeKind; |
58 |
import java.util.Collections; |
|
|
59 |
import java.util.EnumSet; |
60 |
import javax.lang.model.element.Modifier; |
56 |
import org.netbeans.api.java.source.Task; |
61 |
import org.netbeans.api.java.source.Task; |
57 |
import org.netbeans.api.java.source.JavaSource; |
62 |
import org.netbeans.api.java.source.JavaSource; |
58 |
import org.netbeans.api.java.source.JavaSource.Phase; |
63 |
import org.netbeans.api.java.source.JavaSource.Phase; |
Lines 66-121
Link Here
|
66 |
* |
71 |
* |
67 |
* @author Pavel Flaska |
72 |
* @author Pavel Flaska |
68 |
*/ |
73 |
*/ |
69 |
public class AddCastTest extends GeneratorTestMDRCompat { |
74 |
public class LambdaTest extends GeneratorTestMDRCompat { |
70 |
|
75 |
|
71 |
/** Creates a new instance of AddCastTest */ |
76 |
/** Creates a new instance of AddCastTest */ |
72 |
public AddCastTest(String name) { |
77 |
public LambdaTest(String name) { |
73 |
super(name); |
78 |
super(name); |
74 |
} |
79 |
} |
75 |
|
80 |
|
76 |
public static NbTestSuite suite() { |
81 |
public static NbTestSuite suite() { |
77 |
NbTestSuite suite = new NbTestSuite(); |
82 |
NbTestSuite suite = new NbTestSuite(); |
78 |
suite.addTestSuite(AddCastTest.class); |
83 |
suite.addTestSuite(LambdaTest.class); |
79 |
return suite; |
84 |
return suite; |
80 |
} |
85 |
} |
81 |
|
86 |
|
82 |
public void testAddCastToDeclStmt() throws Exception { |
87 |
public void testPrintMemberReference() throws Exception { |
83 |
testFile = new File(getWorkDir(), "Test.java"); |
88 |
testFile = new File(getWorkDir(), "Test.java"); |
84 |
TestUtilities.copyStringToFile(testFile, |
89 |
TestUtilities.copyStringToFile(testFile, |
85 |
"package hierbas.del.litoral;\n\n" + |
90 |
"package hierbas.del.litoral;\n\n" + |
86 |
"import java.util.*;\n\n" + |
91 |
"public class Test {\n" + |
87 |
"public class Test<E> {\n" + |
92 |
" public static void taragui() {\n" + |
88 |
" public void taragui() {\n" + |
93 |
" Runnable r = null;\n" + |
89 |
" System.err.println(\"taragui() method\");\n" + |
|
|
90 |
" String s = \"Oven.\";\n" + |
91 |
"// line comment\n" + |
92 |
" }\n" + |
94 |
" }\n" + |
93 |
"}\n" |
95 |
"}\n" |
94 |
); |
96 |
); |
95 |
String golden = |
97 |
String golden = |
96 |
"package hierbas.del.litoral;\n\n" + |
98 |
"package hierbas.del.litoral;\n\n" + |
97 |
"import java.util.*;\n\n" + |
99 |
"public class Test {\n" + |
98 |
"public class Test<E> {\n" + |
100 |
" public static void taragui() {\n" + |
99 |
" public void taragui() {\n" + |
101 |
" Runnable r = Test::taragui;\n" + |
100 |
" System.err.println(\"taragui() method\");\n" + |
|
|
101 |
" String s = (String) \"Oven.\";\n" + |
102 |
"// line comment\n" + |
103 |
" }\n" + |
102 |
" }\n" + |
104 |
"}\n"; |
103 |
"}\n"; |
105 |
JavaSource src = getJavaSource(testFile); |
104 |
JavaSource src = getJavaSource(testFile); |
106 |
|
105 |
|
107 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
106 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
108 |
|
107 |
|
109 |
public void run(WorkingCopy workingCopy) throws IOException { |
108 |
public void run(final WorkingCopy workingCopy) throws IOException { |
110 |
workingCopy.toPhase(Phase.RESOLVED); |
109 |
workingCopy.toPhase(Phase.RESOLVED); |
111 |
CompilationUnitTree cut = workingCopy.getCompilationUnit(); |
110 |
CompilationUnitTree cut = workingCopy.getCompilationUnit(); |
112 |
TreeMaker make = workingCopy.getTreeMaker(); |
111 |
final TreeMaker make = workingCopy.getTreeMaker(); |
113 |
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
112 |
new TreeScanner<Void, Void>() { |
114 |
MethodTree method = (MethodTree) clazz.getMembers().get(1); |
113 |
@Override public Void visitLiteral(LiteralTree node, Void p) { |
115 |
VariableTree var = (VariableTree) method.getBody().getStatements().get(1); |
114 |
workingCopy.rewrite(node, make.MemberReference(ReferenceMode.INVOKE, make.Identifier("Test"), "taragui", Collections.<ExpressionTree>emptyList())); |
116 |
ExpressionTree init = var.getInitializer(); |
115 |
return super.visitLiteral(node, p); |
117 |
ExpressionTree cast = make.TypeCast(make.Identifier("String"), init); |
116 |
} |
118 |
workingCopy.rewrite(init, cast); |
117 |
}.scan(workingCopy.getCompilationUnit(), null); |
119 |
} |
118 |
} |
120 |
|
119 |
|
121 |
}; |
120 |
}; |
Lines 125-171
Link Here
|
125 |
assertEquals(golden, res); |
124 |
assertEquals(golden, res); |
126 |
} |
125 |
} |
127 |
|
126 |
|
128 |
public void testAddCastInForWithoutSteps() throws Exception { |
127 |
public void testBasicLambdaDiff() throws Exception { |
129 |
testFile = new File(getWorkDir(), "Test.java"); |
128 |
testFile = new File(getWorkDir(), "Test.java"); |
130 |
TestUtilities.copyStringToFile(testFile, |
129 |
TestUtilities.copyStringToFile(testFile, |
131 |
"package hierbas.del.litoral;\n\n" + |
130 |
"package hierbas.del.litoral;\n\n" + |
132 |
"import java.util.*;\n\n" + |
131 |
"public class Test {\n" + |
133 |
"public class Test<E> {\n" + |
132 |
" public static void taragui() {\n" + |
134 |
" public void cast() {\n" + |
133 |
" ChangeListener l = (e) -> {};\n" + |
135 |
" Object o = null;\n" + |
|
|
136 |
" for (int i = 0; i < 5; ) {\n" + |
137 |
" String s = o;\n" + |
138 |
" }\n" + |
139 |
" }\n" + |
134 |
" }\n" + |
140 |
"}\n" |
135 |
"}\n" |
141 |
); |
136 |
); |
142 |
String golden = |
137 |
String golden = |
143 |
"package hierbas.del.litoral;\n\n" + |
138 |
"package hierbas.del.litoral;\n\n" + |
144 |
"import java.util.*;\n\n" + |
139 |
"public class Test {\n" + |
145 |
"public class Test<E> {\n" + |
140 |
" public static void taragui() {\n" + |
146 |
" public void cast() {\n" + |
141 |
" ChangeListener l = (f) -> {};\n" + |
147 |
" Object o = null;\n" + |
|
|
148 |
" for (int i = 0; i < 5; ) {\n" + |
149 |
" String s = (String) o;\n" + |
150 |
" }\n" + |
151 |
" }\n" + |
142 |
" }\n" + |
152 |
"}\n"; |
143 |
"}\n"; |
153 |
JavaSource src = getJavaSource(testFile); |
144 |
JavaSource src = getJavaSource(testFile); |
154 |
|
145 |
|
155 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
146 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
156 |
|
147 |
|
157 |
public void run(WorkingCopy workingCopy) throws IOException { |
148 |
public void run(final WorkingCopy workingCopy) throws IOException { |
158 |
workingCopy.toPhase(Phase.RESOLVED); |
149 |
workingCopy.toPhase(Phase.RESOLVED); |
159 |
CompilationUnitTree cut = workingCopy.getCompilationUnit(); |
150 |
CompilationUnitTree cut = workingCopy.getCompilationUnit(); |
160 |
TreeMaker make = workingCopy.getTreeMaker(); |
151 |
final TreeMaker make = workingCopy.getTreeMaker(); |
161 |
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
152 |
new TreeScanner<Void, Void>() { |
162 |
MethodTree method = (MethodTree) clazz.getMembers().get(1); |
153 |
@Override public Void visitVariable(VariableTree node, Void p) { |
163 |
ForLoopTree forLoop = (ForLoopTree) method.getBody().getStatements().get(1); |
154 |
if (node.getName().contentEquals("e")) { |
164 |
BlockTree block = (BlockTree) forLoop.getStatement(); |
155 |
workingCopy.rewrite(node, make.setLabel(node, "f")); |
165 |
VariableTree vt = (VariableTree) block.getStatements().get(0); |
156 |
} |
166 |
ExpressionTree init = vt.getInitializer(); |
157 |
return super.visitVariable(node, p); |
167 |
ExpressionTree cast = make.TypeCast(make.Identifier("String"), init); |
158 |
} |
168 |
workingCopy.rewrite(init, cast); |
159 |
}.scan(workingCopy.getCompilationUnit(), null); |
169 |
} |
160 |
} |
170 |
|
161 |
|
171 |
}; |
162 |
}; |
Lines 175-230
Link Here
|
175 |
assertEquals(golden, res); |
166 |
assertEquals(golden, res); |
176 |
} |
167 |
} |
177 |
|
168 |
|
178 |
/* |
169 |
public void testAddFirstLambdaParam() throws Exception { |
179 |
* #94324 |
|
|
180 |
*/ |
181 |
public void testAddCast94324() throws Exception { |
182 |
testFile = new File(getWorkDir(), "Test.java"); |
170 |
testFile = new File(getWorkDir(), "Test.java"); |
183 |
TestUtilities.copyStringToFile(testFile, |
171 |
TestUtilities.copyStringToFile(testFile, |
184 |
"package javaapplication3;\n" + |
172 |
"package hierbas.del.litoral;\n\n" + |
185 |
"\n" + |
173 |
"public class Test {\n" + |
186 |
"public class Klasa {\n" + |
174 |
" public static void taragui() {\n" + |
187 |
" \n" + |
175 |
" ChangeListener l = () -> {};\n" + |
188 |
" static Object method() {\n" + |
|
|
189 |
" return null;\n" + |
190 |
" }\n" + |
176 |
" }\n" + |
191 |
" \n" + |
177 |
"}\n" |
192 |
" public static void main(String[] args) {\n" + |
178 |
); |
193 |
" // TODO code application logic here\n" + |
179 |
String golden = |
194 |
" String s = Klasa.method();\n" + |
180 |
"package hierbas.del.litoral;\n\n" + |
|
|
181 |
"public class Test {\n" + |
182 |
" public static void taragui() {\n" + |
183 |
" ChangeListener l = (e) -> {};\n" + |
195 |
" }\n" + |
184 |
" }\n" + |
196 |
"\n" + |
|
|
197 |
"}\n" |
198 |
); |
199 |
String golden = |
200 |
"package javaapplication3;\n" + |
201 |
"\n" + |
202 |
"public class Klasa {\n" + |
203 |
" \n" + |
204 |
" static Object method() {\n" + |
205 |
" return null;\n" + |
206 |
" }\n" + |
207 |
" \n" + |
208 |
" public static void main(String[] args) {\n" + |
209 |
" // TODO code application logic here\n" + |
210 |
" String s = (String) Klasa.method();\n" + |
211 |
" }\n" + |
212 |
"\n" + |
213 |
"}\n"; |
185 |
"}\n"; |
214 |
JavaSource src = getJavaSource(testFile); |
186 |
JavaSource src = getJavaSource(testFile); |
215 |
|
187 |
|
216 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
188 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
217 |
|
189 |
|
218 |
public void run(WorkingCopy workingCopy) throws IOException { |
190 |
public void run(final WorkingCopy workingCopy) throws IOException { |
219 |
workingCopy.toPhase(Phase.RESOLVED); |
191 |
workingCopy.toPhase(Phase.RESOLVED); |
220 |
CompilationUnitTree cut = workingCopy.getCompilationUnit(); |
192 |
final TreeMaker make = workingCopy.getTreeMaker(); |
221 |
TreeMaker make = workingCopy.getTreeMaker(); |
193 |
new TreeScanner<Void, Void>() { |
222 |
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
194 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
223 |
MethodTree method = (MethodTree) clazz.getMembers().get(2); |
195 |
workingCopy.rewrite(node, make.addLambdaParameter(node, make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "e", null, null))); |
224 |
VariableTree var = (VariableTree) method.getBody().getStatements().get(0); |
196 |
return super.visitLambdaExpression(node, p); |
225 |
ExpressionTree init = var.getInitializer(); |
197 |
} |
226 |
ExpressionTree cast = make.TypeCast(make.Identifier("String"), init); |
198 |
}.scan(workingCopy.getCompilationUnit(), null); |
227 |
workingCopy.rewrite(init, cast); |
|
|
228 |
} |
199 |
} |
229 |
|
200 |
|
230 |
}; |
201 |
}; |
Lines 234-274
Link Here
|
234 |
assertEquals(golden, res); |
205 |
assertEquals(golden, res); |
235 |
} |
206 |
} |
236 |
|
207 |
|
237 |
public void testAddCastLiteral() throws Exception { |
208 |
public void testAddSecondLambdaParam() throws Exception { |
238 |
testFile = new File(getWorkDir(), "Test.java"); |
209 |
testFile = new File(getWorkDir(), "Test.java"); |
239 |
TestUtilities.copyStringToFile(testFile, |
210 |
TestUtilities.copyStringToFile(testFile, |
240 |
"package javaapplication3;\n" + |
211 |
"package hierbas.del.litoral;\n\n" + |
241 |
"\n" + |
212 |
"public class Test {\n" + |
242 |
"public class Klasa {\n" + |
213 |
" public static void taragui() {\n" + |
243 |
" public static void main(String[] args) {\n" + |
214 |
" ChangeListener l = (e) -> {};\n" + |
244 |
" int i = args.length & 0xFEFE;\n" + |
|
|
245 |
" }\n" + |
215 |
" }\n" + |
246 |
"\n" + |
|
|
247 |
"}\n" |
216 |
"}\n" |
248 |
); |
217 |
); |
249 |
String golden = |
218 |
String golden = |
250 |
"package javaapplication3;\n" + |
219 |
"package hierbas.del.litoral;\n\n" + |
251 |
"\n" + |
220 |
"public class Test {\n" + |
252 |
"public class Klasa {\n" + |
221 |
" public static void taragui() {\n" + |
253 |
" public static void main(String[] args) {\n" + |
222 |
" ChangeListener l = (e, f) -> {};\n" + |
254 |
" int i = (int) (args.length & 0xFEFE);\n" + |
|
|
255 |
" }\n" + |
223 |
" }\n" + |
256 |
"\n" + |
|
|
257 |
"}\n"; |
224 |
"}\n"; |
258 |
JavaSource src = getJavaSource(testFile); |
225 |
JavaSource src = getJavaSource(testFile); |
259 |
|
226 |
|
260 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
227 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
261 |
|
228 |
|
262 |
public void run(WorkingCopy workingCopy) throws IOException { |
229 |
public void run(final WorkingCopy workingCopy) throws IOException { |
263 |
workingCopy.toPhase(Phase.RESOLVED); |
230 |
workingCopy.toPhase(Phase.RESOLVED); |
264 |
CompilationUnitTree cut = workingCopy.getCompilationUnit(); |
231 |
final TreeMaker make = workingCopy.getTreeMaker(); |
265 |
TreeMaker make = workingCopy.getTreeMaker(); |
232 |
new TreeScanner<Void, Void>() { |
266 |
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
233 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
267 |
MethodTree method = (MethodTree) clazz.getMembers().get(1); |
234 |
workingCopy.rewrite(node, make.addLambdaParameter(node, make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "f", null, null))); |
268 |
VariableTree var = (VariableTree) method.getBody().getStatements().get(0); |
235 |
return super.visitLambdaExpression(node, p); |
269 |
ExpressionTree init = var.getInitializer(); |
236 |
} |
270 |
ExpressionTree cast = make.TypeCast(make.PrimitiveType(TypeKind.INT), make.Parenthesized(init)); |
237 |
}.scan(workingCopy.getCompilationUnit(), null); |
271 |
workingCopy.rewrite(init, cast); |
|
|
272 |
} |
238 |
} |
273 |
|
239 |
|
274 |
}; |
240 |
}; |
Lines 277-351
Link Here
|
277 |
System.err.println(res); |
243 |
System.err.println(res); |
278 |
assertEquals(golden, res); |
244 |
assertEquals(golden, res); |
279 |
} |
245 |
} |
280 |
|
246 |
|
281 |
/* |
247 |
public void testPrependSecondLambdaParam() throws Exception { |
282 |
* #94324 - second test. If the moved expression is changed, it is again |
|
|
283 |
* rewritten by VeryPretty - in this time, i do not see any solution for |
284 |
* that. |
285 |
*//* |
286 |
public void testAddCast94324_2() throws Exception { |
287 |
testFile = new File(getWorkDir(), "Test.java"); |
248 |
testFile = new File(getWorkDir(), "Test.java"); |
288 |
TestUtilities.copyStringToFile(testFile, |
249 |
TestUtilities.copyStringToFile(testFile, |
289 |
"package javaapplication3;\n" + |
250 |
"package hierbas.del.litoral;\n\n" + |
290 |
"\n" + |
251 |
"public class Test {\n" + |
291 |
"public class Klasa {\n" + |
252 |
" public static void taragui() {\n" + |
292 |
" \n" + |
253 |
" ChangeListener l = (e) -> {};\n" + |
293 |
" static Object method() {\n" + |
|
|
294 |
" return null;\n" + |
295 |
" }\n" + |
254 |
" }\n" + |
296 |
" \n" + |
255 |
"}\n" |
297 |
" public static void main(String[] args) {\n" + |
256 |
); |
298 |
" // TODO code application logic here\n" + |
257 |
String golden = |
299 |
" String s = Klasa.method();\n" + |
258 |
"package hierbas.del.litoral;\n\n" + |
|
|
259 |
"public class Test {\n" + |
260 |
" public static void taragui() {\n" + |
261 |
" ChangeListener l = (f, e) -> {};\n" + |
300 |
" }\n" + |
262 |
" }\n" + |
301 |
"\n" + |
|
|
302 |
"}\n" |
303 |
); |
304 |
String golden = |
305 |
"package javaapplication3;\n" + |
306 |
"\n" + |
307 |
"public class Klasa {\n" + |
308 |
" \n" + |
309 |
" static Object method() {\n" + |
310 |
" return null;\n" + |
311 |
" }\n" + |
312 |
" \n" + |
313 |
" public static void main(String[] args) {\n" + |
314 |
" // TODO code application logic here\n" + |
315 |
" String s = (String) javaapplication3.Klasa.metoda();\n" + |
316 |
" }\n" + |
317 |
"\n" + |
318 |
"}\n"; |
263 |
"}\n"; |
319 |
JavaSource src = getJavaSource(testFile); |
264 |
JavaSource src = getJavaSource(testFile); |
320 |
|
265 |
|
321 |
CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() { |
266 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
322 |
|
267 |
|
323 |
public void run(WorkingCopy workingCopy) throws IOException { |
268 |
public void run(final WorkingCopy workingCopy) throws IOException { |
324 |
workingCopy.toPhase(Phase.RESOLVED); |
269 |
workingCopy.toPhase(Phase.RESOLVED); |
325 |
CompilationUnitTree cut = workingCopy.getCompilationUnit(); |
270 |
final TreeMaker make = workingCopy.getTreeMaker(); |
326 |
TreeMaker make = workingCopy.getTreeMaker(); |
271 |
new TreeScanner<Void, Void>() { |
327 |
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); |
272 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
328 |
MethodTree method = (MethodTree) clazz.getMembers().get(2); |
273 |
workingCopy.rewrite(node, make.insertLambdaParameter(node, 0, make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "f", null, null))); |
329 |
VariableTree var = (VariableTree) method.getBody().getStatements().get(0); |
274 |
return super.visitLambdaExpression(node, p); |
330 |
MethodInvocationTree init = (MethodInvocationTree) var.getInitializer(); |
275 |
} |
331 |
MethodInvocationTree initCopy = make.MethodInvocation( |
276 |
}.scan(workingCopy.getCompilationUnit(), null); |
332 |
(List<ExpressionTree>) init.getTypeArguments(), |
|
|
333 |
make.setLabel((MemberSelectTree) init.getMethodSelect(), "metoda"), |
334 |
init.getArguments() |
335 |
); |
336 |
workingCopy.rewrite(init, initCopy); |
337 |
ExpressionTree cast = make.TypeCast(make.Identifier("String"), initCopy); |
338 |
workingCopy.rewrite(init, cast); |
339 |
} |
277 |
} |
340 |
|
278 |
|
341 |
public void cancel() { |
|
|
342 |
} |
343 |
}; |
279 |
}; |
344 |
src.runModificationTask(task).commit(); |
280 |
src.runModificationTask(task).commit(); |
345 |
String res = TestUtilities.copyFileToString(testFile); |
281 |
String res = TestUtilities.copyFileToString(testFile); |
346 |
System.err.println(res); |
282 |
System.err.println(res); |
347 |
assertEquals(golden, res); |
283 |
assertEquals(golden, res); |
348 |
}*/ |
284 |
} |
|
|
285 |
|
286 |
public void testRemoveFirstLambdaParam() throws Exception { |
287 |
testFile = new File(getWorkDir(), "Test.java"); |
288 |
TestUtilities.copyStringToFile(testFile, |
289 |
"package hierbas.del.litoral;\n\n" + |
290 |
"public class Test {\n" + |
291 |
" public static void taragui() {\n" + |
292 |
" ChangeListener l = (e, f) -> {};\n" + |
293 |
" }\n" + |
294 |
"}\n" |
295 |
); |
296 |
String golden = |
297 |
"package hierbas.del.litoral;\n\n" + |
298 |
"public class Test {\n" + |
299 |
" public static void taragui() {\n" + |
300 |
" ChangeListener l = (f) -> {};\n" + |
301 |
" }\n" + |
302 |
"}\n"; |
303 |
JavaSource src = getJavaSource(testFile); |
304 |
|
305 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
306 |
|
307 |
public void run(final WorkingCopy workingCopy) throws IOException { |
308 |
workingCopy.toPhase(Phase.RESOLVED); |
309 |
final TreeMaker make = workingCopy.getTreeMaker(); |
310 |
new TreeScanner<Void, Void>() { |
311 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
312 |
workingCopy.rewrite(node, make.removeLambdaParameter(node, 0)); |
313 |
return super.visitLambdaExpression(node, p); |
314 |
} |
315 |
}.scan(workingCopy.getCompilationUnit(), null); |
316 |
} |
317 |
|
318 |
}; |
319 |
src.runModificationTask(task).commit(); |
320 |
String res = TestUtilities.copyFileToString(testFile); |
321 |
System.err.println(res); |
322 |
assertEquals(golden, res); |
323 |
} |
324 |
|
325 |
public void testRemoveSecondLambdaParam() throws Exception { |
326 |
testFile = new File(getWorkDir(), "Test.java"); |
327 |
TestUtilities.copyStringToFile(testFile, |
328 |
"package hierbas.del.litoral;\n\n" + |
329 |
"public class Test {\n" + |
330 |
" public static void taragui() {\n" + |
331 |
" ChangeListener l = (e, f) -> {};\n" + |
332 |
" }\n" + |
333 |
"}\n" |
334 |
); |
335 |
String golden = |
336 |
"package hierbas.del.litoral;\n\n" + |
337 |
"public class Test {\n" + |
338 |
" public static void taragui() {\n" + |
339 |
" ChangeListener l = (e) -> {};\n" + |
340 |
" }\n" + |
341 |
"}\n"; |
342 |
JavaSource src = getJavaSource(testFile); |
343 |
|
344 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
345 |
|
346 |
public void run(final WorkingCopy workingCopy) throws IOException { |
347 |
workingCopy.toPhase(Phase.RESOLVED); |
348 |
final TreeMaker make = workingCopy.getTreeMaker(); |
349 |
new TreeScanner<Void, Void>() { |
350 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
351 |
workingCopy.rewrite(node, make.removeLambdaParameter(node, 1)); |
352 |
return super.visitLambdaExpression(node, p); |
353 |
} |
354 |
}.scan(workingCopy.getCompilationUnit(), null); |
355 |
} |
356 |
|
357 |
}; |
358 |
src.runModificationTask(task).commit(); |
359 |
String res = TestUtilities.copyFileToString(testFile); |
360 |
System.err.println(res); |
361 |
assertEquals(golden, res); |
362 |
} |
363 |
|
364 |
public void testOnlyLambdaParam() throws Exception { |
365 |
testFile = new File(getWorkDir(), "Test.java"); |
366 |
TestUtilities.copyStringToFile(testFile, |
367 |
"package hierbas.del.litoral;\n\n" + |
368 |
"public class Test {\n" + |
369 |
" public static void taragui() {\n" + |
370 |
" ChangeListener l = (e) -> {};\n" + |
371 |
" }\n" + |
372 |
"}\n" |
373 |
); |
374 |
String golden = |
375 |
"package hierbas.del.litoral;\n\n" + |
376 |
"public class Test {\n" + |
377 |
" public static void taragui() {\n" + |
378 |
" ChangeListener l = () -> {};\n" + |
379 |
" }\n" + |
380 |
"}\n"; |
381 |
JavaSource src = getJavaSource(testFile); |
382 |
|
383 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
384 |
|
385 |
public void run(final WorkingCopy workingCopy) throws IOException { |
386 |
workingCopy.toPhase(Phase.RESOLVED); |
387 |
final TreeMaker make = workingCopy.getTreeMaker(); |
388 |
new TreeScanner<Void, Void>() { |
389 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
390 |
workingCopy.rewrite(node, make.removeLambdaParameter(node, 0)); |
391 |
return super.visitLambdaExpression(node, p); |
392 |
} |
393 |
}.scan(workingCopy.getCompilationUnit(), null); |
394 |
} |
395 |
|
396 |
}; |
397 |
src.runModificationTask(task).commit(); |
398 |
String res = TestUtilities.copyFileToString(testFile); |
399 |
System.err.println(res); |
400 |
assertEquals(golden, res); |
401 |
} |
402 |
|
403 |
public void testLambdaFullBody2Expression() throws Exception { |
404 |
testFile = new File(getWorkDir(), "Test.java"); |
405 |
TestUtilities.copyStringToFile(testFile, |
406 |
"package hierbas.del.litoral;\n\n" + |
407 |
"public class Test {\n" + |
408 |
" public static void taragui() {\n" + |
409 |
" ChangeListener l = (e) -> {return 1;};\n" + |
410 |
" }\n" + |
411 |
"}\n" |
412 |
); |
413 |
String golden = |
414 |
"package hierbas.del.litoral;\n\n" + |
415 |
"public class Test {\n" + |
416 |
" public static void taragui() {\n" + |
417 |
" ChangeListener l = (e) -> 1;\n" + |
418 |
" }\n" + |
419 |
"}\n"; |
420 |
JavaSource src = getJavaSource(testFile); |
421 |
|
422 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
423 |
|
424 |
public void run(final WorkingCopy workingCopy) throws IOException { |
425 |
workingCopy.toPhase(Phase.RESOLVED); |
426 |
final TreeMaker make = workingCopy.getTreeMaker(); |
427 |
new TreeScanner<Void, Void>() { |
428 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
429 |
ReturnTree t = (ReturnTree) ((BlockTree) node.getBody()).getStatements().get(0); |
430 |
workingCopy.rewrite(node, make.setLambdaBody(node, t.getExpression())); |
431 |
return super.visitLambdaExpression(node, p); |
432 |
} |
433 |
}.scan(workingCopy.getCompilationUnit(), null); |
434 |
} |
435 |
|
436 |
}; |
437 |
src.runModificationTask(task).commit(); |
438 |
String res = TestUtilities.copyFileToString(testFile); |
439 |
System.err.println(res); |
440 |
assertEquals(golden, res); |
441 |
} |
442 |
|
443 |
public void testLambdaExpression2FullBody() throws Exception { |
444 |
testFile = new File(getWorkDir(), "Test.java"); |
445 |
TestUtilities.copyStringToFile(testFile, |
446 |
"package hierbas.del.litoral;\n\n" + |
447 |
"public class Test {\n" + |
448 |
" public static void taragui() {\n" + |
449 |
" ChangeListener l = (e) -> 1;\n" + |
450 |
" }\n" + |
451 |
"}\n" |
452 |
); |
453 |
String golden = |
454 |
"package hierbas.del.litoral;\n\n" + |
455 |
"public class Test {\n" + |
456 |
" public static void taragui() {\n" + |
457 |
" ChangeListener l = (e) -> {\n" + |
458 |
" return 1;\n" + |
459 |
" };\n" + |
460 |
" }\n" + |
461 |
"}\n"; |
462 |
JavaSource src = getJavaSource(testFile); |
463 |
|
464 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
465 |
|
466 |
public void run(final WorkingCopy workingCopy) throws IOException { |
467 |
workingCopy.toPhase(Phase.RESOLVED); |
468 |
final TreeMaker make = workingCopy.getTreeMaker(); |
469 |
new TreeScanner<Void, Void>() { |
470 |
@Override public Void visitLambdaExpression(LambdaExpressionTree node, Void p) { |
471 |
workingCopy.rewrite(node, make.setLambdaBody(node, make.Block(Collections.singletonList(make.Return((ExpressionTree) node.getBody())), false))); |
472 |
return super.visitLambdaExpression(node, p); |
473 |
} |
474 |
}.scan(workingCopy.getCompilationUnit(), null); |
475 |
} |
476 |
|
477 |
}; |
478 |
src.runModificationTask(task).commit(); |
479 |
String res = TestUtilities.copyFileToString(testFile); |
480 |
System.err.println(res); |
481 |
assertEquals(golden, res); |
482 |
} |
483 |
|
484 |
public void testMethodReferenceDiff() throws Exception { |
485 |
testFile = new File(getWorkDir(), "Test.java"); |
486 |
TestUtilities.copyStringToFile(testFile, |
487 |
"package hierbas.del.litoral;\n\n" + |
488 |
"public class Test {\n" + |
489 |
" public static void taragui() {\n" + |
490 |
" Runnable r = hierbas.del.litoral.Test :: taragui;\n" + |
491 |
" }\n" + |
492 |
"}\n" |
493 |
); |
494 |
String golden = |
495 |
"package hierbas.del.litoral;\n\n" + |
496 |
"public class Test {\n" + |
497 |
" public static void taragui() {\n" + |
498 |
" Runnable r = Test :: taragui;\n" + |
499 |
" }\n" + |
500 |
"}\n"; |
501 |
JavaSource src = getJavaSource(testFile); |
502 |
|
503 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
504 |
|
505 |
public void run(final WorkingCopy workingCopy) throws IOException { |
506 |
workingCopy.toPhase(Phase.RESOLVED); |
507 |
final TreeMaker make = workingCopy.getTreeMaker(); |
508 |
new TreeScanner<Void, Void>() { |
509 |
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) { |
510 |
workingCopy.rewrite(node, make.MemberReference(node.getMode(), make.Identifier("Test"), node.getName(), node.getTypeArguments())); |
511 |
return super.visitMemberReference(node, p); |
512 |
} |
513 |
}.scan(workingCopy.getCompilationUnit(), null); |
514 |
} |
515 |
|
516 |
}; |
517 |
src.runModificationTask(task).commit(); |
518 |
String res = TestUtilities.copyFileToString(testFile); |
519 |
System.err.println(res); |
520 |
assertEquals(golden, res); |
521 |
} |
522 |
|
523 |
public void testMethodReferenceNameDiff() throws Exception { |
524 |
testFile = new File(getWorkDir(), "Test.java"); |
525 |
TestUtilities.copyStringToFile(testFile, |
526 |
"package hierbas.del.litoral;\n\n" + |
527 |
"public class Test {\n" + |
528 |
" public static void taragui() {\n" + |
529 |
" Runnable r = Test :: taragui;\n" + |
530 |
" }\n" + |
531 |
"}\n" |
532 |
); |
533 |
String golden = |
534 |
"package hierbas.del.litoral;\n\n" + |
535 |
"public class Test {\n" + |
536 |
" public static void taragui() {\n" + |
537 |
" Runnable r = Test :: taragui2;\n" + |
538 |
" }\n" + |
539 |
"}\n"; |
540 |
JavaSource src = getJavaSource(testFile); |
541 |
|
542 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
543 |
|
544 |
public void run(final WorkingCopy workingCopy) throws IOException { |
545 |
workingCopy.toPhase(Phase.RESOLVED); |
546 |
final TreeMaker make = workingCopy.getTreeMaker(); |
547 |
new TreeScanner<Void, Void>() { |
548 |
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) { |
549 |
workingCopy.rewrite(node, make.setLabel(node, "taragui2")); |
550 |
return super.visitMemberReference(node, p); |
551 |
} |
552 |
}.scan(workingCopy.getCompilationUnit(), null); |
553 |
} |
554 |
|
555 |
}; |
556 |
src.runModificationTask(task).commit(); |
557 |
String res = TestUtilities.copyFileToString(testFile); |
558 |
System.err.println(res); |
559 |
assertEquals(golden, res); |
560 |
} |
561 |
|
562 |
public void testMethodReferenceFirstTypeParam() throws Exception { |
563 |
testFile = new File(getWorkDir(), "Test.java"); |
564 |
TestUtilities.copyStringToFile(testFile, |
565 |
"package hierbas.del.litoral;\n\n" + |
566 |
"public class Test {\n" + |
567 |
" public static void taragui() {\n" + |
568 |
" Runnable r = Test::taragui;\n" + |
569 |
" }\n" + |
570 |
"}\n" |
571 |
); |
572 |
String golden = |
573 |
"package hierbas.del.litoral;\n\n" + |
574 |
"public class Test {\n" + |
575 |
" public static void taragui() {\n" + |
576 |
" Runnable r = Test::<String>taragui;\n" + |
577 |
" }\n" + |
578 |
"}\n"; |
579 |
JavaSource src = getJavaSource(testFile); |
580 |
|
581 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
582 |
|
583 |
public void run(final WorkingCopy workingCopy) throws IOException { |
584 |
workingCopy.toPhase(Phase.RESOLVED); |
585 |
final TreeMaker make = workingCopy.getTreeMaker(); |
586 |
new TreeScanner<Void, Void>() { |
587 |
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) { |
588 |
workingCopy.rewrite(node, make.MemberReference(node.getMode(), node.getQualifierExpression(), node.getName(), Collections.singletonList(make.Identifier("String")))); |
589 |
return super.visitMemberReference(node, p); |
590 |
} |
591 |
}.scan(workingCopy.getCompilationUnit(), null); |
592 |
} |
593 |
|
594 |
}; |
595 |
src.runModificationTask(task).commit(); |
596 |
String res = TestUtilities.copyFileToString(testFile); |
597 |
System.err.println(res); |
598 |
assertEquals(golden, res); |
599 |
} |
600 |
|
601 |
public void testMethodReferenceLastTypeParam() throws Exception { |
602 |
testFile = new File(getWorkDir(), "Test.java"); |
603 |
TestUtilities.copyStringToFile(testFile, |
604 |
"package hierbas.del.litoral;\n\n" + |
605 |
"public class Test {\n" + |
606 |
" public static void taragui() {\n" + |
607 |
" Runnable r = Test::<String>taragui;\n" + |
608 |
" }\n" + |
609 |
"}\n" |
610 |
); |
611 |
String golden = |
612 |
"package hierbas.del.litoral;\n\n" + |
613 |
"public class Test {\n" + |
614 |
" public static void taragui() {\n" + |
615 |
" Runnable r = Test::taragui;\n" + |
616 |
" }\n" + |
617 |
"}\n"; |
618 |
JavaSource src = getJavaSource(testFile); |
619 |
|
620 |
Task<WorkingCopy> task = new Task<WorkingCopy>() { |
621 |
|
622 |
public void run(final WorkingCopy workingCopy) throws IOException { |
623 |
workingCopy.toPhase(Phase.RESOLVED); |
624 |
final TreeMaker make = workingCopy.getTreeMaker(); |
625 |
new TreeScanner<Void, Void>() { |
626 |
@Override public Void visitMemberReference(MemberReferenceTree node, Void p) { |
627 |
workingCopy.rewrite(node, make.MemberReference(node.getMode(), node.getQualifierExpression(), node.getName(), null)); |
628 |
return super.visitMemberReference(node, p); |
629 |
} |
630 |
}.scan(workingCopy.getCompilationUnit(), null); |
631 |
} |
632 |
|
633 |
}; |
634 |
src.runModificationTask(task).commit(); |
635 |
String res = TestUtilities.copyFileToString(testFile); |
636 |
System.err.println(res); |
637 |
assertEquals(golden, res); |
638 |
} |
349 |
|
639 |
|
350 |
String getGoldenPckg() { |
640 |
String getGoldenPckg() { |
351 |
return ""; |
641 |
return ""; |