method.addParameter(simpleParametricType("LinkedQueueAtomicNode", "E"), "newValue"); method.setBody(fieldUpdaterGetAndSet(fieldUpdaterFieldName, "newValue"));
/** * Given a method declaration node this method will replace it's code and * signature with code to redirect all calls to it to the * <code>newMethodName</code>. Method signatures of both methods must match * exactly. * * @param methodToPatch * @param toMethodName * @param returnType * @param parameters */ private static void patchMethodAsDepreciatedRedirector(MethodDeclaration methodToPatch, String toMethodName, Type returnType, Parameter... parameters) { methodToPatch.setType(returnType); for (Parameter parameter : parameters) { methodToPatch.addParameter(parameter); } methodToPatch.addAnnotation(new MarkerAnnotationExpr("Deprecated")); methodToPatch.setJavadocComment( formatMultilineJavadoc(1, "@deprecated This was renamed to " + toMethodName + " please migrate")); MethodCallExpr methodCall = methodCallExpr("this", toMethodName); for (Parameter parameter : parameters) { methodCall.addArgument(new NameExpr(parameter.getName())); } BlockStmt body = new BlockStmt(); body.addStatement(new ReturnStmt(methodCall)); methodToPatch.setBody(body); }
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); } }
addElement.addParameter("String", "newElement");