Search in sources :

Example 36 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class ParentTest method testParentOfCtPackageReference.

@Test
public void testParentOfCtPackageReference() throws Exception {
    // contract: a parent at a top level must be the root package and in the code, the element which call getParent().
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/reference-package");
    launcher.run();
    final CtType<Object> panini = launcher.getFactory().Type().get("Panini");
    CtElement topLevelParent = panini.getPackage().getParent();
    assertNotNull(topLevelParent);
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, panini.getPackage().getSimpleName());
    CtPackage pack1 = factory.Package().getRootPackage();
    // the factory are not the same
    assertNotEquals(factory, launcher.getFactory());
    // so the root packages are not deeply equals
    assertNotEquals(pack1, topLevelParent);
    final CtTypeReference<?> burritos = panini.getElements(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference<?> reference) {
            return "Burritos".equals(reference.getSimpleName()) && super.matches(reference);
        }
    }).get(0);
    assertNotNull(burritos.getPackage().getParent());
    assertEquals("com.awesome", burritos.getPackage().getSimpleName());
    assertEquals(burritos, burritos.getPackage().getParent());
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 37 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class SetParentTest method testContract.

@Test
public void testContract() throws Throwable {
    Object o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
    CtMethod<?> setter = factory.Type().get(CtElement.class).getMethodsByName("setParent").get(0);
    Object argument = createCompatibleObject(setter.getParameters().get(0).getType());
    if (!(argument instanceof CtElement)) {
        // is a primitive type or a list
        throw new AssertionError("impossible, setParent always takes an element");
    }
    // we create a fresh object
    CtElement receiver = ((CtElement) o).clone();
    if ("CtClass".equals(toTest.getSimpleName()) || "CtInterface".equals(toTest.getSimpleName()) || "CtEnum".equals(toTest.getSimpleName()) || "CtAnnotationType".equals(toTest.getSimpleName()) || "CtPackage".equals(toTest.getSimpleName())) {
        // contract: root package is the parent for those classes
        assertTrue(receiver.getParent() instanceof CtModelImpl.CtRootPackage);
    } else if ("CtModule".equals(toTest.getSimpleName())) {
        // contract: module parent is necessarily the unnamedmodule
        assertTrue(receiver.getParent() instanceof ModuleFactory.CtUnnamedModule);
    } else {
        // contract: there is no parent before
        try {
            receiver.getParent().hashCode();
            fail(receiver.getParent().getClass().getSimpleName());
        } catch (ParentNotInitializedException normal) {
        }
    }
    Method actualMethod = setter.getReference().getActualMethod();
    CtElement argumentClone = ((CtElement) argument).clone();
    actualMethod.invoke(receiver, new Object[] { argument });
    // contract: the parent has not been changed by a call to setParent on an elemnt
    assertTrue(argument.equals(argumentClone));
    assertFalse(argument == argumentClone);
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) ParentContractTest.createCompatibleObject(spoon.test.parent.ParentContractTest.createCompatibleObject) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CtModelImpl(spoon.reflect.CtModelImpl) Test(org.junit.Test)

Example 38 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class PathTest method testMultiPathFromString.

@Test
public void testMultiPathFromString() throws Exception {
    // When role match a list but no index is provided, all of them must be returned
    Collection<CtElement> results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.foo#body#statement").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 3);
    // When role match a set but no name is provided, all of them must be returned
    results = new CtPathStringBuilder().fromString("#subPackage").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 1);
    // When role match a map but no key is provided, all of them must be returned
    results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.bar##annotation[index=0]#value").evaluateOn(factory.getModel().getRootPackage());
    assertEquals(results.size(), 1);
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtElement(spoon.reflect.declaration.CtElement) Test(org.junit.Test)

Example 39 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class LinesTest method testIdenticalPrettyPrinter.

