Search in sources :

Example 1 with CtAbstractVisitor

use of spoon.reflect.visitor.CtAbstractVisitor in project spoon by INRIA.

the class CtParameterRemoveRefactoring method canRemoveExpression.

/**
 * Detects whether `toBeRemovedExpression` can be safely removed during the refactoring
 *
 * @param toBeRemovedExpression the {@link CtExpression}, which will be removed by this refactoring
 * @return true if the expression used to deliver argument of removed parameter can be removed
 * false if cannot be removed and this refactoring has to be avoided.
 */
protected boolean canRemoveExpression(CtExpression<?> toBeRemovedExpression) {
    class Context {

        boolean canBeRemoved = false;
    }
    final Context context = new Context();
    toBeRemovedExpression.accept(new CtAbstractVisitor() {

        @Override
        public <T> void visitCtVariableRead(CtVariableRead<T> variableRead) {
            context.canBeRemoved = true;
        }

        @Override
        public <T> void visitCtArrayRead(CtArrayRead<T> arrayRead) {
            context.canBeRemoved = true;
        }

        @Override
        public <T> void visitCtFieldRead(CtFieldRead<T> fieldRead) {
            context.canBeRemoved = true;
        }

        @Override
        public <T> void visitCtParameterReference(CtParameterReference<T> reference) {
            context.canBeRemoved = true;
        }

        @Override
        public <T> void visitCtLiteral(CtLiteral<T> literal) {
            context.canBeRemoved = true;
        }

        @Override
        public <T> void visitCtNewArray(CtNewArray<T> newArray) {
            context.canBeRemoved = true;
        }

        @Override
        public <T> void visitCtAnnotationFieldAccess(CtAnnotationFieldAccess<T> annotationFieldAccess) {
            context.canBeRemoved = true;
        }

        @Override
        public <T> void visitCtThisAccess(CtThisAccess<T> thisAccess) {
            context.canBeRemoved = true;
        }
    });
    return context.canBeRemoved;
}
Also used : CtAbstractVisitor(spoon.reflect.visitor.CtAbstractVisitor)

Example 2 with CtAbstractVisitor

use of spoon.reflect.visitor.CtAbstractVisitor in project spoon by INRIA.

the class TypeFactory method createTypeAdapter.

/**
 * Create a {@link GenericTypeAdapter} for adapting of formal type parameters from any compatible context to the context of provided `formalTypeDeclarer`
 *
 * @param formalTypeDeclarer
 * 		the target scope of the returned {@link GenericTypeAdapter}
 */
public GenericTypeAdapter createTypeAdapter(CtFormalTypeDeclarer formalTypeDeclarer) {
    class Visitor extends CtAbstractVisitor {

        GenericTypeAdapter adapter;

        @Override
        public <T> void visitCtClass(CtClass<T> ctClass) {
            adapter = new ClassTypingContext(ctClass);
        }

        @Override
        public <T> void visitCtInterface(CtInterface<T> intrface) {
            adapter = new ClassTypingContext(intrface);
        }

        @Override
        public <T> void visitCtMethod(CtMethod<T> m) {
            adapter = new MethodTypingContext().setMethod(m);
        }

        @Override
        public <T> void visitCtConstructor(CtConstructor<T> c) {
            adapter = new MethodTypingContext().setConstructor(c);
        }
    }
    Visitor visitor = new Visitor();
    ((CtElement) formalTypeDeclarer).accept(visitor);
    return visitor.adapter;
}
Also used : CtAbstractVisitor(spoon.reflect.visitor.CtAbstractVisitor) CtClass(spoon.reflect.declaration.CtClass) CtInterface(spoon.reflect.declaration.CtInterface) ClassTypingContext(spoon.support.visitor.ClassTypingContext) GenericTypeAdapter(spoon.support.visitor.GenericTypeAdapter) CtAbstractVisitor(spoon.reflect.visitor.CtAbstractVisitor) MethodTypingContext(spoon.support.visitor.MethodTypingContext) CtElement(spoon.reflect.declaration.CtElement) CtMethod(spoon.reflect.declaration.CtMethod) CtConstructor(spoon.reflect.declaration.CtConstructor)

Aggregations

CtAbstractVisitor (spoon.reflect.visitor.CtAbstractVisitor)2 CtClass (spoon.reflect.declaration.CtClass)1 CtConstructor (spoon.reflect.declaration.CtConstructor)1 CtElement (spoon.reflect.declaration.CtElement)1 CtInterface (spoon.reflect.declaration.CtInterface)1 CtMethod (spoon.reflect.declaration.CtMethod)1 ClassTypingContext (spoon.support.visitor.ClassTypingContext)1 GenericTypeAdapter (spoon.support.visitor.GenericTypeAdapter)1 MethodTypingContext (spoon.support.visitor.MethodTypingContext)1