/** * Generates something like <code>field = newValue</code> * * @param fieldName * @param valueName * @return */ private static BlockStmt fieldAssignment(String fieldName, String valueName) { BlockStmt body = new BlockStmt(); body.addStatement( new ExpressionStmt(new AssignExpr(new NameExpr(fieldName), new NameExpr(valueName), Operator.ASSIGN))); return body; }
private static MethodCallExpr methodCallExpr(String owner, String method, Expression... args) { MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr(owner), method); for (Expression expr : args) { methodCallExpr.addArgument(expr); } return methodCallExpr; }
/** * Generates something like <code>field = newValue</code> * * @param fieldName * @param valueName * @return */ private static BlockStmt fieldAssignment(String fieldName, String valueName) { BlockStmt body = new BlockStmt(); body.addStatement( new ExpressionStmt(new AssignExpr(new NameExpr(fieldName), new NameExpr(valueName), Operator.ASSIGN))); return body; }
private static MethodCallExpr methodCallExpr(String owner, String method, Expression... args) { MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr(owner), method); for (Expression expr : args) { methodCallExpr.addArgument(expr); } return methodCallExpr; }
/** * 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; }
/** * 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; }
/** * 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); }
String initString = "ArrayList<" + argType + ">()"; FieldDeclaration fd = new FieldDeclaration( ModifierSet.PUBLIC, new ClassOrInterfaceType(className), new VariableDeclarator( new VariableDeclaratorId(name, new NameExpr(initString))));
private boolean updateImports() { List<ImportDeclaration> imports = compilationUnit.getImports(); if (!hasImport(imports, TAB_LAYOUT_PACKAGE_CLASS)) { imports.add(new ImportDeclaration(new NameExpr(TAB_LAYOUT_PACKAGE_CLASS), false, false)); return true; } return false; }
private boolean updateImports() { List<ImportDeclaration> imports = compilationUnit.getImports(); if (!hasImport(imports, TAB_LAYOUT_PACKAGE_CLASS)) { imports.add(new ImportDeclaration(new NameExpr(TAB_LAYOUT_PACKAGE_CLASS), false, false)); return true; } return false; }
@Override public NameExpr doMerge(NameExpr first, NameExpr second) { NameExpr ne = new NameExpr(); ne.setName(first.getName()); return ne; }
@Test(expected = IllegalStateException.class) public void resolveLocaleOutsideAST() { getExpressionType(typeResolver, new FieldAccessExpr(new NameExpr("Locale"), "US")); } }
/** * creates the compilation unit */ private static CompilationUnit createCU() { CompilationUnit cu = new CompilationUnit(); // set the package cu.setPakage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test"))); // create the type declaration ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass"); ASTHelper.addTypeDeclaration(cu, type); // create a method MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main"); method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC)); ASTHelper.addMember(type, method); // add a parameter to the method Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args"); param.setVarArgs(true); ASTHelper.addParameter(method, param); // add a body to the method BlockStmt block = new BlockStmt(); method.setBody(block); // add a statement do the method body NameExpr clazz = new NameExpr("System"); FieldAccessExpr field = new FieldAccessExpr(clazz, "out"); MethodCallExpr call = new MethodCallExpr(field, "println"); ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!")); ASTHelper.addStmt(block, call); return cu; }
getint.addArgument(new NameExpr("_FH_" + featureName)); replaceInParent(n, getint);
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); } }