@Test
public void testIdenticalPrettyPrinter() throws Exception {
    // contract: the end line should also be preserved
    // setup
    String[] options = { "--output-type", "compilationunits", "--output", "target/testIdenticalPrettyPrinter", // those three options together are the closest to what the developer wrote
    "--enable-comments", "--lines", "--with-imports" };
    List<String> paths = new ArrayList<>();
    paths.add("spoon/test/prettyprinter/testclasses/A.java");
    paths.add("spoon/test/prettyprinter/testclasses/AClass.java");
    // paths.add("spoon/test/prettyprinter/testclasses/QualifiedThisRef.java");
    // paths.add("spoon/test/prettyprinter/testclasses/ImportStatic.java");
    // paths.add("spoon/test/prettyprinter/testclasses/QualifiedThisRef.java");
    // paths.add("spoon/test/prettyprinter/testclasses/Rule.java");
    // paths.add("spoon/test/prettyprinter/testclasses/TypeIdentifierCollision.java");
    final Launcher launcher = new Launcher();
    launcher.setArgs(options);
    for (String path : paths) {
        launcher.addInputResource("./src/test/java/" + path);
    }
    launcher.run();
    final Launcher launcher2 = new Launcher();
    launcher2.setArgs(options);
    for (String path : paths) {
        launcher2.addInputResource("./target/testIdenticalPrettyPrinter/" + path);
    }
    launcher2.run();
    int n = 0;
    List<CtElement> elements = launcher.getModel().getElements(new TypeFilter<>(CtElement.class));
    for (int i = 0; i < elements.size(); i++) {
        n++;
        CtElement e = elements.get(i);
        CtElement el2 = launcher2.getModel().getElements(new TypeFilter<>(CtElement.class)).get(i);
        assertNotSame(e, el2);
        assertEquals(e.toString() + " not handled", e.getPosition().getLine(), el2.getPosition().getLine());
        assertEquals(e.toString() + " not handled", e.getPosition().getEndLine(), el2.getPosition().getEndLine());
    }
    assertTrue(n > 20);
}
Also used : CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 40 with CtElement

use of spoon.reflect.declaration.CtElement in project dspot by STAMP-project.

the class AssertionRemover method removeAssertion.

/**
 * Replaces an invocation with its arguments.
 *
 * @param invocation Invocation
 */
public void removeAssertion(CtInvocation<?> invocation) {
    final Factory factory = invocation.getFactory();
    invocation.getArguments().forEach(argument -> {
        CtExpression clone = ((CtExpression) argument).clone();
        if (clone instanceof CtUnaryOperator) {
            clone = ((CtUnaryOperator) clone).getOperand();
        }
        if (clone instanceof CtStatement) {
            clone.getTypeCasts().clear();
            invocation.insertBefore((CtStatement) clone);
        } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
            CtTypeReference<?> typeOfParameter = clone.getType();
            if (clone.getType().equals(factory.Type().NULL_TYPE)) {
                typeOfParameter = factory.Type().createReference(Object.class);
            }
            final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
            invocation.insertBefore(localVariable);
        }
    });
    CtElement currentParent = invocation;
    while (!(currentParent.getParent() instanceof CtStatementList)) {
        currentParent = currentParent.getParent();
    }
    ((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtStatementList(spoon.reflect.code.CtStatementList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtVariableRead(spoon.reflect.code.CtVariableRead)

Aggregations

CtElement (spoon.reflect.declaration.CtElement)86 Test (org.junit.Test)39 Launcher (spoon.Launcher)23 Factory (spoon.reflect.factory.Factory)18 ArrayList (java.util.ArrayList)17 CtMethod (spoon.reflect.declaration.CtMethod)17 CtType (spoon.reflect.declaration.CtType)15 CtStatement (spoon.reflect.code.CtStatement)14 CtTypeReference (spoon.reflect.reference.CtTypeReference)14 List (java.util.List)12 CtClass (spoon.reflect.declaration.CtClass)12 CtField (spoon.reflect.declaration.CtField)12 CtIf (spoon.reflect.code.CtIf)11 CtInvocation (spoon.reflect.code.CtInvocation)11 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)11 CtLocalVariable (spoon.reflect.code.CtLocalVariable)10 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 CtScanner (spoon.reflect.visitor.CtScanner)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9 Collection (java.util.Collection)8