use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class PositionTest method getPositionOfImplicitBlock.
@Test
public void getPositionOfImplicitBlock() {
// contract: position of implicit block in if (cond) [implicit block] else [implicit block] should be the source position of implicit block content.
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/position/testclasses/ImplicitBlock.java");
launcher.buildModel();
CtIf ifElement = launcher.getModel().getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
CtStatement thenStatement = ifElement.getThenStatement();
assertTrue(thenStatement instanceof CtBlock);
CtBlock thenBlock = (CtBlock) thenStatement;
SourcePosition positionThen = thenBlock.getPosition();
CtStatement returnStatement = thenBlock.getStatement(0);
assertEquals(returnStatement.getPosition(), positionThen);
assertEquals("ImplicitBlock.java", positionThen.getFile().getName());
assertEquals(7, positionThen.getLine());
CtStatement elseStatement = ifElement.getElseStatement();
assertTrue(elseStatement instanceof CtBlock);
CtBlock elseBlock = (CtBlock) elseStatement;
SourcePosition positionElse = elseBlock.getPosition();
CtStatement otherReturnStatement = elseBlock.getStatement(0);
assertEquals(otherReturnStatement.getPosition(), positionElse);
assertEquals("ImplicitBlock.java", positionThen.getFile().getName());
assertEquals(8, positionElse.getLine());
assertNotEquals(returnStatement, otherReturnStatement);
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DefaultPrettyPrinterTest method testTernaryParenthesesOnLocalVariable.
@Test
public void testTernaryParenthesesOnLocalVariable() {
// Spooning the code snippet
Launcher launcher = new Launcher();
CtCodeSnippetStatement snippet = launcher.getFactory().Code().createCodeSnippetStatement("final int foo = (new Object() instanceof Object ? new Object().equals(null) : new Object().equals(new Object())) ? 0 : new Object().hashCode();");
CtStatement compile = snippet.compile();
// Pretty-printing the Spooned code snippet and compiling the resulting code.
snippet = launcher.getFactory().Code().createCodeSnippetStatement(compile.toString());
assertEquals(compile, snippet.compile());
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtCase.
@Override
@SuppressWarnings("rawtypes")
public <E> void visitCtCase(CtCase<E> caseStatement) {
enterCtStatement(caseStatement);
if (caseStatement.getCaseExpression() != null) {
printer.writeKeyword("case").writeSpace();
// writing enum case expression
if (caseStatement.getCaseExpression() instanceof CtFieldAccess) {
final CtFieldReference variable = ((CtFieldAccess) caseStatement.getCaseExpression()).getVariable();
// In noclasspath mode, we don't have always the type of the declaring type.
if (variable.getType() != null && variable.getDeclaringType() != null && variable.getType().getQualifiedName().equals(variable.getDeclaringType().getQualifiedName())) {
printer.writeIdentifier(variable.getSimpleName());
} else {
scan(caseStatement.getCaseExpression());
}
} else {
scan(caseStatement.getCaseExpression());
}
} else {
printer.writeKeyword("default");
}
printer.writeSpace().writeSeparator(":").incTab();
for (CtStatement statement : caseStatement.getStatements()) {
printer.writeln();
elementPrinterHelper.writeStatement(statement);
}
printer.decTab();
}
use of spoon.reflect.code.CtStatement 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;
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class DeleteTest method testDeleteAStatementInMethod.
@Test
public void testDeleteAStatementInMethod() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod method = adobada.getMethod("m");
assertEquals(2, method.getBody().getStatements().size());
final CtStatement statement = method.getBody().getStatement(1);
statement.delete();
assertEquals(1, method.getBody().getStatements().size());
assertFalse(method.getBody().getStatements().contains(statement));
}
Aggregations