Search in sources :

Example 1 with PUBLIC

use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC 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 PUBLIC

use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC 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 PUBLIC

use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC 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 PUBLIC

use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC 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 PUBLIC

use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC in project javaparser by javaparser.

the class VisitorGenerator method generate.

public final void generate() throws Exception {
    Log.info("Running %s", () -> getClass().getSimpleName());
    final CompilationUnit compilationUnit = sourceRoot.tryToParse(pkg, visitorClassName + ".java").getResult().get();
    Optional<ClassOrInterfaceDeclaration> visitorClassOptional = compilationUnit.getClassByName(visitorClassName);
    if (!visitorClassOptional.isPresent()) {
        visitorClassOptional = compilationUnit.getInterfaceByName(visitorClassName);
    }
    final ClassOrInterfaceDeclaration visitorClass = visitorClassOptional.get();
    JavaParserMetaModel.getNodeMetaModels().stream().filter((baseNodeMetaModel) -> !baseNodeMetaModel.isAbstract()).forEach(node -> generateVisitMethodForNode(node, visitorClass, compilationUnit));
    after();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Log(com.github.javaparser.utils.Log) SourceRoot(com.github.javaparser.utils.SourceRoot) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Name(com.github.javaparser.ast.expr.Name) Optional(java.util.Optional) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) PUBLIC(com.github.javaparser.ast.Modifier.Keyword.PUBLIC) BaseNodeMetaModel(com.github.javaparser.metamodel.BaseNodeMetaModel) JavaParserMetaModel(com.github.javaparser.metamodel.JavaParserMetaModel) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)9 PUBLIC (com.github.javaparser.ast.Modifier.Keyword.PUBLIC)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)9 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)8 List (java.util.List)8 Modifier (com.github.javaparser.ast.Modifier)7 NodeList (com.github.javaparser.ast.NodeList)7 Parameter (com.github.javaparser.ast.body.Parameter)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)7 IfStmt (com.github.javaparser.ast.stmt.IfStmt)7 Type (com.github.javaparser.ast.type.Type)7 IOException (java.io.IOException)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.parse (com.github.javaparser.StaticJavaParser.parse)6 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)6 ArrayCreationLevel (com.github.javaparser.ast.ArrayCreationLevel)6