Search in sources :

Example 76 with CtElement

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

the class VisitorPartialEvaluator method visitCtBlock.

public <R> void visitCtBlock(CtBlock<R> block) {
    CtBlock<?> b = block.getFactory().Core().createBlock();
    for (CtStatement s : block.getStatements()) {
        CtElement res = evaluate(s);
        if (res != null) {
            if (res instanceof CtStatement) {
                b.addStatement((CtStatement) res);
            } else {
                // the context expectes statement. We cannot simplify in this case
                b.addStatement(s);
            }
        }
        // do not copy unreachable statements
        if (flowEnded) {
            break;
        }
    }
    setResult(b);
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtElement(spoon.reflect.declaration.CtElement)

Example 77 with CtElement

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

the class VisitorPartialEvaluator method evaluate.

@SuppressWarnings("unchecked")
public <R extends CtElement> R evaluate(R element) {
    if (element == null) {
        return null;
    }
    element.accept(this);
    if (result != null) {
        CtElement r = result;
        // reset result, to not influence another evaluation.
        result = null;
        if (element.isParentInitialized()) {
            r.setParent(element.getParent());
        }
        return (R) r;
    }
    // otherwise nothing has been changed
    return (R) element.clone();
}
Also used : CtElement(spoon.reflect.declaration.CtElement)

Example 78 with CtElement

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

the class AbstractTypingContext method adaptType.

@Override
public CtTypeReference<?> adaptType(CtTypeInformation type) {
    CtTypeReference<?> result;
    boolean isCopy = false;
    if (type instanceof CtTypeReference<?>) {
        if (type instanceof CtTypeParameterReference) {
            return adaptTypeParameterReference((CtTypeParameterReference) type);
        }
        result = (CtTypeReference<?>) type;
    } else {
        if (type instanceof CtTypeParameter) {
            return adaptTypeParameter((CtTypeParameter) type);
        }
        CtType<?> t = (CtType<?>) type;
        result = t.getFactory().Type().createReference(t, true);
        isCopy = true;
    }
    if (result.getActualTypeArguments().size() > 0) {
        // we have to adapt actual type arguments recursive too
        if (isCopy == false) {
            CtElement parent = result.getParent();
            result = result.clone();
            result.setParent(parent);
            List<CtTypeReference<?>> actTypeArgs = new ArrayList<>(result.getActualTypeArguments());
            for (int i = 0; i < actTypeArgs.size(); i++) {
                CtTypeReference adaptedTypeArgs = adaptType(actTypeArgs.get(i));
                // for some type argument we might return null to avoid recursive calls
                if (adaptedTypeArgs != null) {
                    actTypeArgs.set(i, adaptedTypeArgs.clone());
                }
            }
            result.setActualTypeArguments(actTypeArgs);
        }
    }
    return result;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList)

Example 79 with CtElement

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

the class CtTypeParameterReferenceImpl method getDeclaration.

@Override
public CtTypeParameter getDeclaration() {
    if (!isParentInitialized()) {
        return null;
    }
    CtElement e = this;
    CtElement parent = getParent();
    if (parent instanceof CtTypeReference) {
        if (parent.isParentInitialized() == false) {
            return null;
        }
        parent = parent.getParent();
    }
    if (parent instanceof CtExecutableReference) {
        CtExecutableReference parentExec = (CtExecutableReference) parent;
        if (!parentExec.getDeclaringType().equals(e)) {
            CtElement parent2 = parentExec.getExecutableDeclaration();
            if (parent2 instanceof CtMethod) {
                e = parent2;
            } else {
                e = e.getParent(CtFormalTypeDeclarer.class);
            }
        } else {
            e = e.getParent(CtFormalTypeDeclarer.class);
        }
    } else {
        e = e.getParent(CtFormalTypeDeclarer.class);
    }
    // collecting all formal type declarers of the hierarchy
    while (e != null) {
        CtTypeParameter result = findTypeParamDeclaration((CtFormalTypeDeclarer) e, this.getSimpleName());
        if (result != null) {
            return result;
        }
        e = e.getParent(CtFormalTypeDeclarer.class);
    }
    return null;
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtElement(spoon.reflect.declaration.CtElement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod)

Example 80 with CtElement

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

the class TemplateTest method testTemplateMatcherMatchTwoSnippets.

@Test
public void testTemplateMatcherMatchTwoSnippets() throws Exception {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/TwoSnippets.java");
    spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java"));
    spoon.buildModel();
    Factory factory = spoon.getFactory();
    CtClass<?> templateKlass = factory.Class().get(SecurityCheckerTemplate.class);
    CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher1")).get(0);
    CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0);
    TemplateMatcher matcher = new TemplateMatcher(templateRoot);
    // match using legacy TemplateMatcher#find method
    List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());
    assertEquals(2, matches.size());
    CtElement match1 = matches.get(0);
    CtElement match2 = matches.get(1);
    assertTrue(match1.equals(match2));
    // match using TemplateMatcher#matches method and query filter
    matches = factory.getModel().filterChildren(matcher).list();
    assertEquals(2, matches.size());
    match1 = matches.get(0);
    match2 = matches.get(1);
    assertTrue(match1.equals(match2));
}
Also used : CtElement(spoon.reflect.declaration.CtElement) TemplateMatcher(spoon.template.TemplateMatcher) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) FileSystemFile(spoon.support.compiler.FileSystemFile) CtMethod(spoon.reflect.declaration.CtMethod) CtIf(spoon.reflect.code.CtIf) 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