Search in sources :

Example 1 with SYSTEM_EOL

use of com.github.javaparser.utils.Utils.SYSTEM_EOL in project javaparser by javaparser.

the class LexicalPreservingPrinterTest method moveOverrideAnnotations.

// See issue #866
@Test
void moveOverrideAnnotations() {
    String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   protected @Override void initializePage() {}" + SYSTEM_EOL + "}";
    CompilationUnit cu = parse(code);
    LexicalPreservingPrinter.setup(cu);
    cu.getTypes().forEach(type -> type.getMembers().forEach(member -> member.ifMethodDeclaration(methodDeclaration -> {
        if (methodDeclaration.getAnnotationByName("Override").isPresent()) {
            while (methodDeclaration.getAnnotations().isNonEmpty()) {
                AnnotationExpr annotationExpr = methodDeclaration.getAnnotations().get(0);
                annotationExpr.remove();
            }
            methodDeclaration.addMarkerAnnotation("Override");
        }
    })));
    assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   @Override" + SYSTEM_EOL + "   protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Assertions.fail(org.junit.jupiter.api.Assertions.fail) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) SYSTEM_EOL(com.github.javaparser.utils.Utils.SYSTEM_EOL) Arrays(java.util.Arrays) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Parameter(com.github.javaparser.ast.body.Parameter) PUBLIC(com.github.javaparser.ast.Modifier.Keyword.PUBLIC) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration) CatchClause(com.github.javaparser.ast.stmt.CatchClause) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) UnionType(com.github.javaparser.ast.type.UnionType) Type(com.github.javaparser.ast.type.Type) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) ModifierVisitor(com.github.javaparser.ast.visitor.ModifierVisitor) VoidType(com.github.javaparser.ast.type.VoidType) TestUtils(com.github.javaparser.utils.TestUtils) IOException(java.io.IOException) ParserConfiguration(com.github.javaparser.ParserConfiguration) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.jupiter.api.Test) ArrayCreationLevel(com.github.javaparser.ast.ArrayCreationLevel) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) Visitable(com.github.javaparser.ast.visitor.Visitable) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) NODE_TEXT_DATA(com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.NODE_TEXT_DATA) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TestUtils.assertEqualsStringIgnoringEol(com.github.javaparser.utils.TestUtils.assertEqualsStringIgnoringEol) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TryStmt(com.github.javaparser.ast.stmt.TryStmt) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) com.github.javaparser.ast.expr(com.github.javaparser.ast.expr) JavaParser(com.github.javaparser.JavaParser) Test(org.junit.jupiter.api.Test)

Example 2 with SYSTEM_EOL

use of com.github.javaparser.utils.Utils.SYSTEM_EOL in project javaparser by javaparser.

the class LexicalPreservingPrinterTest method handleAddingMarkerAnnotation.

// See issue #865
@Test
void handleAddingMarkerAnnotation() {
    String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   @Override" + SYSTEM_EOL + "   protected void initializePage() {}" + SYSTEM_EOL + "}";
    CompilationUnit cu = parse(code);
    LexicalPreservingPrinter.setup(cu);
    cu.getTypes().forEach(type -> type.getMembers().forEach(member -> {
        if (member instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) member;
            if (!methodDeclaration.getAnnotationByName("Override").isPresent()) {
                methodDeclaration.addMarkerAnnotation("Override");
            }
        }
    }));
    assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   @Override" + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   @Override" + SYSTEM_EOL + "   protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Assertions.fail(org.junit.jupiter.api.Assertions.fail) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) SYSTEM_EOL(com.github.javaparser.utils.Utils.SYSTEM_EOL) Arrays(java.util.Arrays) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Parameter(com.github.javaparser.ast.body.Parameter) PUBLIC(com.github.javaparser.ast.Modifier.Keyword.PUBLIC) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration) CatchClause(com.github.javaparser.ast.stmt.CatchClause) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) UnionType(com.github.javaparser.ast.type.UnionType) Type(com.github.javaparser.ast.type.Type) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) ModifierVisitor(com.github.javaparser.ast.visitor.ModifierVisitor) VoidType(com.github.javaparser.ast.type.VoidType) TestUtils(com.github.javaparser.utils.TestUtils) IOException(java.io.IOException) ParserConfiguration(com.github.javaparser.ParserConfiguration) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.jupiter.api.Test) ArrayCreationLevel(com.github.javaparser.ast.ArrayCreationLevel) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) Visitable(com.github.javaparser.ast.visitor.Visitable) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) NODE_TEXT_DATA(com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.NODE_TEXT_DATA) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TestUtils.assertEqualsStringIgnoringEol(com.github.javaparser.utils.TestUtils.assertEqualsStringIgnoringEol) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TryStmt(com.github.javaparser.ast.stmt.TryStmt) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) com.github.javaparser.ast.expr(com.github.javaparser.ast.expr) JavaParser(com.github.javaparser.JavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.jupiter.api.Test)

