-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
CtClassTest.java
425 lines (352 loc) · 19.1 KB
/
CtClassTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* Copyright (C) 2006-2018 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.test.ctClass;
import java.io.File;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.SpoonTestHelpers;
import spoon.test.ctClass.testclasses.AnonymousClass;
import spoon.test.ctClass.testclasses.Foo;
import spoon.test.ctClass.testclasses.Pozole;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static spoon.test.SpoonTestHelpers.contentEquals;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.buildClass;
import static spoon.testing.utils.ModelUtils.canBeBuilt;
public class CtClassTest {
@Test
public void getConstructor() throws Exception {
final Factory build = build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
assertEquals(3, foo.getConstructors().size());
CtTypeReference<Object> typeString = build.Code().createCtTypeReference(String.class);
CtConstructor<?> constructor = foo.getConstructor(typeString);
assertEquals(typeString, constructor.getParameters().get(0).getType());
CtArrayTypeReference<Object> typeStringArray = build.Core().createArrayTypeReference();
typeStringArray.setComponentType(typeString);
constructor = foo.getConstructor(typeStringArray);
assertEquals(typeStringArray, constructor.getParameters().get(0).getType());
CtArrayTypeReference<Object> typeStringArrayArray = build.Core().createArrayTypeReference();
typeStringArrayArray.setComponentType(typeStringArray);
constructor = foo.getConstructor(typeStringArrayArray);
assertEquals(typeStringArrayArray, constructor.getParameters().get(0).getType());
// contract: one could add a type member that already exists (equals but not same) and modify it afterwards
// this adds some flexibility for client code
// see https://github.com/INRIA/spoon/issues/1862
CtConstructor cons = foo.getConstructors().toArray(new CtConstructor[0])[0].clone();
foo.addConstructor(cons);
int position = foo.getTypeMembers().indexOf(cons);
// as long as we have not changed the signature, getConstructors, which is based on signatures,
// thinks there is one single constructor (and that's OK)
assertEquals(3, foo.getConstructors().size());
cons.addParameter(cons.getFactory().createParameter().setType(cons.getFactory().Type().objectType()));
// now that we have changed the signature we can call getConstructors safely
assertEquals(4, foo.getConstructors().size());
// we cloned the first constructor, so it has the same position, and comes before the 2nd and 3rd constructor
assertSame(cons, foo.getTypeMembers().get(position));
// the parent is set (the core problem described in the issue has been fixed)
assertSame(foo, cons.getParent());
// now we clone and reset the position
CtConstructor cons2 = foo.getConstructors().toArray(new CtConstructor[0])[0].clone();
cons2.setPosition(null);
// adding the constructor, this time, without a position
foo.addConstructor(cons2);
// without position, it has been addded at the end
assertSame(cons2, foo.getTypeMembers().get(4));
// contract: setPositions sets the position of all children
NoSourcePosition position1 = new NoSourcePosition();
cons2.setPositions(position1);
for(CtElement el: cons2.getDirectChildren()) {
assertSame(position1, el.getPosition());
}
}
@Test
public void testParentOfTheEnclosingClassOfStaticClass() {
// contract: When we have a static class which extends a superclass in the classpath,
// the enclosing class don't have a superclass. This is probably a bug in JDT but good
// luck to report a bug about noclasspath in their bugtracker. :)
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/InvariantChecker.java");
launcher.addInputResource("./src/test/resources/noclasspath/FileIO.java");
launcher.addInputResource("./src/test/resources/noclasspath/Daikon.java");
launcher.setSourceOutputDirectory("./target/class");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Object> aClass = launcher.getFactory().Class().get("daikon.tools.InvariantChecker");
final CtType<?> staticClass = aClass.getNestedType("InvariantCheckProcessor");
assertNotNull(staticClass);
assertEquals("InvariantCheckProcessor", staticClass.getSimpleName());
assertNotNull(staticClass.getSuperclass());
assertEquals("daikon.FileIO$Processor", staticClass.getSuperclass().getQualifiedName());
assertNull(aClass.getSuperclass());
canBeBuilt("./target/class", 8, true);
}
@Test
public void testNoClasspathWithSuperClassOfAClassInAnInterface() {
// contract: When we specify a superclass which is declared in an interface and
// where the visibility is okay, we must use it.
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/draw2d");
launcher.setSourceOutputDirectory("./target/draw2d");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Object> aClass = launcher.getFactory().Class().get("org.eclipse.draw2d.parts.ScrollableThumbnail");
final CtType<?> innerClass = aClass.getNestedType("ClickScrollerAndDragTransferrer");
assertEquals("org.eclipse.draw2d.MouseMotionListener$Stub", innerClass.getSuperclass().getQualifiedName());
canBeBuilt("./target/draw2d", 8, true);
}
@Test
public void testAllTypeReferencesToALocalTypeShouldNotStartWithNumber() throws Exception {
// contract: When we have a local type, we should never start with its number. But this
// number isn't rewrite only in the class declaration but in all type references and in
// the constructor.
final CtType<Pozole> aPozole = buildClass(Pozole.class);
final CtClass<?> cook = aPozole.getNestedType("1Cook");
assertEquals("1Cook", cook.getSimpleName());
assertEquals("spoon.test.ctClass.testclasses.Pozole$1Cook", cook.getQualifiedName());
final Set<? extends CtConstructor<?>> constructors = cook.getConstructors();
final String expectedConstructor = "public Cook() {" + System.lineSeparator() + "}";
assertEquals(expectedConstructor, constructors.toArray(new CtConstructor[constructors.size()])[0].toString());
CtLocalVariable m = cook.getMethod("m").getBody().getStatement(0);
assertEquals("final java.lang.Class<Cook> cookClass = Cook.class", m.toString());
CtFieldAccess ac = (CtFieldAccess) m.getAssignment();
assertEquals("class", ac.getVariable().getSimpleName());
assertEquals(true, ac.getTarget() instanceof CtTypeAccess);
// contract: one can call getModifiers on ".class" of "Aclass.class", it does not crash and it returns 0
assertEquals(0, ac.getVariable().getModifiers().size());
Factory factory = aPozole.getFactory();
aPozole.removeModifier(ModifierKind.PUBLIC);
factory.Code().createCodeSnippetStatement(aPozole.toString()).compile();
CtClass internalClass = factory.Core().createClass();
internalClass.setSimpleName("Foo");
cook.getParent(CtBlock.class).addStatement(internalClass);
assertEquals("Foo", internalClass.getSimpleName());
assertEquals("spoon.test.ctClass.testclasses.Pozole$Foo", internalClass.getQualifiedName());
internalClass.addConstructor(factory.Core().createConstructor());
CtConstructor cons = (CtConstructor) internalClass.getConstructors().toArray(new CtConstructor[0])[0];
cons.setBody(factory.Core().createBlock());
CtConstructorCall call = cook.getFactory().Core().createConstructorCall();
call.setExecutable(cons.getReference());
assertEquals(internalClass, internalClass.getReference().getDeclaration());
assertEquals("new Foo()", call.toString());
internalClass.insertAfter(call);
factory.getEnvironment().setAutoImports(true);
factory.Code().createCodeSnippetStatement(aPozole.toString()).compile();
factory.getEnvironment().setAutoImports(false);
factory.Code().createCodeSnippetStatement(aPozole.toString()).compile();
}
@Test
public void testSpoonShouldInferImplicitPackageInNoClasspath() {
// contract: in noClasspath, when a type is used and no import is specified, then Spoon
// should infer that this type is in the same package as the current class.
final Launcher launcher2 = new Launcher();
launcher2.addInputResource("./src/test/resources/noclasspath/issue1293/com/cristal/ircica/applicationcolis/userinterface/fragments/TransporteurFragment.java");
launcher2.getEnvironment().setNoClasspath(true);
launcher2.buildModel();
final CtClass<Object> aClass2 = launcher2.getFactory().Class().get("com.cristal.ircica.applicationcolis.userinterface.fragments.TransporteurFragment");
final String type2 = aClass2.getSuperclass().getQualifiedName();
CtField field = aClass2.getField("transporteurRadioGroup");
assertThat(field.getType().getQualifiedName(), is("android.widget.RadioGroup"));
assertThat(type2, is("com.cristal.ircica.applicationcolis.userinterface.fragments.CompletableFragment"));
}
@Test
public void toStringWithImports() {
String newLine = System.getProperty("line.separator");
final Launcher launcher2 = new Launcher();
launcher2.addInputResource("./src/test/java/spoon/test/ctClass/");
launcher2.getEnvironment().setNoClasspath(true);
launcher2.buildModel();
final CtClass<Object> aClass2 = launcher2.getFactory().Class().get(AnonymousClass.class);
DefaultJavaPrettyPrinter djpp = new DefaultJavaPrettyPrinter(launcher2.getEnvironment());
aClass2.accept(djpp);
// contract: a class can be printed with full context
assertEquals("package spoon.test.ctClass.testclasses;" + newLine +
"/**" + newLine +
" * Created by urli on 11/10/2017." + newLine +
" */" + newLine +
"public class AnonymousClass {" + newLine +
" final int machin = new java.util.Comparator<java.lang.Integer>() {" + newLine +
" @java.lang.Override" + newLine +
" public int compare(java.lang.Integer o1, java.lang.Integer o2) {" + newLine +
" return 0;" + newLine +
" }" + newLine +
" }.compare(1, 2);" + newLine +
"}", aClass2.toStringWithImports());
// contract: a class can be printed with full context in autoimports
aClass2.getFactory().getEnvironment().setAutoImports(true);
assertEquals("package spoon.test.ctClass.testclasses;" + newLine +
"import java.util.Comparator;" + newLine +
"/**" + newLine +
" * Created by urli on 11/10/2017." + newLine +
" */" + newLine +
"public class AnonymousClass {" + newLine +
" final int machin = new Comparator<Integer>() {" + newLine +
" @Override" + newLine +
" public int compare(Integer o1, Integer o2) {" + newLine +
" return 0;" + newLine +
" }" + newLine +
" }.compare(1, 2);" + newLine +
"}", aClass2.toStringWithImports());
// contract: toStringWithImports works with a new class with no position
assertEquals("package foo;" + newLine +
"import java.io.File;" + newLine +
"class Bar extends File {}", launcher2.getFactory().createClass("foo.Bar").setSuperclass(launcher2.getFactory().Type().get(File.class).getReference()).toStringWithImports());
}
@Test
public void testDefaultConstructorAreOk() {
// contract: When we specify a superclass which is declared in an interface and
// where the visibility is okay, we must use it.
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/issue1306");
launcher.setSourceOutputDirectory("./target/issue1306");
launcher.getEnvironment().setNoClasspath(false);
launcher.getEnvironment().setShouldCompile(true);
launcher.getEnvironment().setAutoImports(true);
launcher.run();
final CtClass<Object> aClass = launcher.getFactory().Class().get("spoon.test.ctClass.testclasses.issue1306.internal.BooleanArraysBaseTest");
assertThat(aClass, notNullValue());
canBeBuilt("./target/issue1306", 8, true);
}
@Test
public void testCloneAnonymousClassInvocation() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when not using autoimport
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(false);
launcher.buildModel();
CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();
CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
// The test stops failing if we set the parent below
//newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());
assertFalse(newClassInvocation.toString().isEmpty());
assertFalse(newClassInvocationCloned.toString().isEmpty());
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
@Test
public void testCloneAnonymousClassInvocationWithAutoimports() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when using autoimport
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(true);
launcher.buildModel();
CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();
CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
// The test stops failing if we set the parent below
//newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());
assertFalse(newClassInvocation.toString().isEmpty());
assertFalse(newClassInvocationCloned.toString().isEmpty());
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = 5000L)
public void test_buildParameterizedClass_withTypeParameterUsedInQualifiedName() {
// contract: It should be possible to build a generic class when one of the type parameters
// is used in the qualified name of another type.
//
// See https://github.com/INRIA/spoon/issues/3903
CtClass<?> cls = Launcher.parseClass(
"public class Main<T extends java.util.Map<String, String>> { "
// Here we add a field with type `T.Entry`, i.e. T is used in a qualified type name
+ "T.Entry<String, String> entry; }");
CtField<?> field = cls.getField("entry");
assertThat(field.getType().getQualifiedName(), equalTo("T$Entry"));
assertThat(field.getType().isSimplyQualified(), is(false));
}
@Test
public void testRemoveAnnotation() {
// contract: removeAnnotation returns true after removing an annotation of a class containing a single
// annotation, and returns false when a non existing annotation is tried to be removed
// arrange
CtClass<?> annotatedClass = Launcher.parseClass("@SuppressWarnings(\"unchecked\") class Annotated { }");
assertEquals(1, annotatedClass.getAnnotations().size());
CtAnnotation<?> annotationToBeRemoved = annotatedClass.getAnnotations().get(0);
// act
boolean firstRemovalSuccessful = annotatedClass.removeAnnotation(annotationToBeRemoved);
boolean secondRemovalSuccessful = annotatedClass.removeAnnotation(annotationToBeRemoved);
// assert
assertEquals(0, annotatedClass.getAnnotations().size());
assertTrue(firstRemovalSuccessful);
assertFalse(secondRemovalSuccessful);
}
@org.junit.jupiter.api.Test
void testLocalClassExists() {
// contract: local classes and their members are part of the model
String code = SpoonTestHelpers.wrapLocal(
" class MyClass {\n" +
" private int field = 2;\n" +
" public void doNothing() { }\n" +
" }\n"
);
CtModel model = SpoonTestHelpers.createModelFromString(code, 5);
CtBlock<?> block = SpoonTestHelpers.getBlock(model);
MatcherAssert.assertThat("The local class does not exist in the model", block.getStatements().size(), CoreMatchers.is(1));
CtStatement statement = block.getStatement(0);
Assertions.assertTrue(statement instanceof CtClass<?>);
CtClass<?> clazz = (CtClass<?>) statement;
MatcherAssert.assertThat(clazz.isLocalType(), CoreMatchers.is(true));
MatcherAssert.assertThat(clazz.getSimpleName(), CoreMatchers.is("1MyClass"));
MatcherAssert.assertThat(clazz.getFields().size(), CoreMatchers.is(1));
MatcherAssert.assertThat(clazz.getMethods().size(), CoreMatchers.is(1));
MatcherAssert.assertThat(clazz.getExtendedModifiers(), contentEquals());
}
}