Search in sources :

Example 11 with CtComment

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;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtField(spoon.reflect.declaration.CtField) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CtMethod(spoon.reflect.declaration.CtMethod)

Example 12 with CtComment

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());
}
Also used : CtComment(spoon.reflect.code.CtComment) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 13 with CtComment

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());
    }
}
Also used : CtComment(spoon.reflect.code.CtComment) CtBlock(spoon.reflect.code.CtBlock) SourcePosition(spoon.reflect.cu.SourcePosition)

Example 14 with CtComment

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;
}
Also used : CtComment(spoon.reflect.code.CtComment) ArrayList(java.util.ArrayList)

Example 15 with CtComment

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());
}
Also used : CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

CtComment (spoon.reflect.code.CtComment)24 Test (org.junit.Test)12 Factory (spoon.reflect.factory.Factory)12 DefaultCoreFactory (spoon.support.DefaultCoreFactory)8 CtClass (spoon.reflect.declaration.CtClass)7 ArrayList (java.util.ArrayList)5 Launcher (spoon.Launcher)4 CtInvocation (spoon.reflect.code.CtInvocation)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 CtElement (spoon.reflect.declaration.CtElement)4 CtMethod (spoon.reflect.declaration.CtMethod)4 List (java.util.List)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3 CtExpression (spoon.reflect.code.CtExpression)3 CtIf (spoon.reflect.code.CtIf)3 CtParameter (spoon.reflect.declaration.CtParameter)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 CtAssignment (spoon.reflect.code.CtAssignment)2 CtBlock (spoon.reflect.code.CtBlock)2