Search in sources :

Example 61 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class TokenKindGenerator method generate.

@Override
public void generate() {
    Log.info("Running %s", getClass().getSimpleName());
    final CompilationUnit javaTokenCu = sourceRoot.parse("com.github.javaparser", "JavaToken.java");
    final ClassOrInterfaceDeclaration javaToken = javaTokenCu.getClassByName("JavaToken").orElseThrow(() -> new AssertionError("Can't find class in java file."));
    final EnumDeclaration kindEnum = javaToken.findFirst(EnumDeclaration.class, e -> e.getNameAsString().equals("Kind")).orElseThrow(() -> new AssertionError("Can't find class in java file."));
    kindEnum.getEntries().clear();
    annotateGenerated(kindEnum);
    final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class).orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
    valueOfSwitch.findAll(SwitchEntryStmt.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);
    final CompilationUnit constantsCu = generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
    final ClassOrInterfaceDeclaration constants = constantsCu.getInterfaceByName("GeneratedJavaParserConstants").orElseThrow(() -> new AssertionError("Can't find class in java file."));
    for (BodyDeclaration<?> member : constants.getMembers()) {
        member.toFieldDeclaration().filter(field -> {
            String javadoc = field.getJavadocComment().get().getContent();
            return javadoc.contains("RegularExpression Id") || javadoc.contains("End of File");
        }).map(field -> field.getVariable(0)).ifPresent(var -> {
            final String name = var.getNameAsString();
            final IntegerLiteralExpr kind = var.getInitializer().get().asIntegerLiteralExpr();
            generateEnumEntry(kindEnum, name, kind);
            generateValueOfEntry(valueOfSwitch, name, kind);
        });
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) Generator(com.github.javaparser.generator.Generator) SourceRoot(com.github.javaparser.utils.SourceRoot) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Log(com.github.javaparser.utils.Log) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 62 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParserTest method everyTokenHasACategory.

@Test
public void everyTokenHasACategory() throws IOException {
    final int tokenCount = GeneratedJavaParserConstants.tokenImage.length;
    Path tokenTypesPath = mavenModuleRoot(JavaParserTest.class).resolve("../javaparser-core/src/main/java/com/github/javaparser/TokenTypes.java");
    CompilationUnit tokenTypesCu = JavaParser.parse(tokenTypesPath);
    // -1 to take off the default: case.
    int switchEntries = tokenTypesCu.findAll(SwitchEntryStmt.class).size() - 1;
    // The amount of "case XXX:" in TokenTypes.java should be equal to the amount of tokens JavaCC knows about:
    assertEquals(tokenCount, switchEntries);
}
Also used : Path(java.nio.file.Path) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Test(org.junit.Test)

Example 63 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParserTest method rangeOfLambdaBody.

@Test
public void rangeOfLambdaBody() {
    String code = "class A {" + EOL + "  Object f() {" + EOL + "    return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
    CompilationUnit cu = JavaParser.parse(code);
    MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
    CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
    LambdaExpr lambdaExpr = castExpr.getExpression().asLambdaExpr();
    Statement lambdaBody = lambdaExpr.getBody();
    assertEquals(range(3, 68, 3, 101), lambdaBody.getRange().get());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Example 64 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParserTest method rangeOfCastNonIntersection.

@Test
public void rangeOfCastNonIntersection() {
    String code = "class A {" + EOL + "  Object f() {" + EOL + "    return (Comparator<Map.Entry<K, V>>               )(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
    CompilationUnit cu = JavaParser.parse(code);
    MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
    CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
    assertEquals(range(3, 12, 3, 101), castExpr.getRange().get());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Example 65 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParserTest method rangeOfCast.

@Test
public void rangeOfCast() {
    String code = "class A {" + EOL + "  Object f() {" + EOL + "    return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
    CompilationUnit cu = JavaParser.parse(code);
    MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
    CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
    assertEquals(range(3, 12, 3, 101), castExpr.getRange().get());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)489 Test (org.junit.Test)304 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)160 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)140 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)128 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)101 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)70 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)66 Context (com.github.javaparser.symbolsolver.core.resolution.Context)62 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)55 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)51 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)45 File (java.io.File)39 Expression (com.github.javaparser.ast.expr.Expression)38 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)38 MethodUsage (com.github.javaparser.resolution.MethodUsage)34 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)33 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)29 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)29 ArrayList (java.util.ArrayList)29