use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class JDTCommentBuilder method insertCommentInAST.
/**
* Inserts the comment into the AST.
* @param comment the comment to insert
*/
private void insertCommentInAST(final CtComment comment) {
CtElement commentParent = findCommentParent(comment);
if (commentParent == null) {
File file = spoonUnit.getFile();
if (file != null && file.getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION)) {
spoonUnit.getDeclaredPackage().addComment(comment);
} else if (file != null && file.getName().equals(DefaultJavaPrettyPrinter.JAVA_MODULE_DECLARATION)) {
spoonUnit.getDeclaredModule().addComment(comment);
} else {
comment.setCommentType(CtComment.CommentType.FILE);
addCommentToNear(comment, new ArrayList<CtElement>(spoonUnit.getDeclaredTypes()));
}
return;
}
// visitor that inserts the comment in the element
CtInheritanceScanner insertionVisitor = new CtInheritanceScanner() {
private boolean isScanned = false;
@Override
public void scan(CtElement e) {
if (e == null) {
return;
}
// Do not visit the AST, only the first element
if (!isScanned) {
isScanned = true;
if (e.getPosition().getSourceStart() == comment.getPosition().getSourceStart()) {
e.addComment(comment);
return;
}
super.scan(e);
}
}
@Override
public <R> void visitCtStatementList(CtStatementList e) {
addCommentToNear(comment, new ArrayList<CtElement>(e.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addStatement(comment);
}
}
@Override
public <T> void visitCtMethod(CtMethod<T> e) {
e.addComment(comment);
}
@Override
public <T> void visitCtConstructor(CtConstructor<T> e) {
e.addComment(comment);
}
@Override
public <T> void visitCtConditional(CtConditional<T> e) {
List<CtElement> elements = new ArrayList<>();
elements.add(e.getElseExpression());
elements.add(e.getThenExpression());
elements.add(e.getCondition());
addCommentToNear(comment, elements);
}
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> e) {
List<CtElement> elements = new ArrayList<>();
elements.add(e.getLeftHandOperand());
elements.add(e.getRightHandOperand());
addCommentToNear(comment, elements);
}
@Override
public <T> void visitCtClass(CtClass<T> e) {
if (comment.getPosition().getLine() <= e.getPosition().getLine()) {
e.addComment(comment);
return;
}
final List<CtElement> elements = new ArrayList<>();
for (CtTypeMember typeMember : e.getTypeMembers()) {
if (typeMember instanceof CtField || typeMember instanceof CtMethod || typeMember instanceof CtConstructor) {
elements.add(typeMember);
}
}
addCommentToNear(comment, elements);
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtInterface(CtInterface<T> e) {
final List<CtElement> elements = new ArrayList<>();
for (CtTypeMember typeMember : e.getTypeMembers()) {
if (typeMember instanceof CtField || typeMember instanceof CtMethod) {
elements.add(typeMember);
}
}
addCommentToNear(comment, elements);
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtField(CtField<T> e) {
e.addComment(comment);
}
@Override
public <E> void visitCtSwitch(CtSwitch<E> e) {
List<CtCase<? super E>> cases = e.getCases();
CtCase previous = null;
for (int i = 0; i < cases.size(); i++) {
CtCase<? super E> ctCase = cases.get(i);
if (previous == null) {
if (comment.getPosition().getSourceStart() < ctCase.getPosition().getSourceStart() && e.getPosition().getSourceStart() < comment.getPosition().getSourceStart()) {
ctCase.addComment(comment);
return;
}
} else {
if (previous.getPosition().getSourceEnd() < comment.getPosition().getSourceStart() && ctCase.getPosition().getSourceStart() > comment.getPosition().getSourceStart()) {
addCommentToNear(comment, new ArrayList<CtElement>(previous.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
previous.addStatement(comment);
}
return;
}
}
previous = ctCase;
}
if (previous.getPosition().getSourceEnd() < comment.getPosition().getSourceStart()) {
addCommentToNear(comment, new ArrayList<CtElement>(previous.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
previous.addStatement(comment);
}
return;
}
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public void visitCtIf(CtIf e) {
if (!(e.getThenStatement() instanceof CtBlock)) {
if (comment.getPosition().getSourceEnd() <= e.getThenStatement().getPosition().getSourceStart()) {
e.getThenStatement().addComment(comment);
return;
}
}
if (e.getElseStatement() != null) {
SourcePosition thenPosition = e.getThenStatement().getPosition() == null ? ((CtBlock) e.getThenStatement()).getStatement(0).getPosition() : e.getThenStatement().getPosition();
SourcePosition elsePosition = e.getElseStatement().getPosition() == null ? ((CtBlock) e.getElseStatement()).getStatement(0).getPosition() : e.getElseStatement().getPosition();
if (comment.getPosition().getSourceStart() > thenPosition.getSourceEnd() && comment.getPosition().getSourceEnd() < elsePosition.getSourceStart()) {
e.getElseStatement().addComment(comment);
}
}
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public void scanCtStatement(CtStatement s) {
if (!(s instanceof CtStatementList || s instanceof CtSwitch)) {
s.addComment(comment);
}
}
@Override
public void visitCtAnonymousExecutable(CtAnonymousExecutable e) {
e.addComment(comment);
}
@Override
public <T> void visitCtNewArray(CtNewArray<T> e) {
addCommentToNear(comment, new ArrayList<CtElement>(e.getElements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtParameter(CtParameter<T> e) {
e.addComment(comment);
}
@Override
public void visitCtCatch(CtCatch e) {
if (comment.getPosition().getLine() <= e.getPosition().getLine()) {
e.addComment(comment);
return;
}
}
@Override
public void visitCtModule(CtModule module) {
addCommentToNear(comment, new ArrayList<>(module.getModuleDirectives()));
}
};
insertionVisitor.scan(commentParent);
try {
comment.getParent();
} catch (ParentNotInitializedException e) {
LOGGER.error(comment + " is not added into the AST", e);
}
}
use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class TestLabels method testLabelsAreDetected.
@Test
public void testLabelsAreDetected() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/labels/testclasses/ManyLabels.java");
launcher.buildModel();
CtMethod mainMethod = launcher.getFactory().getModel().getElements(new NamedElementFilter<>(CtMethod.class, "main")).get(0);
CtBlock body = mainMethod.getBody();
assertEquals(2, body.getStatements().size());
CtBlock block = (CtBlock) body.getStatement(0);
CtSwitch ctSwitch = (CtSwitch) body.getStatement(1);
assertEquals("labelBlock", block.getLabel());
assertEquals("sw", ctSwitch.getLabel());
assertTrue(block.getStatement(1) instanceof CtIf);
CtIf firstIf = (CtIf) block.getStatement(1);
CtBlock then = firstIf.getThenStatement();
CtBreak firstBreak = (CtBreak) then.getStatement(1);
assertEquals("labelBlock", firstBreak.getTargetLabel());
assertSame(block, firstBreak.getLabelledStatement());
CtIf secondIf = (CtIf) block.getStatement(2);
assertEquals("labelIf", secondIf.getLabel());
CtBlock thenBlock = secondIf.getThenStatement();
CtIf innerIf = (CtIf) thenBlock.getStatement(0);
CtBlock innerThenBlock = innerIf.getThenStatement();
CtBreak breakInnerIf = (CtBreak) innerThenBlock.getStatement(0);
assertSame(secondIf, breakInnerIf.getLabelledStatement());
CtCase firstCase = (CtCase) ctSwitch.getCases().get(0);
List<CtStatement> statementList = firstCase.getStatements();
assertEquals(2, statementList.size());
CtDo ctDo = (CtDo) statementList.get(0);
assertEquals("label", ctDo.getLabel());
CtBreak finalBreak = (CtBreak) statementList.get(1);
assertNull(finalBreak.getTargetLabel());
assertNull(finalBreak.getLabelledStatement());
CtBlock doBlock = (CtBlock) ctDo.getBody();
CtWhile ctWhile = (CtWhile) doBlock.getStatement(1);
assertEquals("lWhile", ctWhile.getLabel());
CtBlock whileBlock = (CtBlock) ctWhile.getBody();
CtFor forLoop = (CtFor) whileBlock.getStatement(0);
CtBreak breakSwitch = (CtBreak) whileBlock.getStatement(1);
assertEquals("sw", breakSwitch.getTargetLabel());
assertSame(ctSwitch, breakSwitch.getLabelledStatement());
assertEquals("forloop", forLoop.getLabel());
CtBlock forBlock = (CtBlock) forLoop.getBody();
assertEquals(7, forBlock.getStatements().size());
CtIf firstForIf = (CtIf) forBlock.getStatement(1);
CtIf secondForIf = (CtIf) forBlock.getStatement(2);
CtIf thirdForIf = (CtIf) forBlock.getStatement(3);
CtIf fourthForIf = (CtIf) forBlock.getStatement(4);
CtBreak breakItself = (CtBreak) forBlock.getStatement(6);
CtContinue continueFor = (CtContinue) ((CtBlock) firstForIf.getThenStatement()).getStatement(0);
assertSame(forLoop, continueFor.getLabelledStatement());
CtContinue continueWhile = (CtContinue) ((CtBlock) secondForIf.getThenStatement()).getStatement(0);
assertSame(ctWhile, continueWhile.getLabelledStatement());
CtContinue continueDo = (CtContinue) ((CtBlock) thirdForIf.getThenStatement()).getStatement(0);
assertSame(ctDo, continueDo.getLabelledStatement());
CtBreak breakDo = (CtBreak) ((CtBlock) fourthForIf.getThenStatement()).getStatement(0);
assertSame(ctDo, breakDo.getLabelledStatement());
assertEquals("labelbreak", breakItself.getLabel());
assertEquals("labelbreak", breakItself.getTargetLabel());
assertSame(breakItself, breakItself.getLabelledStatement());
}
use of spoon.reflect.code.CtCase in project spoon by INRIA.
the class SwitchCaseTest method testIterationStatements.
@Test
public void testIterationStatements() {
Factory factory = createFactory();
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " int x=0;" + "switch(x) {" + "case 0: x=x+1;break;" + "case 1: x=0;" + "default: x=-1;" + "}" + "}};").compile();
CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
CtSwitch<?> sw = foo.getElements(new TypeFilter<CtSwitch<?>>(CtSwitch.class)).get(0);
assertEquals(3, sw.getCases().size());
CtCase<?> c = (CtCase<?>) sw.getCases().get(0);
assertEquals(0, ((CtLiteral<?>) c.getCaseExpression()).getValue());
assertEquals(2, c.getStatements().size());
List<CtStatement> l = new ArrayList<CtStatement>();
// this compiles (thanks to the new CtCase extends CtStatementList)
for (CtStatement s : c) {
l.add(s);
}
assertTrue(c.getStatements().equals(l));
}
Aggregations