use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInStatements.
@Test
public void testUsageOfTypeAnnotationWithGenericTypesInStatements() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtMethod<?> method = ctClass.getMethodsByName("m4").get(0);
// New type parameter declaration.
final List<CtTypeParameter> typeParameters = method.getFormalCtTypeParameters();
assertEquals("Method has 1 generic parameter", 1, typeParameters.size());
assertEquals("Method with an type annotation must be well printed", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
final CtBlock<?> body = method.getBody();
final String expectedFirstStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T> list = new java.util.ArrayList<>()";
final CtStatement firstStatement = body.getStatement(0);
assertEquals("Type annotation on generic parameter declared in the method", expectedFirstStatement, firstStatement.toString());
final CtConstructorCall firstConstructorCall = firstStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
final CtTypeReference<?> firstTypeReference = firstConstructorCall.getType().getActualTypeArguments().get(0);
assertTrue(firstTypeReference.isImplicit());
assertEquals("T", firstTypeReference.getSimpleName());
final String expectedSecondStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "?> list2 = new java.util.ArrayList<>()";
final CtStatement secondStatement = body.getStatement(1);
assertEquals("Wildcard with an type annotation must be well printed", expectedSecondStatement, secondStatement.toString());
final CtConstructorCall secondConstructorCall = secondStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
final CtTypeReference<?> secondTypeReference = secondConstructorCall.getType().getActualTypeArguments().get(0);
assertTrue(secondTypeReference.isImplicit());
assertEquals("Object", secondTypeReference.getSimpleName());
final String expectedThirdStatement = "java.util.List<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation> list3 = new java.util.ArrayList<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation>()";
assertEquals("Type in generic parameter with an type annotation must be well printed", expectedThirdStatement, body.getStatement(2).toString());
}
use of spoon.reflect.code.CtStatement 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.CtStatement in project spoon by INRIA.
the class ParentExiter method scanCtLoop.
@Override
public void scanCtLoop(CtLoop loop) {
if (loop.getBody() == null && child instanceof CtStatement) {
CtStatement child = (CtStatement) this.child;
if (!(this.child instanceof CtBlock)) {
child = jdtTreeBuilder.getFactory().Code().createCtBlock(child);
child.setImplicit(true);
}
loop.setBody(child);
}
super.scanCtLoop(loop);
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class CtBlockImpl method insertBegin.
@Override
public <T extends CtStatementList> T insertBegin(CtStatementList statements) {
if (this.shouldInsertAfterSuper()) {
getStatements().get(0).insertAfter(statements);
return (T) this;
}
ensureModifiableStatementsList();
for (CtStatement statement : statements.getStatements()) {
statement.setParent(this);
this.addStatement(0, statement);
}
if (isImplicit() && this.statements.size() > 1) {
setImplicit(false);
}
return (T) this;
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class CtCaseImpl method insertBegin.
@Override
public <T extends CtStatementList> T insertBegin(CtStatementList statements) {
this.ensureModifiableStatementsList();
for (CtStatement statement : statements.getStatements()) {
statement.setParent(this);
this.addStatement(0, statement);
}
if (isImplicit() && this.statements.size() > 1) {
setImplicit(false);
}
return (T) this;
}
Aggregations