Search in sources :

Example 31 with CtElement

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

the class SpoonMetaModel method getAllInstantiableMetamodelInterfaces.

public List<CtType<? extends CtElement>> getAllInstantiableMetamodelInterfaces() {
    SpoonAPI interfaces = new Launcher();
    interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
    interfaces.addInputResource("src/main/java/spoon/reflect/code");
    interfaces.addInputResource("src/main/java/spoon/reflect/reference");
    interfaces.buildModel();
    SpoonAPI implementations = new Launcher();
    implementations.addInputResource("src/main/java/spoon/support/reflect/declaration");
    implementations.addInputResource("src/main/java/spoon/support/reflect/code");
    implementations.addInputResource("src/main/java/spoon/support/reflect/reference");
    implementations.buildModel();
    List<CtType<? extends CtElement>> result = new ArrayList<>();
    for (CtType<?> itf : interfaces.getModel().getAllTypes()) {
        String impl = itf.getQualifiedName().replace("spoon.reflect", "spoon.support.reflect") + "Impl";
        CtType implClass = implementations.getFactory().Type().get(impl);
        if (implClass != null && !implClass.hasModifier(ModifierKind.ABSTRACT)) {
            result.add((CtType<? extends CtElement>) itf);
        }
    }
    return result;
}
Also used : CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI)

Example 32 with CtElement

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

the class MainTest method checkModelIsTree.

/*
	 * contract: each element is used only once
	 * For example this is always true: field.getType() != field.getDeclaringType()
	 */
@Test
public void checkModelIsTree() {
    Exception dummyException = new Exception("STACK");
    PrinterHelper problems = new PrinterHelper(rootPackage.getFactory().getEnvironment());
    Map<CtElement, Exception> allElements = new IdentityHashMap<>();
    rootPackage.filterChildren(null).forEach((CtElement ele) -> {
        // uncomment this line to get stacktrace of real problem. The dummyException is used to avoid OutOfMemoryException
        // Exception secondStack = new Exception("STACK");
        Exception secondStack = dummyException;
        Exception firstStack = allElements.put(ele, secondStack);
        if (firstStack != null) {
            if (firstStack == dummyException) {
                Assert.fail("The Spoon model is not a tree. The " + ele.getClass().getSimpleName() + ":" + ele.toString() + " is shared");
            }
            // the element ele was already visited. It means it used on more places
            // report the stacktrace of first and second usage, so that place can be found easily
            problems.write("The element " + ele.getClass().getSimpleName()).writeln().incTab().write(ele.toString()).writeln().write("Is linked by these stacktraces").writeln().write("1) " + getStackTrace(firstStack)).writeln().write("2) " + getStackTrace(secondStack)).writeln().decTab();
        }
    });
    String report = problems.toString();
    if (report.length() > 0) {
        Assert.fail(report);
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) IdentityHashMap(java.util.IdentityHashMap) CtPathException(spoon.reflect.path.CtPathException) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) PrinterHelper(spoon.reflect.visitor.PrinterHelper) Test(org.junit.Test) ParentTest(spoon.test.parent.ParentTest)

Example 33 with CtElement

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

the class MainTest method checkParentConsistency.

public static void checkParentConsistency(CtElement ele) {
    final Set<CtElement> inconsistentParents = new HashSet<>();
    new CtScanner() {

        private Deque<CtElement> previous = new ArrayDeque();

        @Override
        protected void enter(CtElement e) {
            if (e != null) {
                if (!previous.isEmpty()) {
                    try {
                        if (e.getParent() != previous.getLast()) {
                            inconsistentParents.add(e);
                        }
                    } catch (ParentNotInitializedException ignore) {
                        inconsistentParents.add(e);
                    }
                }
                previous.add(e);
            }
            super.enter(e);
        }

        @Override
        protected void exit(CtElement e) {
            if (e == null) {
                return;
            }
            if (e.equals(previous.getLast())) {
                previous.removeLast();
            } else {
                throw new RuntimeException("Inconsistent stack");
            }
            super.exit(e);
        }
    }.scan(ele);
    assertEquals("All parents have to be consistent", 0, inconsistentParents.size());
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) CtScanner(spoon.reflect.visitor.CtScanner) ArrayDeque(java.util.ArrayDeque) HashSet(java.util.HashSet)

Example 34 with CtElement

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

the class JavaDocTest method testJavadocNotPresentInAST.

@Test
public void testJavadocNotPresentInAST() throws Exception {
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setCommentEnabled(false);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/javadoc/testclasses/");
    launcher.run();
    new CtScanner() {

        @Override
        public void scan(CtElement element) {
            if (element != null) {
                assertEquals(0, element.getComments().size());
            }
            super.scan(element);
        }

        @Override
        public void visitCtComment(CtComment comment) {
            fail("Shouldn't have comment in the model.");
            super.visitCtComment(comment);
        }
    }.scan(launcher.getModel().getRootPackage());
}
Also used : CtComment(spoon.reflect.code.CtComment) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 35 with CtElement

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

the class ParentContractTest method testContract.

@Test
public void testContract() throws Throwable {
    int nSetterCalls = 0;
    int nAssertsOnParent = 0;
    int nAssertsOnParentInList = 0;
    // contract: all setters/adders must set the parent (not necessarily the direct parent, can be upper in the parent tree, for instance when injecting blocks
    Object o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
    for (CtMethod<?> setter : SpoonTestHelpers.getAllSetters(toTest)) {
        Object argument = createCompatibleObject(setter.getParameters().get(0).getType());
        try {
            // we create a fresh object
            CtElement receiver = ((CtElement) o).clone();
            // we invoke the setter
            Method actualMethod = setter.getReference().getActualMethod();
            actualMethod.invoke(receiver, new Object[] { argument });
            nSetterCalls++;
            nTotalSetterCalls++;
            // directly the element
            if (CtElement.class.isInstance(argument) && setter.getAnnotation(UnsettableProperty.class) == null) {
                nAssertsOnParent++;
                assertTrue(((CtElement) argument).hasParent(receiver));
            }
            // the element is in a list
            if (Collection.class.isInstance(argument) && setter.getAnnotation(UnsettableProperty.class) == null) {
                nAssertsOnParentInList++;
                assertTrue(((CtElement) ((Collection) argument).iterator().next()).hasParent(receiver));
            }
        } catch (AssertionError e) {
            Assert.fail("call setParent contract failed for " + setter.toString() + " " + e.toString());
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof UnsupportedOperationException) {
                // this is now documented by @UnsettableProperty
                throw e;
            } else if (e.getCause() instanceof RuntimeException) {
                throw e.getCause();
            } else {
                throw new SpoonException(e.getCause());
            }
        }
    }
    assertTrue(nSetterCalls > 0);
    assertTrue(nAssertsOnParent > 0 || nAssertsOnParentInList > 0);
}
Also used : SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) Collection(java.util.Collection) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

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