/** * Generates something like * <code>P_INDEX_UPDATER.lazySet(this, newValue)</code> * * @param fieldUpdaterFieldName * @param newValueName * @return */ private static BlockStmt fieldUpdaterLazySet(String fieldUpdaterFieldName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ExpressionStmt( methodCallExpr(fieldUpdaterFieldName, "lazySet", new ThisExpr(), new NameExpr(newValueName)))); return body; }
/** * Generates something like * <code>return P_INDEX_UPDATER.getAndSet(this, newValue)</code> * * @param fieldUpdaterFieldName * @param newValueName * @return */ private static BlockStmt fieldUpdaterGetAndSet(String fieldUpdaterFieldName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ReturnStmt( methodCallExpr(fieldUpdaterFieldName, "getAndSet", new ThisExpr(), new NameExpr(newValueName)))); return body; }
/** * Generates something like * <code>P_INDEX_UPDATER.lazySet(this, newValue)</code> * * @param fieldUpdaterFieldName * @param newValueName * @return */ private static BlockStmt fieldUpdaterLazySet(String fieldUpdaterFieldName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ExpressionStmt( methodCallExpr(fieldUpdaterFieldName, "lazySet", new ThisExpr(), new NameExpr(newValueName)))); return body; }
/** * Generates something like * <code>return P_INDEX_UPDATER.compareAndSet(this, expectedValue, newValue)</code> * * @param fieldUpdaterFieldName * @param expectedValueName * @param newValueName * @return */ private static BlockStmt fieldUpdaterCompareAndSet(String fieldUpdaterFieldName, String expectedValueName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ReturnStmt(methodCallExpr(fieldUpdaterFieldName, "compareAndSet", new ThisExpr(), new NameExpr(expectedValueName), new NameExpr(newValueName)))); return body; }
/** * Generates something like * <code>return P_INDEX_UPDATER.compareAndSet(this, expectedValue, newValue)</code> * * @param fieldUpdaterFieldName * @param expectedValueName * @param newValueName * @return */ private static BlockStmt fieldUpdaterCompareAndSet(String fieldUpdaterFieldName, String expectedValueName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ReturnStmt(methodCallExpr(fieldUpdaterFieldName, "compareAndSet", new ThisExpr(), new NameExpr(expectedValueName), new NameExpr(newValueName)))); return body; }
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; }
@Override public ThisExpr doMerge(ThisExpr first, ThisExpr second) { ThisExpr te = new ThisExpr(); te.setClassExpr(mergeSingle(first.getClassExpr(),second.getClassExpr())); return te; }
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; }