Added
Link Here
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* The Original Software is NetBeans. The Initial Developer of the Original |
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
17 |
* Microsystems, Inc. All Rights Reserved. |
18 |
*/ |
19 |
|
20 |
package org.netbeans.api.java.source; |
21 |
|
22 |
import com.sun.source.tree.ClassTree; |
23 |
import com.sun.source.tree.ExpressionTree; |
24 |
import com.sun.source.tree.MethodTree; |
25 |
import com.sun.source.tree.StatementTree; |
26 |
import com.sun.source.tree.Tree; |
27 |
import com.sun.source.tree.TypeParameterTree; |
28 |
import com.sun.source.tree.VariableTree; |
29 |
import com.sun.source.util.TreePath; |
30 |
import java.beans.PropertyVetoException; |
31 |
import java.io.File; |
32 |
import java.io.IOException; |
33 |
import java.io.OutputStream; |
34 |
import java.util.ArrayList; |
35 |
import java.util.Collections; |
36 |
import java.util.EnumSet; |
37 |
import java.util.List; |
38 |
import java.util.Set; |
39 |
import javax.lang.model.element.ElementKind; |
40 |
import javax.lang.model.element.ExecutableElement; |
41 |
import javax.lang.model.element.Modifier; |
42 |
import javax.lang.model.element.Name; |
43 |
import javax.lang.model.element.TypeElement; |
44 |
import javax.lang.model.element.VariableElement; |
45 |
import javax.lang.model.type.ArrayType; |
46 |
import javax.lang.model.type.DeclaredType; |
47 |
import javax.lang.model.type.TypeKind; |
48 |
import javax.lang.model.type.TypeMirror; |
49 |
import javax.lang.model.util.ElementFilter; |
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.openide.filesystems.FileLock; |
52 |
import org.openide.filesystems.FileObject; |
53 |
import org.openide.filesystems.FileUtil; |
54 |
import org.openide.filesystems.LocalFileSystem; |
55 |
import org.openide.filesystems.Repository; |
56 |
|
57 |
/** |
58 |
* |
59 |
* @author Jan Lahoda, Dusan Balek |
60 |
*/ |
61 |
public class GeneratorUtilitiesTest extends NbTestCase { |
62 |
|
63 |
public GeneratorUtilitiesTest(String testName) { |
64 |
super(testName); |
65 |
} |
66 |
|
67 |
protected void setUp() throws Exception { |
68 |
SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]); |
69 |
} |
70 |
|
71 |
private void writeIntoFile(FileObject file, String what) throws Exception { |
72 |
FileLock lock = file.lock(); |
73 |
OutputStream out = file.getOutputStream(lock); |
74 |
|
75 |
try { |
76 |
out.write(what.getBytes()); |
77 |
} finally { |
78 |
out.close(); |
79 |
lock.releaseLock(); |
80 |
} |
81 |
} |
82 |
|
83 |
public void testInsertFields() throws Exception { |
84 |
performTest("package test;\npublic class Test {\npublic static int i;\nprivate String s;\npublic Test(){\n}\npublic void op(){\n}\nprivate class Nested{\n}\n}\n", |
85 |
new InsertMembersTask(34, EnumSet.of(Modifier.PUBLIC), ElementKind.FIELD), new InsertMemberValidator(2)); |
86 |
} |
87 |
|
88 |
public void testInsertStaticFields() throws Exception { |
89 |
performTest("package test;\npublic class Test {\npublic static int i;\nprivate String s;\npublic Test(){\n}\npublic void op(){\n}\nprivate class Nested{\n}\n}\n", |
90 |
new InsertMembersTask(34, EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), ElementKind.FIELD), new InsertMemberValidator(1)); |
91 |
} |
92 |
|
93 |
public void testInsertConstructors() throws Exception { |
94 |
performTest("package test;\npublic class Test {\npublic static int i;\nprivate String s;\npublic Test(){\n}\npublic void op(){\n}\nprivate class Nested{\n}\n}\n", |
95 |
new InsertMembersTask(34, EnumSet.of(Modifier.PUBLIC), ElementKind.CONSTRUCTOR), new InsertMemberValidator(3)); |
96 |
} |
97 |
|
98 |
public void testInsertMethods() throws Exception { |
99 |
performTest("package test;\npublic class Test {\npublic static int i;\nprivate String s;\npublic Test(){\n}\npublic void op(){\n}\nprivate class Nested{\n}\n}\n", |
100 |
new InsertMembersTask(34, EnumSet.of(Modifier.PUBLIC), ElementKind.METHOD), new InsertMemberValidator(4)); |
101 |
} |
102 |
|
103 |
public void testInsertStaticMethods() throws Exception { |
104 |
performTest("package test;\npublic class Test {\npublic static int i;\nprivate String s;\npublic Test(){\n}\npublic void op(){\n}\nprivate class Nested{\n}\n}\n", |
105 |
new InsertMembersTask(34, EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), ElementKind.METHOD), new InsertMemberValidator(1)); |
106 |
} |
107 |
|
108 |
public void testInsertNestedClasses() throws Exception { |
109 |
performTest("package test;\npublic class Test {\npublic static int i;\nprivate String s;\npublic Test(){\n}\npublic void op(){\n}\nprivate class Nested{\n}\n}\n", |
110 |
new InsertMembersTask(34, EnumSet.of(Modifier.PUBLIC), ElementKind.CLASS), new InsertMemberValidator(5)); |
111 |
} |
112 |
|
113 |
public void testInsertStaticNestedClasses() throws Exception { |
114 |
performTest("package test;\npublic class Test {\npublic static int i;\nprivate String s;\npublic Test(){\n}\npublic void op(){\n}\nprivate class Nested{\n}\n}\n", |
115 |
new InsertMembersTask(34, EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), ElementKind.CLASS), new InsertMemberValidator(4)); |
116 |
} |
117 |
|
118 |
public void testImplementAllAbstractMethods1() throws Exception { |
119 |
performTest("package test;\npublic class Test implements Runnable {\npublic Test(){\n}\n }\n", new AddAllAbstractMethodsTask(54), new RunnableValidator()); |
120 |
} |
121 |
|
122 |
public void testImplementAllAbstractMethods2() throws Exception { |
123 |
performTest("package test;\npublic class Test implements Runnable {\n }\n", new AddAllAbstractMethodsTask(54), new RunnableValidator()); |
124 |
} |
125 |
|
126 |
public void testImplementAllAbstractMethods3() throws Exception { |
127 |
performTest("package test;\npublic class Test implements Runnable {\npublic void testMethod() {\n} }\n", new AddAllAbstractMethodsTask(54), new RunnableValidator()); |
128 |
} |
129 |
|
130 |
public void testImplementAllAbstractMethods4() throws Exception { |
131 |
performTest("package test;\npublic class Test implements Runnable {\npublic Test(){\n}\npublic void testMethod() {\n} }\n", new AddAllAbstractMethodsTask(54), new RunnableValidator()); |
132 |
} |
133 |
|
134 |
public void testImplementAllAbstractMethods5() throws Exception { |
135 |
performTest("package test;import java.util.concurrent.*;\npublic class Test implements Future<String>{\npublic Test(){\n} }\n", new AddAllAbstractMethodsTask(89), new SimpleFutureValidator("java.lang.String")); |
136 |
} |
137 |
|
138 |
public void testImplementAllAbstractMethods6() throws Exception { |
139 |
performTest("package test;import java.util.concurrent.*;\npublic class Test implements Future<java.util.List<? extends java.util.List>>{\npublic Test(){\n} }\n", new AddAllAbstractMethodsTask(123), new FutureValidator() { |
140 |
protected TypeMirror returnType(CompilationInfo info) { |
141 |
return info.getTreeUtilities().parseType("java.util.List<? extends java.util.List>", info.getElements().getTypeElement("test.Test")); |
142 |
} |
143 |
}); |
144 |
} |
145 |
|
146 |
public void testImplementAllAbstractMethods7() throws Exception { |
147 |
performTest("package test;\npublic class Test extends java.util.AbstractList{\npublic Test(){\n} }\n", new AddAllAbstractMethodsTask(64), null); |
148 |
} |
149 |
|
150 |
/** issue #85966 |
151 |
*/ |
152 |
public void testImplementAllAbstractMethods8() throws Exception { |
153 |
performTest("package test;\npublic class Test implements XX {\npublic Test(){\n} }\ninterface XX {\npublic void test(String ... a);}", new AddAllAbstractMethodsTask(42), new Validator() { |
154 |
public void validate(CompilationInfo info) { |
155 |
TypeElement clazz = info.getElements().getTypeElement("test.Test"); |
156 |
ExecutableElement method = ElementFilter.methodsIn(clazz.getEnclosedElements()).get(0); |
157 |
assertTrue(method.isVarArgs()); |
158 |
} |
159 |
}); |
160 |
} |
161 |
|
162 |
public void testImplementAllAbstractMethods9() throws Exception { |
163 |
performTest("package test;\npublic class Test implements java.util.concurrent.ExecutorService {\npublic Test(){\n} }\n", new AddAllAbstractMethodsTask(30), null); |
164 |
} |
165 |
|
166 |
public void testImplementAllAbstractMethodsa() throws Exception { |
167 |
performTest("package test;\npublic class Test implements XX {\npublic Test(){\n} }\ninterface XX {public <T extends java.util.List> void test(T t);}", new AddAllAbstractMethodsTask(30), null); |
168 |
} |
169 |
|
170 |
public void testOverrideMethods1() throws Exception { |
171 |
performTest("package test;\npublic class Test {\npublic Test(){\n}\n }\n", new SimpleOverrideMethodsTask(34), new CloneAndToStringValidator()); |
172 |
} |
173 |
|
174 |
public void testOverrideMethods2() throws Exception { |
175 |
performTest("package test;\npublic class Test {\n }\n", new SimpleOverrideMethodsTask(34), new CloneAndToStringValidator()); |
176 |
} |
177 |
|
178 |
public void testOverrideMethods3() throws Exception { |
179 |
performTest("package test;\npublic class Test {\npublic void testMethod() {\n} }\n", new SimpleOverrideMethodsTask(34), new CloneAndToStringValidator()); |
180 |
} |
181 |
|
182 |
public void testOverrideMethods4() throws Exception { |
183 |
performTest("package test;\npublic class Test {\npublic Test(){\n}\npublic void testMethod() {\n} }\n", new SimpleOverrideMethodsTask(34), new CloneAndToStringValidator()); |
184 |
} |
185 |
|
186 |
public void testOverrideMethods5() throws Exception { |
187 |
performTest("package test;\npublic class Test extends XX<Number> {\npublic Test(){\n} }\nclass XX<T> {\npublic void test(T ... a) {}}", new OverrideMethodsTask(30), new Validator() { |
188 |
public void validate(CompilationInfo info) { |
189 |
TypeElement clazz = info.getElements().getTypeElement("test.Test"); |
190 |
ExecutableElement method = ElementFilter.methodsIn(clazz.getEnclosedElements()).get(0); |
191 |
assertTrue(method.getSimpleName().contentEquals("test")); |
192 |
TypeElement te = info.getElements().getTypeElement("java.lang.Number"); |
193 |
assertEquals(1, method.getParameters().size()); |
194 |
TypeMirror paramType = method.getParameters().get(0).asType(); |
195 |
assertNotNull(paramType); |
196 |
assertTrue(paramType.getKind() == TypeKind.ARRAY); |
197 |
assertTrue(info.getTypes().isSameType(te.asType(), ((ArrayType)paramType).getComponentType())); |
198 |
assertTrue(method.isVarArgs()); |
199 |
} |
200 |
}); |
201 |
} |
202 |
|
203 |
public void testConstructor1() throws Exception { |
204 |
performTest("package test;\npublic class Test {\nprivate int test;\n}\n", new ConstructorTask(34), new ConstructorValidator()); |
205 |
} |
206 |
|
207 |
public void testConstructor2() throws Exception { |
208 |
performTest("package test;\npublic class Test extends XX {\nprivate int test;\n}\nclass XX {\npublic XX(boolean b){\n}\n}\n", new ConstructorTask(30), new ConstructorValidator()); |
209 |
} |
210 |
|
211 |
public void testGetter() throws Exception { |
212 |
performTest("package test;\npublic class Test {\nprivate int test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, true), new GetterSetterValidator(true)); |
213 |
} |
214 |
|
215 |
public void testBooleanGetter() throws Exception { |
216 |
performTest("package test;\npublic class Test {\nprivate boolean test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, true), new GetterSetterValidator(true)); |
217 |
} |
218 |
|
219 |
public void testStaticGetter() throws Exception { |
220 |
performTest("package test;\npublic class Test {\nprivate static int test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, true), new GetterSetterValidator(true)); |
221 |
} |
222 |
|
223 |
public void testStaticBooleanGetter() throws Exception { |
224 |
performTest("package test;\npublic class Test {\nprivate static boolean test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, true), new GetterSetterValidator(true)); |
225 |
} |
226 |
|
227 |
public void testSetter() throws Exception { |
228 |
performTest("package test;\npublic class Test {\nprivate int test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, false), new GetterSetterValidator(false)); |
229 |
} |
230 |
|
231 |
public void testBooleanSetter() throws Exception { |
232 |
performTest("package test;\npublic class Test {\nprivate boolean test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, false), new GetterSetterValidator(false)); |
233 |
} |
234 |
|
235 |
public void testStaticSetter() throws Exception { |
236 |
performTest("package test;\npublic class Test {\nprivate static int test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, false), new GetterSetterValidator(false)); |
237 |
} |
238 |
|
239 |
public void testStaticBooleanSetter() throws Exception { |
240 |
performTest("package test;\npublic class Test {\nprivate static boolean test;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, false), new GetterSetterValidator(false)); |
241 |
} |
242 |
|
243 |
public static interface Validator { |
244 |
|
245 |
public void validate(CompilationInfo info); |
246 |
|
247 |
} |
248 |
|
249 |
private static class InsertMembersTask implements CancellableTask<WorkingCopy> { |
250 |
|
251 |
private int offset; |
252 |
private Set<Modifier> modifiers; |
253 |
private ElementKind kind; |
254 |
|
255 |
public InsertMembersTask(int offset, Set<Modifier> modifiers, ElementKind kind) { |
256 |
this.offset = offset; |
257 |
this.modifiers = modifiers; |
258 |
this.kind = kind; |
259 |
} |
260 |
|
261 |
public void cancel() { |
262 |
} |
263 |
|
264 |
public void run(WorkingCopy copy) throws Exception { |
265 |
copy.toPhase(JavaSource.Phase.RESOLVED); |
266 |
TreePath tp = copy.getTreeUtilities().pathFor(offset); |
267 |
assertTrue(tp.getLeaf().getKind() == Tree.Kind.CLASS); |
268 |
ClassTree ct = (ClassTree)tp.getLeaf(); |
269 |
GeneratorUtilities utilities = GeneratorUtilities.get(copy); |
270 |
assertNotNull(utilities); |
271 |
TreeMaker maker = copy.getTreeMaker(); |
272 |
ArrayList<Tree> members = new ArrayList<Tree>(1); |
273 |
switch(kind) { |
274 |
case FIELD: |
275 |
members.add(maker.Variable(maker.Modifiers(modifiers), "test", maker.PrimitiveType(TypeKind.INT), null)); |
276 |
break; |
277 |
case METHOD: |
278 |
members.add(maker.Method(maker.Modifiers(modifiers), "test", maker.PrimitiveType(TypeKind.VOID), |
279 |
Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), |
280 |
Collections.<ExpressionTree>emptyList(), maker.Block(Collections.<StatementTree>emptyList(), false), |
281 |
null)); |
282 |
break; |
283 |
case CONSTRUCTOR: |
284 |
members.add(maker.Method(maker.Modifiers(modifiers), "<init>", null, |
285 |
Collections.<TypeParameterTree>emptyList(), Collections.singletonList(maker.Variable( |
286 |
maker.Modifiers(EnumSet.noneOf(Modifier.class)), "s", maker.Identifier("String"), null)), |
287 |
Collections.<ExpressionTree>emptyList(), maker.Block(Collections.<StatementTree>emptyList(), false), |
288 |
null)); |
289 |
break; |
290 |
case CLASS: |
291 |
members.add(maker.Class(maker.Modifiers(modifiers), "test", Collections.<TypeParameterTree>emptyList(), |
292 |
null, Collections.<ExpressionTree>emptyList(), Collections.<Tree>emptyList())); |
293 |
break; |
294 |
} |
295 |
ClassTree newCt = utilities.insertClassMembers(ct, members); |
296 |
copy.rewrite(ct, newCt); |
297 |
} |
298 |
} |
299 |
|
300 |
private final class InsertMemberValidator implements Validator { |
301 |
|
302 |
private int testIdx; |
303 |
|
304 |
public InsertMemberValidator(int testIdx) { |
305 |
this.testIdx = testIdx; |
306 |
} |
307 |
|
308 |
public void validate(CompilationInfo info) { |
309 |
TypeElement test = info.getElements().getTypeElement("test.Test"); |
310 |
ClassTree ct = info.getTrees().getTree(test); |
311 |
assertNotNull(ct); |
312 |
|
313 |
int foundTestIdx = -1; |
314 |
|
315 |
int idx = 0; |
316 |
for (Tree t : ct.getMembers()) { |
317 |
Name name = null; |
318 |
switch(t.getKind()) { |
319 |
case VARIABLE: |
320 |
name = ((VariableTree)t).getName(); |
321 |
break; |
322 |
case METHOD: |
323 |
name = ((MethodTree)t).getName(); |
324 |
break; |
325 |
case CLASS: |
326 |
name = ((ClassTree)t).getSimpleName(); |
327 |
break; |
328 |
} |
329 |
if (name != null) { |
330 |
if (name.contentEquals("test")) { |
331 |
assertEquals(-1, foundTestIdx); |
332 |
foundTestIdx = idx; |
333 |
} else if (name.contentEquals("<init>") && ((MethodTree)t).getParameters().size() > 0) { |
334 |
assertEquals(-1, foundTestIdx); |
335 |
foundTestIdx = idx; |
336 |
} |
337 |
} |
338 |
idx++; |
339 |
} |
340 |
|
341 |
assertEquals(testIdx, foundTestIdx); |
342 |
} |
343 |
|
344 |
} |
345 |
|
346 |
private static class AddAllAbstractMethodsTask implements CancellableTask<WorkingCopy> { |
347 |
|
348 |
private int offset; |
349 |
|
350 |
public AddAllAbstractMethodsTask(int offset) { |
351 |
this.offset = offset; |
352 |
} |
353 |
|
354 |
public void cancel() { |
355 |
} |
356 |
|
357 |
public void run(WorkingCopy copy) throws Exception { |
358 |
copy.toPhase(JavaSource.Phase.RESOLVED); |
359 |
TreePath tp = copy.getTreeUtilities().pathFor(offset); |
360 |
assertTrue(tp.getLeaf().getKind() == Tree.Kind.CLASS); |
361 |
ClassTree ct = (ClassTree)tp.getLeaf(); |
362 |
TypeElement te = (TypeElement)copy.getTrees().getElement(tp); |
363 |
assertNotNull(te); |
364 |
GeneratorUtilities utilities = GeneratorUtilities.get(copy); |
365 |
assertNotNull(utilities); |
366 |
ClassTree newCt = utilities.insertClassMembers(ct, utilities.createAllAbstractMethodImplementations(te)); |
367 |
copy.rewrite(ct, newCt); |
368 |
} |
369 |
} |
370 |
|
371 |
private final class RunnableValidator implements Validator { |
372 |
|
373 |
public void validate(CompilationInfo info) { |
374 |
TypeElement test = info.getElements().getTypeElement("test.Test"); |
375 |
|
376 |
boolean foundRunMethod = false; |
377 |
|
378 |
for (ExecutableElement ee : ElementFilter.methodsIn(test.getEnclosedElements())) { |
379 |
if ("run".equals(ee.getSimpleName().toString())) { |
380 |
if (ee.getParameters().isEmpty()) { |
381 |
assertFalse(foundRunMethod); |
382 |
foundRunMethod = true; |
383 |
} |
384 |
} |
385 |
} |
386 |
|
387 |
assertTrue(foundRunMethod); |
388 |
} |
389 |
|
390 |
} |
391 |
|
392 |
private final class SimpleFutureValidator extends FutureValidator { |
393 |
|
394 |
private String returnTypeName; |
395 |
|
396 |
public SimpleFutureValidator(String returnTypeName) { |
397 |
this.returnTypeName = returnTypeName; |
398 |
} |
399 |
|
400 |
protected TypeMirror returnType(CompilationInfo info) { |
401 |
TypeElement returnTypeElement = info.getElements().getTypeElement(returnTypeName); |
402 |
|
403 |
return returnTypeElement.asType(); |
404 |
} |
405 |
} |
406 |
|
407 |
private abstract class FutureValidator implements Validator { |
408 |
|
409 |
protected abstract TypeMirror returnType(CompilationInfo info); |
410 |
|
411 |
public void validate(CompilationInfo info) { |
412 |
TypeElement test = info.getElements().getTypeElement("test.Test"); |
413 |
TypeMirror returnType = returnType(info); |
414 |
|
415 |
boolean hasShortGet = false; |
416 |
boolean hasLongGet = false; |
417 |
|
418 |
for (ExecutableElement ee : ElementFilter.methodsIn(test.getEnclosedElements())) { |
419 |
if ("get".equals(ee.getSimpleName().toString())) { |
420 |
if (ee.getParameters().isEmpty()) { |
421 |
assertFalse(hasShortGet); |
422 |
assertTrue(info.getTypes().isSameType(returnType, ee.getReturnType())); |
423 |
hasShortGet = true; |
424 |
} |
425 |
if (ee.getParameters().size() == 2) { |
426 |
assertFalse(hasLongGet); |
427 |
assertTrue(info.getTypes().isSameType(returnType, ee.getReturnType())); |
428 |
hasLongGet = true; |
429 |
} |
430 |
} |
431 |
} |
432 |
|
433 |
assertTrue(hasShortGet); |
434 |
assertTrue(hasLongGet); |
435 |
} |
436 |
|
437 |
} |
438 |
|
439 |
private static class SimpleOverrideMethodsTask implements CancellableTask<WorkingCopy> { |
440 |
|
441 |
private int offset; |
442 |
|
443 |
public SimpleOverrideMethodsTask(int offset) { |
444 |
this.offset = offset; |
445 |
} |
446 |
|
447 |
public void cancel() { |
448 |
} |
449 |
|
450 |
public void run(WorkingCopy copy) throws Exception { |
451 |
copy.toPhase(JavaSource.Phase.RESOLVED); |
452 |
TreePath tp = copy.getTreeUtilities().pathFor(offset); |
453 |
assertTrue(tp.getLeaf().getKind() == Tree.Kind.CLASS); |
454 |
ClassTree ct = (ClassTree)tp.getLeaf(); |
455 |
TypeElement te = (TypeElement)copy.getTrees().getElement(tp); |
456 |
assertNotNull(te); |
457 |
ArrayList<ExecutableElement> methods = new ArrayList<ExecutableElement>(2); |
458 |
TypeElement object = copy.getElements().getTypeElement("java.lang.Object"); |
459 |
assertNotNull(object); |
460 |
for (ExecutableElement method : ElementFilter.methodsIn(object.getEnclosedElements())) { |
461 |
if (method.getSimpleName().contentEquals("clone")) |
462 |
methods.add(method); |
463 |
else if (method.getSimpleName().contentEquals("toString")) |
464 |
methods.add(method); |
465 |
} |
466 |
GeneratorUtilities utilities = GeneratorUtilities.get(copy); |
467 |
assertNotNull(utilities); |
468 |
ClassTree newCt = utilities.insertClassMembers(ct, utilities.createOverridingMethods(te, methods)); |
469 |
copy.rewrite(ct, newCt); |
470 |
} |
471 |
} |
472 |
|
473 |
private static class OverrideMethodsTask implements CancellableTask<WorkingCopy> { |
474 |
|
475 |
private int offset; |
476 |
|
477 |
public OverrideMethodsTask(int offset) { |
478 |
this.offset = offset; |
479 |
} |
480 |
|
481 |
public void cancel() { |
482 |
} |
483 |
|
484 |
public void run(WorkingCopy copy) throws Exception { |
485 |
copy.toPhase(JavaSource.Phase.RESOLVED); |
486 |
TreePath tp = copy.getTreeUtilities().pathFor(offset); |
487 |
assertTrue(tp.getLeaf().getKind() == Tree.Kind.CLASS); |
488 |
ClassTree ct = (ClassTree)tp.getLeaf(); |
489 |
TypeElement te = (TypeElement)copy.getTrees().getElement(tp); |
490 |
assertNotNull(te); |
491 |
ArrayList<ExecutableElement> methods = new ArrayList<ExecutableElement>(1); |
492 |
TypeElement sup = (TypeElement)((DeclaredType)te.getSuperclass()).asElement(); |
493 |
assertNotNull(sup); |
494 |
for (ExecutableElement method : ElementFilter.methodsIn(sup.getEnclosedElements())) { |
495 |
if (method.getSimpleName().contentEquals("test")) |
496 |
methods.add(method); |
497 |
} |
498 |
GeneratorUtilities utilities = GeneratorUtilities.get(copy); |
499 |
assertNotNull(utilities); |
500 |
ClassTree newCt = utilities.insertClassMembers(ct, utilities.createOverridingMethods(te, methods)); |
501 |
copy.rewrite(ct, newCt); |
502 |
} |
503 |
} |
504 |
|
505 |
private final class CloneAndToStringValidator implements Validator { |
506 |
|
507 |
public void validate(CompilationInfo info) { |
508 |
TypeElement test = info.getElements().getTypeElement("test.Test"); |
509 |
|
510 |
boolean foundCloneMethod = false; |
511 |
boolean foundToStringMethod = false; |
512 |
|
513 |
for (ExecutableElement ee : ElementFilter.methodsIn(test.getEnclosedElements())) { |
514 |
if (ee.getSimpleName().contentEquals("clone")) { |
515 |
if (ee.getParameters().isEmpty()) { |
516 |
assertFalse(foundCloneMethod); |
517 |
foundCloneMethod = true; |
518 |
} |
519 |
} else if (ee.getSimpleName().contentEquals("toString")) { |
520 |
if (ee.getParameters().isEmpty()) { |
521 |
assertFalse(foundToStringMethod); |
522 |
foundToStringMethod = true; |
523 |
} |
524 |
} |
525 |
} |
526 |
|
527 |
assertTrue(foundCloneMethod); |
528 |
assertTrue(foundToStringMethod); |
529 |
} |
530 |
|
531 |
} |
532 |
|
533 |
private static class ConstructorTask implements CancellableTask<WorkingCopy> { |
534 |
|
535 |
private int offset; |
536 |
|
537 |
public ConstructorTask(int offset) { |
538 |
this.offset = offset; |
539 |
} |
540 |
|
541 |
public void cancel() { |
542 |
} |
543 |
|
544 |
public void run(WorkingCopy copy) throws Exception { |
545 |
copy.toPhase(JavaSource.Phase.RESOLVED); |
546 |
TreePath tp = copy.getTreeUtilities().pathFor(offset); |
547 |
assertTrue(tp.getLeaf().getKind() == Tree.Kind.CLASS); |
548 |
ClassTree ct = (ClassTree)tp.getLeaf(); |
549 |
TypeElement te = (TypeElement)copy.getTrees().getElement(tp); |
550 |
assertNotNull(te); |
551 |
List<? extends VariableElement> vars = ElementFilter.fieldsIn(te.getEnclosedElements()); |
552 |
TypeElement sup = (TypeElement)((DeclaredType)te.getSuperclass()).asElement(); |
553 |
assertNotNull(sup); |
554 |
List<? extends ExecutableElement> ctors = sup.getQualifiedName().contentEquals("java.lang.Object") |
555 |
? null : ElementFilter.constructorsIn(sup.getEnclosedElements()); |
556 |
if (ctors != null) |
557 |
assertEquals(1, ctors.size()); |
558 |
GeneratorUtilities utilities = GeneratorUtilities.get(copy); |
559 |
assertNotNull(utilities); |
560 |
ClassTree newCt = utilities.insertClassMember(ct, utilities.createConstructor(te, vars, ctors != null ? ctors.get(0) : null)); |
561 |
copy.rewrite(ct, newCt); |
562 |
} |
563 |
} |
564 |
|
565 |
private final class ConstructorValidator implements Validator { |
566 |
|
567 |
public void validate(CompilationInfo info) { |
568 |
TypeElement test = info.getElements().getTypeElement("test.Test"); |
569 |
VariableElement var = ElementFilter.fieldsIn(test.getEnclosedElements()).get(0); |
570 |
TypeElement sup = (TypeElement)((DeclaredType)test.getSuperclass()).asElement(); |
571 |
ExecutableElement supCtor = sup.getQualifiedName().contentEquals("java.lang.Object") |
572 |
? null : ElementFilter.constructorsIn(sup.getEnclosedElements()).get(0); |
573 |
|
574 |
List<? extends ExecutableElement> ctors = ElementFilter.constructorsIn(test.getEnclosedElements()); |
575 |
assertEquals(1, ctors.size()); |
576 |
ExecutableElement ctor = ctors.get(0); |
577 |
|
578 |
assertEquals(supCtor == null ? 1 : 2, ctor.getParameters().size()); |
579 |
} |
580 |
|
581 |
} |
582 |
|
583 |
private static class GetterSetterTask implements CancellableTask<WorkingCopy> { |
584 |
|
585 |
private int offset; |
586 |
private boolean getter; |
587 |
|
588 |
public GetterSetterTask(int offset, boolean getter) { |
589 |
this.offset = offset; |
590 |
this.getter = getter; |
591 |
} |
592 |
|
593 |
public void cancel() { |
594 |
} |
595 |
|
596 |
public void run(WorkingCopy copy) throws Exception { |
597 |
copy.toPhase(JavaSource.Phase.RESOLVED); |
598 |
TreePath tp = copy.getTreeUtilities().pathFor(offset); |
599 |
assertTrue(tp.getLeaf().getKind() == Tree.Kind.CLASS); |
600 |
ClassTree ct = (ClassTree)tp.getLeaf(); |
601 |
TypeElement te = (TypeElement)copy.getTrees().getElement(tp); |
602 |
assertNotNull(te); |
603 |
List<? extends VariableElement> vars = ElementFilter.fieldsIn(te.getEnclosedElements()); |
604 |
assertEquals(1, vars.size()); |
605 |
GeneratorUtilities utilities = GeneratorUtilities.get(copy); |
606 |
assertNotNull(utilities); |
607 |
ClassTree newCt = utilities.insertClassMember(ct, getter |
608 |
? utilities.createGetter(te, vars.get(0)) |
609 |
: utilities.createSetter(te, vars.get(0))); |
610 |
copy.rewrite(ct, newCt); |
611 |
} |
612 |
} |
613 |
|
614 |
private final class GetterSetterValidator implements Validator { |
615 |
|
616 |
private boolean getter; |
617 |
|
618 |
public GetterSetterValidator(boolean getter) { |
619 |
this.getter = getter; |
620 |
} |
621 |
|
622 |
public void validate(CompilationInfo info) { |
623 |
TypeElement test = info.getElements().getTypeElement("test.Test"); |
624 |
VariableElement var = ElementFilter.fieldsIn(test.getEnclosedElements()).get(0); |
625 |
|
626 |
List<? extends ExecutableElement> methods = ElementFilter.methodsIn(test.getEnclosedElements()); |
627 |
assertEquals(1, methods.size()); |
628 |
ExecutableElement method = methods.get(0); |
629 |
|
630 |
TypeMirror type = info.getTypes().asMemberOf((DeclaredType)test.asType(), var); |
631 |
if (getter) { |
632 |
assertTrue(info.getTypes().isSameType(type, method.getReturnType())); |
633 |
assertEquals(type.getKind() == TypeKind.BOOLEAN ? "isTest" : "getTest", method.getSimpleName().toString()); |
634 |
assertEquals(0, method.getParameters().size()); |
635 |
} else { |
636 |
assertTrue(info.getTypes().isSameType(info.getTypes().getNoType(TypeKind.VOID), method.getReturnType())); |
637 |
assertEquals("setTest", method.getSimpleName().toString()); |
638 |
assertEquals(1, method.getParameters().size()); |
639 |
assertTrue(info.getTypes().isSameType(type, method.getParameters().get(0).asType())); |
640 |
} |
641 |
} |
642 |
|
643 |
} |
644 |
|
645 |
private void performTest(String sourceCode, final CancellableTask<WorkingCopy> task, final Validator validator) throws Exception { |
646 |
FileObject root = makeScratchDir(this); |
647 |
|
648 |
FileObject sourceDir = root.createFolder("src"); |
649 |
FileObject buildDir = root.createFolder("build"); |
650 |
FileObject cacheDir = root.createFolder("cache"); |
651 |
|
652 |
FileObject source = sourceDir.createFolder("test").createData("Test.java"); |
653 |
|
654 |
writeIntoFile(source, sourceCode); |
655 |
|
656 |
SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir, new FileObject[0]); |
657 |
|
658 |
JavaSource js = JavaSource.forFileObject(source); |
659 |
|
660 |
ModificationResult result = js.runModificationTask(task); |
661 |
|
662 |
result.commit(); |
663 |
|
664 |
js.runUserActionTask(new CancellableTask<CompilationController>() { |
665 |
public void cancel() { |
666 |
} |
667 |
public void run(CompilationController controller) throws Exception { |
668 |
System.err.println("text:"); |
669 |
System.err.println(controller.getText()); |
670 |
controller.toPhase(JavaSource.Phase.RESOLVED); |
671 |
|
672 |
assertEquals(controller.getDiagnostics().toString(), 0, controller.getDiagnostics().size()); |
673 |
|
674 |
if (validator != null) |
675 |
validator.validate(controller); |
676 |
} |
677 |
}, true); |
678 |
} |
679 |
|
680 |
/**Copied from org.netbeans.api.project. |
681 |
* Create a scratch directory for tests. |
682 |
* Will be in /tmp or whatever, and will be empty. |
683 |
* If you just need a java.io.File use clearWorkDir + getWorkDir. |
684 |
*/ |
685 |
public static FileObject makeScratchDir(NbTestCase test) throws IOException { |
686 |
test.clearWorkDir(); |
687 |
File root = test.getWorkDir(); |
688 |
assert root.isDirectory() && root.list().length == 0; |
689 |
FileObject fo = FileUtil.toFileObject(root); |
690 |
if (fo != null) { |
691 |
// Presumably using masterfs. |
692 |
return fo; |
693 |
} else { |
694 |
// For the benefit of those not using masterfs. |
695 |
LocalFileSystem lfs = new LocalFileSystem(); |
696 |
try { |
697 |
lfs.setRootDirectory(root); |
698 |
} catch (PropertyVetoException e) { |
699 |
assert false : e; |
700 |
} |
701 |
Repository.getDefault().addFileSystem(lfs); |
702 |
return lfs.getRoot(); |
703 |
} |
704 |
} |
705 |
|
706 |
} |