Search in sources :

Example 1 with CtStatementList

use of spoon.reflect.code.CtStatementList in project spoon by INRIA.

the class DefaultCoreFactory method createStatementList.

public <R> CtStatementList createStatementList() {
    CtStatementList e = new CtStatementListImpl<R>();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtStatementListImpl(spoon.support.reflect.code.CtStatementListImpl) CtStatementList(spoon.reflect.code.CtStatementList)

Example 2 with CtStatementList

use of spoon.reflect.code.CtStatementList in project spoon by INRIA.

the class CtStatementImpl method insertAfter.

public static void insertAfter(CtStatement target, CtStatement statement) throws ParentNotInitializedException {
    CtStatementList sts = target.getFactory().Core().createStatementList();
    sts.addStatement(statement);
    insertAfter(target, sts);
}
Also used : CtStatementList(spoon.reflect.code.CtStatementList)

Example 3 with CtStatementList

use of spoon.reflect.code.CtStatementList in project spoon by INRIA.

the class PotentialVariableDeclarationFunction method apply.

@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
    isTypeOnTheWay = false;
    isInStaticScope = false;
    // Search previous siblings for element which may represents the declaration of this local variable
    CtQuery siblingsQuery = input.getFactory().createQuery().map(new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS)).select(new TypeFilter<>(CtVariable.class));
    if (variableName != null) {
        // variable name is defined so we have to search only for variables with that name
        siblingsQuery = siblingsQuery.select(new NamedElementFilter<>(CtNamedElement.class, variableName));
    }
    CtElement scopeElement = input;
    // Search input and then all parents until first CtPackage for element which may represents the declaration of this local variable
    while (scopeElement != null && !(scopeElement instanceof CtPackage) && scopeElement.isParentInitialized()) {
        CtElement parent = scopeElement.getParent();
        if (parent instanceof CtType<?>) {
            isTypeOnTheWay = true;
            // visit each CtField of `parent` CtType
            CtQuery q = parent.map(new AllTypeMembersFunction(CtField.class));
            q.forEach((CtField<?> field) -> {
                if (isInStaticScope && field.hasModifier(ModifierKind.STATIC) == false) {
                    /*
						 * the variable reference is used in static scope,
						 * but the field is not static - ignore it
						 */
                    return;
                }
                // else send field as potential variable declaration
                if (sendToOutput(field, outputConsumer)) {
                    // and terminate the internal query q if outer query is already terminated
                    q.terminate();
                }
            });
            if (query.isTerminated()) {
                return;
            }
        } else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
            // visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
            siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
            if (query.isTerminated()) {
                return;
            }
            // visit parameters of CtCatch and CtExecutable (method, lambda)
            if (parent instanceof CtCatch) {
                CtCatch ctCatch = (CtCatch) parent;
                if (sendToOutput(ctCatch.getParameter(), outputConsumer)) {
                    return;
                }
            } else if (parent instanceof CtExecutable) {
                CtExecutable<?> exec = (CtExecutable<?>) parent;
                for (CtParameter<?> param : exec.getParameters()) {
                    if (sendToOutput(param, outputConsumer)) {
                        return;
                    }
                }
            }
        }
        if (parent instanceof CtModifiable) {
            isInStaticScope = isInStaticScope || ((CtModifiable) parent).hasModifier(ModifierKind.STATIC);
        }
        scopeElement = parent;
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) CtExecutable(spoon.reflect.declaration.CtExecutable) CtBodyHolder(spoon.reflect.code.CtBodyHolder) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtVariable(spoon.reflect.declaration.CtVariable) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) CtCatch(spoon.reflect.code.CtCatch) CtModifiable(spoon.reflect.declaration.CtModifiable)

Example 4 with CtStatementList

use of spoon.reflect.code.CtStatementList in project dspot by STAMP-project.

the class AssertionRemover method removeAssertion.

/**
 * Replaces an invocation with its arguments.
 *
 * @param invocation Invocation
 */
