Search in sources :

Example 16 with CtClass

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);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 17 with CtClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 18 with CtClass

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));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Foo(spoon.test.ctClass.testclasses.Foo) Factory(spoon.reflect.factory.Factory) CtConstructor(spoon.reflect.declaration.CtConstructor) Test(org.junit.Test)

Example 19 with CtClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 20 with CtClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14