use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class ReplaceScanner method createInvocation.
private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candidate, List<CtExpression<?>> invArgs, CtInvocation getter, Class getterTypeClass) {
CtInvocation<?> invocation;
Type type;
if (getterTypeClass.equals(Collection.class) || getterTypeClass.equals(List.class)) {
invocation = factory.Code().createInvocation(null, this.list, invArgs);
type = Type.LIST;
} else if (getterTypeClass.equals(Map.class)) {
invocation = factory.Code().createInvocation(null, this.map, invArgs);
type = Type.MAP;
} else if (getterTypeClass.equals(Set.class)) {
invocation = factory.Code().createInvocation(null, this.set, invArgs);
type = Type.SET;
} else {
invocation = factory.Code().createInvocation(null, this.element, invArgs);
type = Type.ELEMENT;
}
// Listener
final String name = getter.getExecutable().getSimpleName().substring(3);
final String listenerName = getter.getExecutable().getDeclaringType().getSimpleName() + name + "ReplaceListener";
CtClass listener;
if (listeners.containsKey(listenerName)) {
listener = listeners.get(listenerName);
} else {
final CtTypeReference getterType = getGetterType(factory, getter);
listener = createListenerClass(factory, listenerName, getterType, type);
final CtMethod setter = getSetter(name, getter.getTarget().getType().getDeclaration());
final CtField field = updateField(listener, setter.getDeclaringType().getReference());
updateConstructor(listener, setter.getDeclaringType().getReference());
updateSetter(factory, (CtMethod<?>) listener.getMethodsByName("set").get(0), getterType, field, setter);
// Add auto-generated comment.
final CtComment comment = factory.Core().createComment();
comment.setCommentType(CtComment.CommentType.INLINE);
comment.setContent("auto-generated, see " + ReplacementVisitorGenerator.class.getName());
listener.addComment(comment);
listeners.put(listenerName, listener);
}
invocation.addArgument(getConstructorCall(listener, factory.Code().createVariableRead(candidate.getParameters().get(0).getReference(), false)));
return invocation;
}
use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class JavaDocTest method testJavadocNotPresentInAST.
@Test
public void testJavadocNotPresentInAST() throws Exception {
Launcher launcher = new Launcher();
launcher.getEnvironment().setCommentEnabled(false);
launcher.getEnvironment().setNoClasspath(true);
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/javadoc/testclasses/");
launcher.run();
new CtScanner() {
@Override
public void scan(CtElement element) {
if (element != null) {
assertEquals(0, element.getComments().size());
}
super.scan(element);
}
@Override
public void visitCtComment(CtComment comment) {
fail("Shouldn't have comment in the model.");
super.visitCtComment(comment);
}
}.scan(launcher.getModel().getRootPackage());
}
use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtIf.
@Override
public void visitCtIf(CtIf ifElement) {
enterCtStatement(ifElement);
printer.writeKeyword("if").writeSpace().writeSeparator("(");
scan(ifElement.getCondition());
printer.writeSeparator(")");
elementPrinterHelper.writeIfOrLoopBlock(ifElement.getThenStatement());
if (ifElement.getElseStatement() != null) {
List<CtComment> comments = elementPrinterHelper.getComments(ifElement, CommentOffset.INSIDE);
for (CtComment comment : comments) {
SourcePosition thenPosition = ifElement.getThenStatement().getPosition() == null ? ((CtBlock) ifElement.getThenStatement()).getStatement(0).getPosition() : ifElement.getThenStatement().getPosition();
if (comment.getPosition().getSourceStart() > thenPosition.getSourceEnd()) {
elementPrinterHelper.writeComment(comment);
}
}
printer.writeKeyword("else");
elementPrinterHelper.writeIfOrLoopBlock(ifElement.getElseStatement());
}
}
use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class ElementPrinterHelper method getComments.
public List<CtComment> getComments(CtElement element, CommentOffset offset) {
List<CtComment> commentsToPrint = new ArrayList<>();
if (!env.isCommentsEnabled() || element == null) {
return commentsToPrint;
}
for (CtComment comment : element.getComments()) {
if (comment.getCommentType() == CtComment.CommentType.FILE && offset == CommentOffset.TOP_FILE) {
commentsToPrint.add(comment);
continue;
}
if (comment.getCommentType() == CtComment.CommentType.FILE) {
continue;
}
if (comment.getPosition() == null || element.getPosition() == null) {
if (offset == CommentOffset.BEFORE) {
commentsToPrint.add(comment);
}
continue;
}
final int line = element.getPosition().getLine();
final int sourceEnd = element.getPosition().getSourceEnd();
final int sourceStart = element.getPosition().getSourceStart();
if (offset == CommentOffset.BEFORE && (comment.getPosition().getLine() < line || (sourceStart <= comment.getPosition().getSourceStart() && sourceEnd >= comment.getPosition().getSourceEnd()))) {
commentsToPrint.add(comment);
} else if (offset == CommentOffset.AFTER && comment.getPosition().getSourceStart() > sourceEnd) {
commentsToPrint.add(comment);
} else {
final int endLine = element.getPosition().getEndLine();
if (offset == CommentOffset.INSIDE && comment.getPosition().getLine() >= line && comment.getPosition().getEndLine() <= endLine) {
commentsToPrint.add(comment);
}
}
}
return commentsToPrint;
}
use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class CommentTest method testCoreFactory.
@Test
public void testCoreFactory() {
Factory spoonFactory = getSpoonFactory();
CtComment comment = spoonFactory.Core().createComment();
assertEquals("/* */", comment.toString());
comment.setContent("comment");
assertEquals("/* comment */", comment.toString());
comment.setCommentType(CtComment.CommentType.INLINE);
assertEquals(CtComment.CommentType.INLINE, comment.getCommentType());
assertEquals("// comment", comment.toString());
comment.setCommentType(CtComment.CommentType.BLOCK);
assertEquals(CtComment.CommentType.BLOCK, comment.getCommentType());
}
Aggregations