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;
}
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);
}
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;
}
}
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);
}
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;
}
Aggregations