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);
});
}
}
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);
}
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());
}
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());
}
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());
}
Aggregations