Search in sources :

Example 26 with CtElement

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

the class CtParameterReferenceImpl method lookupDynamically.

private CtParameter<T> lookupDynamically() {
    CtElement element = this;
    CtParameter optional = null;
    String name = getSimpleName();
    try {
        do {
            CtExecutable executable = element.getParent(CtExecutable.class);
            if (executable == null) {
                return null;
            }
            for (CtParameter parameter : (List<CtParameter>) executable.getParameters()) {
                if (name.equals(parameter.getSimpleName())) {
                    optional = parameter;
                }
            }
            element = executable;
        } while (optional == null);
    } catch (ParentNotInitializedException e) {
        return null;
    }
    return optional;
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) CtParameter(spoon.reflect.declaration.CtParameter) List(java.util.List) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 27 with CtElement

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

the class SubstitutionVisitor method substitute.

/**
 * Substitutes all template parameters of element and returns substituted element.
 *
 * @param element to be substituted model
 * @return substituted model
 */
public <E extends CtElement> List<E> substitute(E element) {
    final Map<CtElement, String> elementToGeneratedByComment = addGeneratedBy ? new IdentityHashMap<CtElement, String>() : null;
    if (addGeneratedBy) {
        /*
			 * collect 'generated by' comments for each type member of the substituted element, before the substitution is done,
			 * so we know the origin names of the members.
			 */
        final CtInheritanceScanner internalScanner = new CtInheritanceScanner() {

            public void scanCtTypeMember(CtTypeMember typeMeber) {
                elementToGeneratedByComment.put(typeMeber, getGeneratedByComment(typeMeber));
            }
        };
        new CtScanner() {

            @Override
            public void scan(CtElement p_element) {
                internalScanner.scan(p_element);
                super.scan(p_element);
            }
        }.scan(element);
    }
    List<E> result = createContext().substitute(element);
    if (addGeneratedBy) {
        // add generated by comments after substitution, otherwise they would be substituted in comments too.
        applyGeneratedByComments(elementToGeneratedByComment);
    }
    return result;
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtElement(spoon.reflect.declaration.CtElement) CtInheritanceScanner(spoon.reflect.visitor.CtInheritanceScanner) CtScanner(spoon.reflect.visitor.CtScanner)

Example 28 with CtElement

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

the class ReplacementVisitor method replaceInSetIfExist.

private <T extends CtElement> void replaceInSetIfExist(Set<T> setProtected, ReplaceSetListener listener) {
    Set<T> set = new HashSet<>(setProtected);
    T shouldBeDeleted = null;
    for (T element : set) {
        if (element == original) {
            shouldBeDeleted = element;
            break;
        }
    }
    if (shouldBeDeleted != null) {
        set.remove(shouldBeDeleted);
        for (CtElement ele : replace) {
            if (ele != null) {
                set.add((T) ele);
                ele.setParent(shouldBeDeleted.getParent());
            }
        }
        listener.set(set);
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) HashSet(java.util.HashSet)

Example 29 with CtElement

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

the class CloneTest method testCloneListener.

@Test
public void testCloneListener() throws Exception {
    // contract: it is possible to extend the cloning behavior
    // in this example extension, a listener of cloning process gets access to origin node and cloned node
    // we check the contract with some complicated class as target of cloning
    Factory factory = ModelUtils.build(new File("./src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java"));
    CtType<?> cloneSource = factory.Type().get(DefaultJavaPrettyPrinter.class);
    class CloneListener extends CloneHelper {

        Map<CtElement, CtElement> sourceToTarget = new IdentityHashMap<>();

        @Override
        public <T extends CtElement> T clone(T source) {
            if (source == null) {
                return null;
            }
            T target = super.clone(source);
            onCloned(source, target);
            return target;
        }

        private void onCloned(CtElement source, CtElement target) {
            CtElement previousTarget = sourceToTarget.put(source, target);
            assertNull(previousTarget);
        }
    }
    CloneListener cl = new CloneListener();
    CtType<?> cloneTarget = cl.clone(cloneSource);
    cloneSource.filterChildren(null).forEach(sourceElement -> {
        // contract: there exists cloned target for each visitable element
        CtElement targetElement = cl.sourceToTarget.remove(sourceElement);
        assertNotNull("Missing target for sourceElement\n" + sourceElement, targetElement);
        assertEquals("Source and Target are not equal", sourceElement, targetElement);
    });
    // contract: each visitable elements was cloned exactly once.  No more no less.
    assertTrue(cl.sourceToTarget.isEmpty());
}
Also used : CloneHelper(spoon.support.visitor.equals.CloneHelper) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) File(java.io.File) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Test(org.junit.Test)

Example 30 with CtElement

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

the class CloneTest method testCopyType.

@Test
public void testCopyType() throws Exception {
    // contract: the copied type is well formed, it never points to the initial type
    Factory factory = ModelUtils.build(new File("./src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java"));
    CtType<?> intialElement = factory.Type().get(DefaultJavaPrettyPrinter.class);
    CtType<?> cloneTarget = intialElement.copyType();
    assertEquals("spoon.reflect.visitor.DefaultJavaPrettyPrinterCopy", cloneTarget.getQualifiedName());
    // we go over all references
    for (CtReference reference : cloneTarget.getElements(new TypeFilter<>(CtReference.class))) {
        CtElement declaration = reference.getDeclaration();
        if (declaration == null) {
            continue;
        }
        // the core assertion: not a single reference points to the initial element
        if (declaration.hasParent(intialElement)) {
            fail();
        }
    }
}
Also used : CtReference(spoon.reflect.reference.CtReference) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) File(java.io.File) 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