use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class GetMetaModelGenerator method generateNode.
@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
final MethodDeclaration getMetaModelMethod = (MethodDeclaration) parseBodyDeclaration(f("%s public %s getMetaModel() { return JavaParserMetaModel.%s; }", nodeMetaModel.isRootNode() ? "" : "@Override", nodeMetaModel.getClass().getSimpleName(), nodeMetaModel.getMetaModelFieldName()));
addOrReplaceWhenSameSignature(nodeCoid, getMetaModelMethod);
nodeCu.addImport(nodeMetaModel.getClass().getName());
nodeCu.addImport(JavaParserMetaModel.class);
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class PropertyGenerator method generateGetter.
private void generateGetter(BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
final MethodDeclaration getter = new MethodDeclaration(EnumSet.of(PUBLIC), parseType(property.getTypeNameForGetter()), property.getGetterMethodName());
final BlockStmt body = getter.getBody().get();
body.getStatements().clear();
if (property.isOptional()) {
body.addStatement(f("return Optional.ofNullable(%s);", property.getName()));
} else {
body.addStatement(f("return %s;", property.getName()));
}
replaceWhenSameSignature(nodeCoid, getter);
}
use of com.github.javaparser.ast.body.MethodDeclaration 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.body.MethodDeclaration 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.body.MethodDeclaration 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