use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class CommentTest method testBlockComment.
@Test
public void testBlockComment() {
Factory f = getSpoonFactory();
CtClass<?> type = (CtClass<?>) f.Type().get(BlockComment.class);
String strType = type.toString();
List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
// verify that the number of comment present in the AST is correct
assertEquals(51, comments.size());
// verify that all comments present in the AST is printed
for (CtComment comment : comments) {
if (comment.getCommentType() == CtComment.CommentType.FILE) {
// the header of the file is not printed with the toString
continue;
}
assertNotNull(comment.getParent());
assertTrue(comment.toString() + ":" + comment.getParent() + " is not printed", strType.contains(comment.toString()));
}
assertEquals(4, type.getComments().size());
assertEquals(createFakeBlockComment(f, "comment class"), type.getComments().get(1));
CtField<?> field = type.getField("field");
assertEquals(2, field.getComments().size());
assertEquals(createFakeBlockComment(f, "Comment Field"), field.getComments().get(0));
assertEquals("/* Comment Field */" + newLine + "/* comment in field */" + newLine + "private int field = 10;", field.toString());
CtAnonymousExecutable ctAnonymousExecutable = type.getAnonymousExecutables().get(0);
assertEquals(1, ctAnonymousExecutable.getComments().size());
assertEquals(createFakeBlockComment(f, "comment static block"), ctAnonymousExecutable.getComments().get(0));
assertEquals(createFakeBlockComment(f, "comment inside static"), ctAnonymousExecutable.getBody().getStatement(0));
assertEquals("/* comment static block */" + newLine + "static {" + newLine + " /* comment inside static */" + newLine + "}", ctAnonymousExecutable.toString());
CtConstructor constructor = type.getConstructor();
assertEquals(1, constructor.getComments().size());
assertEquals(createFakeBlockComment(f, "comment constructor"), constructor.getComments().get(0));
// index 0 is the implicit super call
assertEquals(createFakeBlockComment(f, "Comment in constructor"), constructor.getBody().getStatement(1));
assertEquals("/* comment constructor */" + newLine + "public BlockComment() {" + newLine + " /* Comment in constructor */" + newLine + "}", constructor.toString());
CtMethod<Object> m = type.getMethod("m");
assertEquals(1, m.getComments().size());
assertEquals(createFakeBlockComment(f, "comment method"), m.getComments().get(0));
assertEquals(createFakeBlockComment(f, "comment empty method block"), m.getBody().getStatement(0));
assertEquals("/* comment method */" + newLine + "public void m() {" + newLine + " /* comment empty method block */" + newLine + "}", m.toString());
CtMethod<Object> m1 = type.getMethod("m1");
CtSwitch ctSwitch = m1.getBody().getStatement(0);
assertEquals(createFakeBlockComment(f, "comment switch"), ctSwitch.getComments().get(0));
assertEquals("/* comment switch */" + newLine + "switch (1) {" + newLine + " /* before first case */" + newLine + " case 0 :" + newLine + " /* comment case 0: empty case */" + newLine + " case 1 :" + newLine + " /* comment case 1 */" + newLine + " int i = 0;" + newLine + " default :" + newLine + " /* comment default */" + newLine + "}", ctSwitch.toString());
CtFor ctFor = m1.getBody().getStatement(1);
assertEquals(createFakeBlockComment(f, "comment for"), ctFor.getComments().get(0));
assertEquals("/* comment for */" + newLine + "for (int i = 0; i < 10; i++) {" + newLine + " /* comment for block */" + newLine + "}", ctFor.toString());
CtIf ctIf = m1.getBody().getStatement(2);
assertEquals(createFakeBlockComment(f, "comment if"), ctIf.getComments().get(0));
assertEquals("/* comment if */" + newLine + "if ((1 % 2) == 0) {" + newLine + " /* comment unary operator */" + newLine + " (field)++;" + newLine + "}", ctIf.toString());
CtConstructorCall ctConstructorCall = m1.getBody().getStatement(3);
assertEquals(createFakeBlockComment(f, "comment constructor call"), ctConstructorCall.getComments().get(0));
assertEquals("/* comment constructor call */" + newLine + "new spoon.test.comment.testclasses.BlockComment()", ctConstructorCall.toString());
CtInvocation ctInvocation = m1.getBody().getStatement(4);
assertEquals(createFakeBlockComment(f, "comment invocation"), ctInvocation.getComments().get(0));
assertEquals("/* comment invocation */" + newLine + "this.m()", ctInvocation.toString());
CtLocalVariable ctLocalVariable = m1.getBody().getStatement(5);
assertEquals(createFakeBlockComment(f, "comment local variable"), ctLocalVariable.getComments().get(0));
assertEquals("/* comment local variable */" + newLine + "int i = 0", ctLocalVariable.toString());
CtLocalVariable ctLocalVariable2 = m1.getBody().getStatement(6);
assertEquals(createFakeBlockComment(f, "comment multi assignments"), ctLocalVariable2.getComments().get(0));
assertEquals("/* comment multi assignments */" + newLine + "int j = 2", ctLocalVariable2.toString());
CtDo ctDo = m1.getBody().getStatement(7);
assertEquals(createFakeBlockComment(f, "comment dowhile"), ctDo.getComments().get(0));
assertEquals("/* comment dowhile */" + newLine + "do {" + newLine + " /* comment in do while */" + newLine + " i++;" + newLine + " /* comment end do while */" + newLine + "} while (i < 10 )", ctDo.toString());
CtTry ctTry = m1.getBody().getStatement(8);
assertEquals(createFakeBlockComment(f, "comment try"), ctTry.getComments().get(0));
assertEquals("/* comment try */" + newLine + "try {" + newLine + " /* comment in try */" + newLine + " i++;" + newLine + "} catch (java.lang.Exception e) {" + newLine + " /* comment in catch */" + newLine + "}", ctTry.toString());
CtSynchronized ctSynchronized = m1.getBody().getStatement(9);
assertEquals(createFakeBlockComment(f, "comment synchronized"), ctSynchronized.getComments().get(0));
assertEquals("/* comment synchronized */" + newLine + "synchronized(this) {" + newLine + " /* comment in synchronized */" + newLine + "}", ctSynchronized.toString());
CtReturn ctReturn = m1.getBody().getStatement(10);
assertEquals(createFakeBlockComment(f, "comment return"), ctReturn.getComments().get(0));
assertEquals("/* comment return */" + newLine + "return", ctReturn.toString());
CtMethod m2 = type.getMethodsByName("m2").get(0);
assertEquals(6, m2.getComments().size());
CtParameter ctParameter = (CtParameter) m2.getParameters().get(0);
assertEquals(4, ctParameter.getComments().size());
assertEquals("/* comment before type */" + newLine + "/* comment after parameter */" + newLine + "/* comment before throws */" + newLine + "/* comment before exception 1 */" + newLine + "/* comment before exception 2 */" + newLine + "/* comment before block */" + newLine + "public void m2(/* comment before name */" + newLine + "/* comment before parameters */" + newLine + "/* comment before type parameter */" + newLine + "/* comment before name parameter */" + newLine + "int i) throws java.lang.Error, java.lang.Exception {" + newLine + "}", m2.toString());
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class ExecutableRefTest method testOverridingMethod.
@Test
public void testOverridingMethod() throws Exception {
final CtType<Pozole> aPozole = ModelUtils.buildClass(Pozole.class);
final CtExecutableReference<?> run = aPozole.getMethodsByName("run").get(0).getReference();
final List<CtInvocation<?>> elements = Query.getElements(run.getFactory(), new InvocationFilter(run));
assertEquals(1, elements.size());
assertEquals(run, elements.get(0).getExecutable());
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class FilterTest method testInvocationFilterWithExecutableInLibrary.
@Test
public void testInvocationFilterWithExecutableInLibrary() throws Exception {
// contract: When we have an invocation of an executable declared in a library,
// we can filter it and get the executable of the invocation.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtInvocation<?> invSize = aTacos.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
if (element.getExecutable() == null) {
return false;
}
return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
final List<CtInvocation<?>> invocations = aTacos.getElements(new InvocationFilter(invSize.getExecutable()));
assertEquals(1, invocations.size());
final CtInvocation<?> expectedInv = invocations.get(0);
assertNotNull(expectedInv);
final CtExecutableReference<?> expectedExecutable = expectedInv.getExecutable();
assertNotNull(expectedExecutable);
assertEquals("size", expectedExecutable.getSimpleName());
assertNull(expectedExecutable.getDeclaration());
CtExecutable<?> exec = expectedExecutable.getExecutableDeclaration();
assertEquals("size", exec.getSimpleName());
assertEquals("ArrayList", ((CtClass) exec.getParent()).getSimpleName());
final CtExecutable<?> declaration = expectedExecutable.getExecutableDeclaration();
assertNotNull(declaration);
assertEquals("size", declaration.getSimpleName());
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class GenericsTest method testInvocationGenerics.
@Test
public void testInvocationGenerics() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtConstructor<?> defaultConstructor = aTacos.getConstructor();
final CtInvocation<?> explicitConstructorCall = (CtInvocation<?>) defaultConstructor.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(1, explicitConstructorCall.getExecutable().getActualTypeArguments().size());
assertEquals("<java.lang.String>this(1)", explicitConstructorCall.toString());
final CtMethod<?> m = aTacos.getMethodsByName("m2").get(0);
final CtInvocation invocation1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(1, invocation1.getExecutable().getActualTypeArguments().size());
assertEquals("this.<java.lang.String>makeTacos(null)", invocation1.toString());
final CtInvocation invocation2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(0, invocation2.getExecutable().getActualTypeArguments().size());
assertEquals("this.makeTacos(null)", invocation2.toString());
canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class GenericsTest method testMethodTypingContext.
@Test
public void testMethodTypingContext() throws Exception {
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
CtMethod<?> trWeddingLunch_eatMe = ctClassWeddingLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
MethodTypingContext methodSTH = new MethodTypingContext().setMethod(trWeddingLunch_eatMe);
// contract: the method typing context provides its scope
assertSame(trWeddingLunch_eatMe, methodSTH.getAdaptationScope());
CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
CtInvocation<?> invokeReserve = factory.Class().get(CelebrationLunch.class).filterChildren(new TypeFilter<>(CtInvocation.class)).select((CtInvocation i) -> "reserve".equals(i.getExecutable().getSimpleName())).first();
MethodTypingContext methodReserveTC = new MethodTypingContext().setInvocation(invokeReserve);
// contract: the method typing context provides its scope
assertSame(invokeReserve.getExecutable().getDeclaration(), methodReserveTC.getAdaptationScope());
// check that MethodTypingContext made from invocation knows actual type arguments of method and all declaring types
// 1) check method actual type argument
CtMethod<?> methodReserve = (CtMethod<?>) invokeReserve.getExecutable().getDeclaration();
CtTypeParameter methodReserve_S = methodReserve.getFormalCtTypeParameters().get(0);
assertEquals("S", methodReserve_S.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(methodReserve_S).getQualifiedName());
// 2) check actual type arguments of declaring type `Section`
CtClass classSection = (CtClass) methodReserve.getDeclaringType();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch$Section", classSection.getQualifiedName());
CtTypeParameter classSection_Y = classSection.getFormalCtTypeParameters().get(0);
assertEquals("Y", classSection_Y.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classSection_Y).getQualifiedName());
// 3) check actual type arguments of declaring type `WeddingLunch`
CtClass classWeddingLunch = (CtClass) classSection.getDeclaringType();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch", classWeddingLunch.getQualifiedName());
CtTypeParameter classWeddingLunch_X = classWeddingLunch.getFormalCtTypeParameters().get(0);
assertEquals("X", classWeddingLunch_X.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classWeddingLunch_X).getQualifiedName());
// 4) check actual type arguments of declaring type `CelebrationLunch`
CtClass classCelebrationLunch = (CtClass) classWeddingLunch.getDeclaringType();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch", classCelebrationLunch.getQualifiedName());
CtTypeParameter classCelebrationLunch_K = classCelebrationLunch.getFormalCtTypeParameters().get(0);
CtTypeParameter classCelebrationLunch_L = classCelebrationLunch.getFormalCtTypeParameters().get(1);
CtTypeParameter classCelebrationLunch_M = classCelebrationLunch.getFormalCtTypeParameters().get(2);
assertEquals("K", classCelebrationLunch_K.getSimpleName());
assertEquals("L", classCelebrationLunch_L.getSimpleName());
assertEquals("M", classCelebrationLunch_M.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(classCelebrationLunch_K).getQualifiedName());
assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classCelebrationLunch_L).getQualifiedName());
assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classCelebrationLunch_M).getQualifiedName());
// method->Section->WeddingLunch->CelebrationLunch
GenericTypeAdapter celebrationLunchTC = methodReserveTC.getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter();
assertEquals("java.lang.Integer", celebrationLunchTC.adaptType(classCelebrationLunch_K).getQualifiedName());
assertEquals("java.lang.Long", celebrationLunchTC.adaptType(classCelebrationLunch_L).getQualifiedName());
assertEquals("java.lang.Double", celebrationLunchTC.adaptType(classCelebrationLunch_M).getQualifiedName());
}
Aggregations