public void removeAssertion(CtInvocation<?> invocation) {
    final Factory factory = invocation.getFactory();
    invocation.getArguments().forEach(argument -> {
        CtExpression clone = ((CtExpression) argument).clone();
        if (clone instanceof CtUnaryOperator) {
            clone = ((CtUnaryOperator) clone).getOperand();
        }
        if (clone instanceof CtStatement) {
            clone.getTypeCasts().clear();
            invocation.insertBefore((CtStatement) clone);
        } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
            CtTypeReference<?> typeOfParameter = clone.getType();
            if (clone.getType().equals(factory.Type().NULL_TYPE)) {
                typeOfParameter = factory.Type().createReference(Object.class);
            }
            final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
            invocation.insertBefore(localVariable);
        }
    });
    CtElement currentParent = invocation;
    while (!(currentParent.getParent() instanceof CtStatementList)) {
        currentParent = currentParent.getParent();
    }
    ((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtStatementList(spoon.reflect.code.CtStatementList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtVariableRead(spoon.reflect.code.CtVariableRead)

Example 5 with CtStatementList

use of spoon.reflect.code.CtStatementList in project spoon by INRIA.

the class AccessibleVariablesFinder method getVariable.

private List<CtVariable> getVariable(final CtElement parent) {
    final List<CtVariable> variables = new ArrayList<>();
    if (parent == null) {
        return variables;
    }
    class VariableScanner extends CtInheritanceScanner {

        @Override
        public void visitCtStatementList(CtStatementList e) {
            for (int i = 0; i < e.getStatements().size(); i++) {
                CtStatement ctStatement = e.getStatements().get(i);
                if (ctStatement.getPosition() == null) {
                }
                if (ctStatement.getPosition() != null && ctStatement.getPosition().getSourceStart() > expression.getPosition().getSourceEnd()) {
                    break;
                }
                if (ctStatement instanceof CtVariable) {
                    variables.add((CtVariable) ctStatement);
                }
            }
            super.visitCtStatementList(e);
        }

        @Override
        public <T> void scanCtType(CtType<T> type) {
            List<CtField<?>> fields = type.getFields();
            for (int i = 0; i < fields.size(); i++) {
                CtField<?> ctField = fields.get(i);
                if (ctField.hasModifier(ModifierKind.PUBLIC) || ctField.hasModifier(ModifierKind.PROTECTED)) {
                    variables.add(ctField);
                } else if (ctField.hasModifier(ModifierKind.PRIVATE)) {
                    if (expression.hasParent(type)) {
                        variables.add(ctField);
                    }
                } else if (expression.getParent(CtPackage.class).equals(type.getParent(CtPackage.class))) {
                    // default visibility
                    variables.add(ctField);
                }
            }
            CtTypeReference<?> superclass = type.getSuperclass();
            if (superclass != null) {
                variables.addAll(getVariable(superclass.getTypeDeclaration()));
            }
            Set<CtTypeReference<?>> superInterfaces = type.getSuperInterfaces();
            for (Iterator<CtTypeReference<?>> iterator = superInterfaces.iterator(); iterator.hasNext(); ) {
                CtTypeReference<?> typeReference = iterator.next();
                variables.addAll(getVariable(typeReference.getTypeDeclaration()));
            }
            super.scanCtType(type);
        }

        @Override
        public void visitCtTryWithResource(CtTryWithResource e) {
            variables.addAll(e.getResources());
            super.visitCtTryWithResource(e);
        }

        @Override
        public void scanCtExecutable(CtExecutable e) {
            variables.addAll(e.getParameters());
            super.scanCtExecutable(e);
        }

        @Override
        public void visitCtFor(CtFor e) {
            for (CtStatement ctStatement : e.getForInit()) {
                this.scan(ctStatement);
            }
            super.visitCtFor(e);
        }

        @Override
        public void visitCtForEach(CtForEach e) {
            variables.add(e.getVariable());
            super.visitCtForEach(e);
        }

        @Override
        public void visitCtMethod(CtMethod e) {
            this.scan(e.getBody());
            super.visitCtMethod(e);
        }

        @Override
        public void visitCtLocalVariable(CtLocalVariable e) {
            variables.add(e);
            super.visitCtLocalVariable(e);
        }

        @Override
        public void visitCtCatch(CtCatch e) {
            variables.add(e.getParameter());
            super.visitCtCatch(e);
        }
    }
    new VariableScanner().scan(parent);
    return variables;
}
Also used : ArrayList(java.util.ArrayList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtForEach(spoon.reflect.code.CtForEach) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtVariable(spoon.reflect.declaration.CtVariable) CtStatementList(spoon.reflect.code.CtStatementList) CtPackage(spoon.reflect.declaration.CtPackage) CtTryWithResource(spoon.reflect.code.CtTryWithResource) CtCatch(spoon.reflect.code.CtCatch) CtFor(spoon.reflect.code.CtFor) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

CtStatementList (spoon.reflect.code.CtStatementList)11 CtStatement (spoon.reflect.code.CtStatement)5 CtElement (spoon.reflect.declaration.CtElement)4 ArrayList (java.util.ArrayList)3 CtCatch (spoon.reflect.code.CtCatch)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtField (spoon.reflect.declaration.CtField)3 CtPackage (spoon.reflect.declaration.CtPackage)3 CtType (spoon.reflect.declaration.CtType)3 CtExpression (spoon.reflect.code.CtExpression)2 CtClass (spoon.reflect.declaration.CtClass)2 CtMethod (spoon.reflect.declaration.CtMethod)2 File (java.io.File)1 List (java.util.List)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AbstractVariableDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1