use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CtBodyHolderTest method testMethod.
@Test
public void testMethod() throws Exception {
Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
assertEquals(2, cwbClass.getMethods().size());
CtMethod<?> method = cwbClass.getMethod("method");
checkCtBody(method, "method_body", 0);
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CtClassTest method testCloneAnonymousClassInvocationWithAutoimports.
@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>(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());
assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CtClassTest method getConstructor.
@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);
// 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().OBJECT));
// 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(1));
// 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));
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CtClassTest method testCloneAnonymousClassInvocation.
@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>(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());
assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class FilterTest method testQueryWithOptionalNumberOfInputs.
@Test
public void testQueryWithOptionalNumberOfInputs() throws Exception {
// contract: QueryFactory allows to create query with an optional number of inputs
// the input can be provided as Array or Iterable
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
CtClass<?> cls2 = launcher.getFactory().Class().get(Tostada.class);
CtClass<?> cls3 = launcher.getFactory().Class().get(Antojito.class);
// here is the query
CtQuery q1 = launcher.getFactory().Query().createQuery(cls, cls2).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada" }, q1.list().toArray());
CtQuery q1b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada" }, q1b.list().toArray());
CtQuery q2 = launcher.getFactory().Query().createQuery(cls, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Antojito" }, q2.list().toArray());
CtQuery q2b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls3)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Antojito" }, q2b.list().toArray());
CtQuery q3 = launcher.getFactory().Query().createQuery(cls, cls2, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada", "Antojito" }, q3.list().toArray());
CtQuery q3b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2, cls3)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[] { "Tacos", "Tostada", "Antojito" }, q3b.list().toArray());
}
Aggregations