Search in sources :

Example 16 with NamedElementFilter

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

the class MethodTest method testGetAllMethodsAdaptingType.

@Test
public void testGetAllMethodsAdaptingType() throws Exception {
    // contract: AbstractTypingContext should not enter in recursive calls when resolving autoreferenced bounding type
    // such as T extends Comparable<? super T>
    Launcher l = new Launcher();
    l.getEnvironment().setNoClasspath(true);
    l.addInputResource("src/test/resources/noclasspath/spring/PropertyComparator.java");
    l.buildModel();
    CtType<?> propertyComparator = l.getModel().getElements(new NamedElementFilter<CtType>(CtType.class, "PropertyComparator")).get(0);
    Set<CtMethod<?>> allMethods = propertyComparator.getAllMethods();
    boolean compareFound = false;
    for (CtMethod<?> method : allMethods) {
        if (method.getSimpleName().equals("compare")) {
            assertEquals("compare(T,T)", method.getSignature());
            compareFound = true;
        }
    }
    assertTrue(compareFound);
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 17 with NamedElementFilter

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

the class InsertMethodsTest method testInsertBeforeWithBrace.

@Test
public void testInsertBeforeWithBrace() throws Exception {
    CtMethod<?> ifWithBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithBraces = ifWithBraces_m.getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithBraces.getThenStatement().insertBefore(s);
    assertTrue(ifWithBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithBraces.getThenStatement()).getStatement(0));
    assertEquals(ifWithBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 18 with NamedElementFilter

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

the class InsertMethodsTest method testInsertAfterWithBrace.

@Test
public void testInsertAfterWithBrace() throws Exception {
    CtMethod<?> ifWithBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithBraces = ifWithBraces_m.getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithBraces.getThenStatement().insertAfter(s);
    assertTrue(ifWithBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithBraces.getThenStatement()).getStatement(1));
    assertEquals(ifWithBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 19 with NamedElementFilter

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

the class InsertMethodsTest method testInsertAfterWithoutBrace.

@Test
public void testInsertAfterWithoutBrace() throws Exception {
    CtMethod<?> ifWithoutBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithoutBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithoutBraces = ifWithoutBraces_m.getElements(new TypeFilter<>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithoutBraces.getThenStatement().insertAfter(s);
    assertTrue(ifWithoutBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithoutBraces.getThenStatement()).getStatement(1));
    assertEquals(ifWithoutBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 20 with NamedElementFilter

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

the class InsertMethodsTest method testInsertBeforeWithoutBrace.

@Test
public void testInsertBeforeWithoutBrace() throws Exception {
    CtMethod<?> ifWithoutBraces_m = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "ifWithoutBraces")).get(0);
    // replace the return
    CtCodeSnippetStatement s = factory.Code().createCodeSnippetStatement("return 2");
    CtIf ifWithoutBraces = ifWithoutBraces_m.getElements(new TypeFilter<>(CtIf.class)).get(0);
    // Inserts a s before the then statement
    ifWithoutBraces.getThenStatement().insertBefore(s);
    assertTrue(ifWithoutBraces.getThenStatement() instanceof CtBlock);
    assertEquals(s, ((CtBlock<?>) ifWithoutBraces.getThenStatement()).getStatement(0));
    assertEquals(ifWithoutBraces.getThenStatement(), s.getParent());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) CtIf(spoon.reflect.code.CtIf) 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