Search in sources :

Example 31 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class CtTypeParameterTest method checkTypeParamErasureOfExecutable.

private void checkTypeParamErasureOfExecutable(CtTypeParameter typeParam) throws NoSuchFieldException, SecurityException {
    CtExecutable<?> exec = (CtExecutable<?>) typeParam.getParent();
    CtParameter<?> param = exec.filterChildren(new NamedElementFilter<>(CtParameter.class, "param" + typeParam.getSimpleName())).first();
    assertNotNull("Missing param" + typeParam.getSimpleName() + " in " + exec.getSignature(), param);
    int paramIdx = exec.getParameters().indexOf(param);
    Class declClass = exec.getParent(CtType.class).getActualClass();
    Executable declExec;
    if (exec instanceof CtConstructor) {
        declExec = declClass.getDeclaredConstructors()[0];
    } else {
        declExec = getMethodByName(declClass, exec.getSimpleName());
    }
    Class<?> paramType = declExec.getParameterTypes()[paramIdx];
    // contract the type erasure given with Java reflection is the same as the one computed by spoon
    assertEquals("TypeErasure of executable param " + getTypeParamIdentification(typeParam), paramType.getName(), typeParam.getTypeErasure().toString());
}
Also used : CtType(spoon.reflect.declaration.CtType) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtClass(spoon.reflect.declaration.CtClass) Executable(java.lang.reflect.Executable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 32 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class CtTypeTest method testIsSubTypeOfonTypeParameters.

@Test
public void testIsSubTypeOfonTypeParameters() throws Exception {
    CtType<X> xCtType = buildClass(X.class);
    Factory factory = xCtType.getFactory();
    CtType<?> oCtType = factory.Type().get("spoon.test.ctType.testclasses.O");
    CtType<?> pCtType = factory.Type().get("spoon.test.ctType.testclasses.P");
    CtTypeReference<?> objectCtTypeRef = factory.Type().OBJECT;
    List<CtTypeParameter> oTypeParameters = oCtType.getFormalCtTypeParameters();
    assertTrue(oTypeParameters.size() == 1);
    List<CtTypeParameter> pTypeParameters = pCtType.getFormalCtTypeParameters();
    assertTrue(pTypeParameters.size() == 2);
    CtType<?> O_A_CtType = oTypeParameters.get(0);
    CtType<?> P_D_CtType = pTypeParameters.get(0);
    CtType<?> P_F_CtType = pTypeParameters.get(1);
    CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    CtMethod<?> P_FooMethod = pCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    CtType<?> O_B_CtType = O_FooMethod.getType().getDeclaration();
    CtType<?> P_E_CtType = P_FooMethod.getType().getDeclaration();
    assertTrue(O_B_CtType.isSubtypeOf(xCtType.getReference()));
    assertTrue(O_B_CtType.isSubtypeOf(O_A_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(xCtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(P_D_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(O_A_CtType.getReference()));
    assertTrue(P_D_CtType.isSubtypeOf(O_A_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(O_B_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(objectCtTypeRef));
    assertTrue(P_F_CtType.isSubtypeOf(objectCtTypeRef));
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) X(spoon.test.ctType.testclasses.X) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 33 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class FactoryTest method testCtModel.

@Test
public void testCtModel() throws Exception {
    SpoonAPI spoon = new Launcher();
    spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
    spoon.buildModel();
    CtModel model = spoon.getModel();
    // contains Foo and Foo.@Bar
    assertEquals(1, model.getAllTypes().size());
    // [, spoon, spoon.test, spoon.test.factory, spoon.test.factory.testclasses]
    assertEquals(5, model.getAllPackages().size());
    // add to itself is fine
    model.getRootPackage().addPackage(model.getRootPackage());
    assertEquals(1, model.getAllTypes().size());
    assertEquals(5, model.getAllPackages().size());
    model.getRootPackage().getPackage("spoon").addPackage(model.getRootPackage().getPackage("spoon"));
    assertEquals(1, model.getAllTypes().size());
    assertEquals(5, model.getAllPackages().size());
    model.getRootPackage().addPackage(model.getRootPackage().getPackage("spoon"));
    assertEquals(1, model.getAllTypes().size());
    assertEquals(5, model.getAllPackages().size());
    CtPackage p = model.getElements(new NamedElementFilter<>(CtPackage.class, "spoon")).get(0).clone();
    // if we change the implem, merge is impossible
    CtField f = spoon.getFactory().Core().createField();
    f.setSimpleName("foo");
    f.setType(spoon.getFactory().Type().BYTE);
    p.getElements(new NamedElementFilter<>(CtPackage.class, "testclasses")).get(0).getType("Foo").addField(f);
    try {
        model.getRootPackage().addPackage(p);
        fail("no exception thrown");
    } catch (IllegalStateException success) {
    }
    model.processWith(new AbstractProcessor<CtType>() {

        @Override
        public void process(CtType element) {
            element.delete();
        }
    });
    assertEquals(0, model.getAllTypes().size());
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtModel(spoon.reflect.CtModel) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 34 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class ReplaceTest method testReplaceReplace.

@Test
public void testReplaceReplace() throws Exception {
    // bug found by Benoit
    CtClass<?> foo = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
    CtMethod<?> fooMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "foo")).get(0);
    assertEquals("foo", fooMethod.getSimpleName());
    CtMethod<?> barMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "bar")).get(0);
    assertEquals("bar", barMethod.getSimpleName());
    CtLocalVariable<?> assignment = (CtLocalVariable<?>) fooMethod.getBody().getStatements().get(0);
    CtLocalVariable<?> newAssignment = barMethod.getBody().getStatement(0);
    assignment.replace(newAssignment);
    assertEquals(fooMethod.getBody(), newAssignment.getParent());
    CtLiteral<Integer> lit = (CtLiteral<Integer>) foo.getElements(new TypeFilter<CtLiteral<?>>(CtLiteral.class)).get(0);
    final CtElement parent = lit.getParent();
    CtLiteral<Integer> newLit = factory.Code().createLiteral(0);
    lit.replace(newLit);
    assertEquals("int y = 0", fooMethod.getBody().getStatement(0).toString());
    assertEquals(parent, newLit.getParent());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtElement(spoon.reflect.declaration.CtElement) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 35 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class ReplaceTest method testReplaceBlock.

@Test
public void testReplaceBlock() throws Exception {
    CtClass<?> foo = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
    CtMethod<?> m = foo.getElements(new NamedElementFilter<>(CtMethod.class, "foo")).get(0);
    assertEquals("foo", m.getSimpleName());
    final CtStatement parent = m.getBody().getStatements().get(2);
    CtAssignment<?, ?> assignment = (CtAssignment<?, ?>) parent;
    CtExpression<Integer> s1 = (CtExpression<Integer>) assignment.getAssignment();
    CtExpression<Integer> s2 = factory.Code().createLiteral(3);
    assertEquals("z = x + 1", assignment.toString());
    assertEquals("x + 1", s1.toString());
    // do
    s1.replace(s2);
    assertSame(s2, assignment.getAssignment());
    assertEquals("z = 3", assignment.toString());
    assertEquals(parent, s2.getParent());
    // undo
    s2.replace(s1);
    assertSame(s1, assignment.getAssignment());
    assertEquals("z = x + 1", assignment.toString());
    assertEquals(parent, s1.getParent());
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Test(org.junit.Test)

Aggregations

NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)43 Test (org.junit.Test)42 Launcher (spoon.Launcher)19 Factory (spoon.reflect.factory.Factory)17 CtClass (spoon.reflect.declaration.CtClass)10 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)10 CtMethod (spoon.reflect.declaration.CtMethod)8 MainTest (spoon.test.main.MainTest)8 CtIf (spoon.reflect.code.CtIf)7 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)7 CtBlock (spoon.reflect.code.CtBlock)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)6 File (java.io.File)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtElement (spoon.reflect.declaration.CtElement)4 Annotation (java.lang.annotation.Annotation)3 CtLiteral (spoon.reflect.code.CtLiteral)3 CtParameter (spoon.reflect.declaration.CtParameter)3