Search in sources :

Example 6 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class CommentTest method testWildComments.

@Test
public void testWildComments() {
    // contract: tests that value of comment is correct even for wild combinations of characters. See WildComments class for details
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(WildComments.class);
    List<CtLiteral<String>> literals = (List) ((CtNewArray<?>) type.getField("comments").getDefaultExpression()).getElements();
    assertTrue(literals.size() > 10);
    /*
		 * each string literal has a comment and string value, which defines expected value of it's comment
		 */
    for (CtLiteral<String> literal : literals) {
        assertEquals(1, literal.getComments().size());
        CtComment comment = literal.getComments().get(0);
        String expected = literal.getValue();
        assertEquals(literal.getPosition().toString(), expected, comment.getContent());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) CtLiteral(spoon.reflect.code.CtLiteral) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) List(java.util.List) ArrayList(java.util.ArrayList) WildComments(spoon.test.comment.testclasses.WildComments) Test(org.junit.Test)

Example 7 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class CommentTest method testAddCommentsToSnippet.

@Test
public void testAddCommentsToSnippet() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setCommentEnabled(true);
    CtStatement statement = factory.Code().createCodeSnippetStatement("System.out.println(\"Caenorhabditis\")");
    CtComment comment = factory.createComment("My comment on my statement", CtComment.CommentType.INLINE);
    statement.addComment(comment);
    CtExpression expression = factory.Code().createCodeSnippetExpression("\"Caenorhabditis\" + \"Caenorhabditis\"");
    CtComment commentExpression = factory.createComment("My comment on my expression", CtComment.CommentType.INLINE);
    expression.addComment(commentExpression);
    assertEquals("// My comment on my statement" + newLine + "System.out.println(\"Caenorhabditis\")", statement.toString());
    assertEquals("// My comment on my expression" + newLine + "\"Caenorhabditis\" + \"Caenorhabditis\"", expression.toString());
}
Also used : CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 8 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class CtTypeTest method testIsSubTypeOfonTypeReferences.

@Test
public void testIsSubTypeOfonTypeReferences() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-c" });
    launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
    CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
    O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
        nameToTypeRef.put(var.getSimpleName(), var.getType());
    });
    int[] count = new int[1];
    O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
        for (CtComment comment : ass.getComments()) {
            checkIsNotSubtype(comment, nameToTypeRef);
            count[0]++;
        }
        ;
        count[0]++;
        checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
    });
    assertTrue(count[0] > (9 * 8));
}
Also used : CtComment(spoon.reflect.code.CtComment) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtAssignment(spoon.reflect.code.CtAssignment) HashMap(java.util.HashMap) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 9 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class CtElementImpl method getDocComment.

public String getDocComment() {
    for (CtComment ctComment : comments) {
        if (ctComment.getCommentType() == CtComment.CommentType.JAVADOC) {
            StringBuffer result = new StringBuffer();
            result.append(ctComment.getContent() + System.lineSeparator());
            for (CtJavaDocTag tag : ((CtJavaDoc) ctComment).getTags()) {
                // the tag already contains a new line
                result.append(tag.toString());
            }
            return result.toString();
        }
    }
    return "";
}
Also used : CtComment(spoon.reflect.code.CtComment) CtJavaDoc(spoon.reflect.code.CtJavaDoc) CtJavaDocTag(spoon.reflect.code.CtJavaDocTag)

Example 10 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class CtElementImpl method setComments.

@Override
public <E extends CtElement> E setComments(List<CtComment> comments) {
    if (comments == null || comments.isEmpty()) {
        this.comments = CtElementImpl.emptyList();
        return (E) this;
    }
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, COMMENT, this.comments, new ArrayList<>(this.comments));
    this.comments.clear();
    for (CtComment comment : comments) {
        addComment(comment);
    }
    return (E) this;
}
Also used : CtComment(spoon.reflect.code.CtComment)

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