Search in sources :

Example 6 with LineComment

use of com.github.javaparser.ast.comments.LineComment in project javaparser by javaparser.

the class NodeTest method removeOrphanCommentPositiveCase.

@Test
public void removeOrphanCommentPositiveCase() {
    ClassOrInterfaceDeclaration decl = new ClassOrInterfaceDeclaration(EnumSet.noneOf(Modifier.class), false, "A");
    Comment c = new LineComment("A comment");
    decl.addOrphanComment(c);
    assertEquals(1, decl.getOrphanComments().size());
    assertTrue(decl == c.getParentNode().get());
    assertTrue(decl.removeOrphanComment(c));
    assertEquals(0, decl.getOrphanComments().size());
    assertFalse(c.getParentNode().isPresent());
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) BlockComment(com.github.javaparser.ast.comments.BlockComment) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) LineComment(com.github.javaparser.ast.comments.LineComment) Test(org.junit.Test)

Example 7 with LineComment

use of com.github.javaparser.ast.comments.LineComment in project javaparser by javaparser.

the class NodeTest method removeOrphanCommentNegativeCase.

@Test
public void removeOrphanCommentNegativeCase() {
    ClassOrInterfaceDeclaration aClass = new ClassOrInterfaceDeclaration(EnumSet.noneOf(Modifier.class), false, "A");
    FieldDeclaration aField = new FieldDeclaration(EnumSet.noneOf(Modifier.class), new VariableDeclarator(PrimitiveType.intType(), "f"));
    aClass.getMembers().add(aField);
    Comment c = new LineComment("A comment");
    aField.addOrphanComment(c);
    // the comment is an orphan comment of the field, so trying to remove it on the class should not work
    assertFalse(aClass.removeOrphanComment(c));
    assertEquals(1, aField.getOrphanComments().size());
    assertTrue(c.getParentNode().isPresent());
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) BlockComment(com.github.javaparser.ast.comments.BlockComment) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 8 with LineComment

use of com.github.javaparser.ast.comments.LineComment in project javaparser by javaparser.

the class PrettyPrintVisitorTest method singlelineCommentGetsFormatted.

@Test
public void singlelineCommentGetsFormatted() {
    CompilationUnit cu = new CompilationUnit();
    cu.addClass("X").addMethod("abc").setComment(new LineComment("   line1  \n "));
    assertEqualsNoEol("public class X {\n" + "\n" + "    // line1\n" + "    void abc() {\n" + "    }\n" + "}\n", cu.toString());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) LineComment(com.github.javaparser.ast.comments.LineComment) Test(org.junit.Test)

Aggregations

LineComment (com.github.javaparser.ast.comments.LineComment)8 Test (org.junit.Test)5 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)4 BlockComment (com.github.javaparser.ast.comments.BlockComment)4 Comment (com.github.javaparser.ast.comments.Comment)4 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)4 com.github.javaparser.ast (com.github.javaparser.ast)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 Modifier (com.github.javaparser.ast.Modifier)1 Node (com.github.javaparser.ast.Node)1 com.github.javaparser.ast.body (com.github.javaparser.ast.body)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 com.github.javaparser.ast.expr (com.github.javaparser.ast.expr)1 com.github.javaparser.ast.modules (com.github.javaparser.ast.modules)1 com.github.javaparser.ast.stmt (com.github.javaparser.ast.stmt)1 com.github.javaparser.ast.type (com.github.javaparser.ast.type)1 Pair (com.github.javaparser.utils.Pair)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1