Example 3 with SYSTEM_EOL

use of com.github.javaparser.utils.Utils.SYSTEM_EOL in project javaparser by javaparser.

the class LexicalPreservingPrinterTest method handleOverrideAnnotationAlternative.

// See issue #865
@Test
void handleOverrideAnnotationAlternative() {
    String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   protected void initializePage() {}" + SYSTEM_EOL + "}";
    CompilationUnit cu = parse(code);
    LexicalPreservingPrinter.setup(cu);
    cu.getTypes().forEach(type -> type.getMembers().forEach(member -> member.ifMethodDeclaration(methodDeclaration -> methodDeclaration.addAnnotation("Override"))));
    assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   @Override()" + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   @Override()" + SYSTEM_EOL + "   protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Assertions.fail(org.junit.jupiter.api.Assertions.fail) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) SYSTEM_EOL(com.github.javaparser.utils.Utils.SYSTEM_EOL) Arrays(java.util.Arrays) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Parameter(com.github.javaparser.ast.body.Parameter) PUBLIC(com.github.javaparser.ast.Modifier.Keyword.PUBLIC) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration) CatchClause(com.github.javaparser.ast.stmt.CatchClause) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) UnionType(com.github.javaparser.ast.type.UnionType) Type(com.github.javaparser.ast.type.Type) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) ModifierVisitor(com.github.javaparser.ast.visitor.ModifierVisitor) VoidType(com.github.javaparser.ast.type.VoidType) TestUtils(com.github.javaparser.utils.TestUtils) IOException(java.io.IOException) ParserConfiguration(com.github.javaparser.ParserConfiguration) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.jupiter.api.Test) ArrayCreationLevel(com.github.javaparser.ast.ArrayCreationLevel) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) Visitable(com.github.javaparser.ast.visitor.Visitable) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) NODE_TEXT_DATA(com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.NODE_TEXT_DATA) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TestUtils.assertEqualsStringIgnoringEol(com.github.javaparser.utils.TestUtils.assertEqualsStringIgnoringEol) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TryStmt(com.github.javaparser.ast.stmt.TryStmt) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) com.github.javaparser.ast.expr(com.github.javaparser.ast.expr) JavaParser(com.github.javaparser.JavaParser) Test(org.junit.jupiter.api.Test)

Example 4 with SYSTEM_EOL

use of com.github.javaparser.utils.Utils.SYSTEM_EOL in project javaparser by javaparser.

the class LexicalPreservingPrinterTest method moveOrAddOverrideAnnotations.

// See issue #866
@Test
void moveOrAddOverrideAnnotations() {
    String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   protected @Override void initializePage() {}" + SYSTEM_EOL + "}";
    CompilationUnit cu = parse(code);
    LexicalPreservingPrinter.setup(cu);
    cu.getTypes().forEach(type -> type.getMembers().forEach(member -> {
        if (member instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) member;
            if (methodDeclaration.getAnnotationByName("Override").isPresent()) {
                while (methodDeclaration.getAnnotations().isNonEmpty()) {
                    AnnotationExpr annotationExpr = methodDeclaration.getAnnotations().get(0);
                    annotationExpr.remove();
                }
            }
            methodDeclaration.addMarkerAnnotation("Override");
        }
    }));
    assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + "   @Override" + SYSTEM_EOL + "   protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + "   @Override" + SYSTEM_EOL + "   protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Assertions.fail(org.junit.jupiter.api.Assertions.fail) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) SYSTEM_EOL(com.github.javaparser.utils.Utils.SYSTEM_EOL) Arrays(java.util.Arrays) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Parameter(com.github.javaparser.ast.body.Parameter) PUBLIC(com.github.javaparser.ast.Modifier.Keyword.PUBLIC) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration) CatchClause(com.github.javaparser.ast.stmt.CatchClause) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) UnionType(com.github.javaparser.ast.type.UnionType) Type(com.github.javaparser.ast.type.Type) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) ModifierVisitor(com.github.javaparser.ast.visitor.ModifierVisitor) VoidType(com.github.javaparser.ast.type.VoidType) TestUtils(com.github.javaparser.utils.TestUtils) IOException(java.io.IOException) ParserConfiguration(com.github.javaparser.ParserConfiguration) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.jupiter.api.Test) ArrayCreationLevel(com.github.javaparser.ast.ArrayCreationLevel) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) Visitable(com.github.javaparser.ast.visitor.Visitable) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) NODE_TEXT_DATA(com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.NODE_TEXT_DATA) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TestUtils.assertEqualsStringIgnoringEol(com.github.javaparser.utils.TestUtils.assertEqualsStringIgnoringEol) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TryStmt(com.github.javaparser.ast.stmt.TryStmt) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) com.github.javaparser.ast.expr(com.github.javaparser.ast.expr) JavaParser(com.github.javaparser.JavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.jupiter.api.Test)

