Search in sources :

Example 1 with CtElement

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

the class TypeFactory method get.

/**
 * Gets a type from its runtime Java class. If the class isn't in the spoon path,
 * the class will be build from the Java reflection and will be marked as
 * shadow (see {@link spoon.reflect.declaration.CtShadowable}).
 *
 * @param <T>
 * 		actual type of the class
 * @param cl
 * 		the java class: note that this class should be Class&lt;T&gt; but it
 * 		then poses problem when T is a generic type itself
 */
@SuppressWarnings("unchecked")
public <T> CtType<T> get(Class<?> cl) {
    final CtType<T> aType = get(cl.getName());
    if (aType == null) {
        final CtType<T> shadowClass = (CtType<T>) this.shadowCache.get(cl);
        if (shadowClass == null) {
            CtType<T> newShadowClass;
            try {
                newShadowClass = new JavaReflectionTreeBuilder(createFactory()).scan((Class<T>) cl);
            } catch (Throwable e) {
                throw new SpoonClassNotFoundException("cannot create shadow class: " + cl.getName(), e);
            }
            newShadowClass.setFactory(factory);
            newShadowClass.accept(new CtScanner() {

                @Override
                public void scan(CtElement element) {
                    if (element != null) {
                        element.setFactory(factory);
                    }
                }
            });
            this.shadowCache.put(cl, newShadowClass);
            return newShadowClass;
        } else {
            return shadowClass;
        }
    }
    return aType;
}
Also used : CtType(spoon.reflect.declaration.CtType) JavaReflectionTreeBuilder(spoon.support.visitor.java.JavaReflectionTreeBuilder) CtElement(spoon.reflect.declaration.CtElement) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtNewClass(spoon.reflect.code.CtNewClass) CtClass(spoon.reflect.declaration.CtClass) CtScanner(spoon.reflect.visitor.CtScanner)

Example 2 with CtElement

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

the class RoleHandlerHelper method getRoleHandlerWrtParent.

/**
 * @param element the {@link CtElement} whose relation from `element.getParent()` to `element` is needed.
 * @return {@link RoleHandler} handling relation from `element.getParent()` to `element`
 */
public static RoleHandler getRoleHandlerWrtParent(CtElement element) {
    if (element.isParentInitialized() == false) {
        return null;
    }
    CtElement parent = element.getParent();
    CtRole roleInParent = element.getRoleInParent();
    if (roleInParent == null) {
        return null;
    }
    return RoleHandlerHelper.getRoleHandler(parent.getClass(), roleInParent);
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtRole(spoon.reflect.path.CtRole)

Example 3 with CtElement

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

the class QueueProcessingManager method process.

public void process(Collection<? extends CtElement> elements) {
    Processor<?> p;
    // copy so that one can reuse the processing manager
    // among different processing steps
    Queue<Processor<?>> processors = new LinkedList<>(getProcessors());
    while ((p = processors.poll()) != null) {
        try {
            getFactory().getEnvironment().reportProgressMessage(p.getClass().getName());
            current = p;
            // load the properties
            p.init();
            p.process();
            for (CtElement e : new ArrayList<>(elements)) {
                getVisitor().setProcessor(p);
                getVisitor().scan(e);
            }
        } catch (ProcessInterruption ignore) {
        } finally {
            p.processingDone();
        }
    }
}
Also used : Processor(spoon.processing.Processor) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ProcessInterruption(spoon.processing.ProcessInterruption)

Example 4 with CtElement

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

the class RuntimeProcessingManager method process.

/**
 * Recursively processes elements and their children with a given processor.
 */
public void process(Collection<? extends CtElement> elements, Processor<?> processor) {
    try {
        getFactory().getEnvironment().debugMessage("processing with '" + processor.getClass().getName() + "'...");
        current = processor;
        Timer.start(processor.getClass().getName());
        for (CtElement e : elements) {
            process(e, processor);
        }
        Timer.stop(processor.getClass().getName());
    } catch (ProcessInterruption ignored) {
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) ProcessInterruption(spoon.processing.ProcessInterruption)

Example 5 with CtElement

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

the class ContextBuilder method enter.

@SuppressWarnings("unchecked")
void enter(CtElement e, ASTNode node) {
    stack.push(new ASTPair(e, node));
    if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon.getFile().getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) {
        if (compilationunitdeclaration != null && !e.isImplicit()) {
            e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node));
        }
    }
    ASTPair pair = stack.peek();
    CtElement current = pair.element;
    if (current instanceof CtExpression) {
        while (!casts.isEmpty()) {
            ((CtExpression<?>) current).addTypeCast(casts.remove(0));
        }
    }
    if (current instanceof CtStatement && !this.label.isEmpty()) {
        ((CtStatement) current).setLabel(this.label.pop());
    }
    try {
        if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && !(e instanceof CtCatchVariable) && node instanceof Expression) {
            if (((CtTypedElement<?>) e).getType() == null) {
                ((CtTypedElement<Object>) e).setType(this.jdtTreeBuilder.getReferencesBuilder().getTypeReference(((Expression) node).resolvedType));
            }
        }
    } catch (UnsupportedOperationException ignore) {
    // For some element, we throw an UnsupportedOperationException when we call setType().
    }
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CtExpression(spoon.reflect.code.CtExpression) CtElement(spoon.reflect.declaration.CtElement) CtPackage(spoon.reflect.declaration.CtPackage) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

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