private MethodDeclaration getterDeclaration(EntityField field) { MethodDeclaration decl = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.createReferenceType(field.getType().getSimpleName(), 0), "get" + CaseConverter.pascalCase(field.getName())); BlockStmt body = new BlockStmt(); body.setStmts( Collections.singletonList( new ReturnStmt( new FieldAccessExpr(new ThisExpr(), field.getName())))); decl.setBody(body); return decl; }
initCallSite.addArgument(new FieldAccessExpr(new NameExpr(className), "class")); initCallSite.addArgument(new StringLiteralExpr(featName)); VariableDeclarator vd_FC = new VariableDeclarator(callSiteType, "_FC_" + featName, initCallSite);
public static void main(String[] args) throws FileNotFoundException { // The directory where we store the examples String pathToExamplesDir = "." + separator + "src" + separator + "main" + separator + "resources"; // Parse the code of an entire source file, a.k.a. a Compilation Unit CompilationUnit compilationUnitNode = JavaParser.parse(new File(pathToExamplesDir + separator + "ASimpleClass.java")); printCompilationUnit("My original class", compilationUnitNode); // Modifying the name of the class compilationUnitNode.getClassByName("ASimpleClass").get() .setName("MyRenamedClass"); printCompilationUnit("Renamed class", compilationUnitNode); // Adding a method: we add a setter MethodDeclaration setter = compilationUnitNode .getClassByName("MyRenamedClass").get() .addMethod("setAField", Modifier.PUBLIC); setter.addParameter("boolean", "aField"); setter.getBody().get().getStatements().add(new ExpressionStmt( new AssignExpr( new FieldAccessExpr(new ThisExpr(),"aField"), new NameExpr("aField"), AssignExpr.Operator.ASSIGN ))); printCompilationUnit("With a setter", compilationUnitNode); } }
private MethodDeclaration setterDeclaration(EntityField field) { MethodDeclaration decl = new MethodDeclaration(ModifierSet.PUBLIC, new VoidType(), "set" + CaseConverter.pascalCase(field.getName()), Collections.singletonList(new Parameter( ASTHelper.createReferenceType(field.getType().getSimpleName(), 0), new VariableDeclaratorId(field.getName())))); BlockStmt body = new BlockStmt(); body.setStmts( Collections.singletonList( new ExpressionStmt( new AssignExpr( new FieldAccessExpr(new ThisExpr(), field.getName()), ASTHelper.createNameExpr(field.getName()), AssignExpr.Operator.assign )))); decl.setBody(body); return decl; }
@Test(expected = IllegalStateException.class) public void resolveLocaleOutsideAST() { getExpressionType(typeResolver, new FieldAccessExpr(new NameExpr("Locale"), "US")); } }
@Override public FieldAccessExpr doMerge(FieldAccessExpr first, FieldAccessExpr second) { FieldAccessExpr fae = new FieldAccessExpr(); fae.setFieldExpr(mergeSingle(first.getFieldExpr(),second.getFieldExpr())); fae.setScope(mergeSingle(first.getScope(),second.getScope())); fae.setTypeArgs(mergeCollections(first.getTypeArgs(),second.getTypeArgs())); return fae; }