Example 5 with SYSTEM_EOL

use of com.github.javaparser.utils.Utils.SYSTEM_EOL in project javaparser by javaparser.

the class JavadocTest method inlineTagsAreParsable.

@Test
void inlineTagsAreParsable() {
    String docText = "Returns the {@link TOFilename}s of all files that existed during the requested" + SYSTEM_EOL + "{@link TOVersion}. Set {@systemProperty JAVA_HOME} correctly." + SYSTEM_EOL + "" + SYSTEM_EOL + "@param versionID the id of the {@link TOVersion}." + SYSTEM_EOL + "@return the filenames" + SYSTEM_EOL + "@throws InvalidIDException if the {@link IPersistence} doesn't recognize the given versionID." + SYSTEM_EOL;
    Javadoc javadoc = parseJavadoc(docText);
    List<JavadocInlineTag> inlineTags = javadoc.getDescription().getElements().stream().filter(element -> element instanceof JavadocInlineTag).map(element -> (JavadocInlineTag) element).collect(toList());
    assertEquals("link", inlineTags.get(0).getName());
    assertEquals(" TOFilename", inlineTags.get(0).getContent());
    assertEquals(LINK, inlineTags.get(0).getType());
    assertEquals("link", inlineTags.get(1).getName());
    assertEquals(" TOVersion", inlineTags.get(1).getContent());
    assertEquals(LINK, inlineTags.get(1).getType());
    assertEquals("systemProperty", inlineTags.get(2).getName());
    assertEquals(" JAVA_HOME", inlineTags.get(2).getContent());
    assertEquals(SYSTEM_PROPERTY, inlineTags.get(2).getType());
    String javadocText = javadoc.toText();
    assertTrue(javadocText.contains("{@link TOVersion}"));
}
Also used : SYSTEM_EOL(com.github.javaparser.utils.Utils.SYSTEM_EOL) JavadocDescription(com.github.javaparser.javadoc.description.JavadocDescription) JavadocInlineTag(com.github.javaparser.javadoc.description.JavadocInlineTag) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Test(org.junit.jupiter.api.Test) StaticJavaParser.parseJavadoc(com.github.javaparser.StaticJavaParser.parseJavadoc) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) JavadocSnippet(com.github.javaparser.javadoc.description.JavadocSnippet) Type(com.github.javaparser.javadoc.description.JavadocInlineTag.Type) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavadocDescriptionElement(com.github.javaparser.javadoc.description.JavadocDescriptionElement) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) StaticJavaParser.parseJavadoc(com.github.javaparser.StaticJavaParser.parseJavadoc) JavadocInlineTag(com.github.javaparser.javadoc.description.JavadocInlineTag) Test(org.junit.jupiter.api.Test)

Aggregations

StaticJavaParser.parse (com.github.javaparser.StaticJavaParser.parse)7 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 SYSTEM_EOL (com.github.javaparser.utils.Utils.SYSTEM_EOL)7 GeneratedJavaParserConstants (com.github.javaparser.GeneratedJavaParserConstants)6 JavaParser (com.github.javaparser.JavaParser)6 ParserConfiguration (com.github.javaparser.ParserConfiguration)6 StaticJavaParser (com.github.javaparser.StaticJavaParser)6 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)6 ArrayCreationLevel (com.github.javaparser.ast.ArrayCreationLevel)6 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)6 Modifier (com.github.javaparser.ast.Modifier)6 PUBLIC (com.github.javaparser.ast.Modifier.Keyword.PUBLIC)6 Node (com.github.javaparser.ast.Node)6 NodeList (com.github.javaparser.ast.NodeList)6 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)6 AnnotationMemberDeclaration (com.github.javaparser.ast.body.AnnotationMemberDeclaration)6 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)6 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)6 InitializerDeclaration (com.github.javaparser.ast.body.InitializerDeclaration)6 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)6