Source:
private void addMethodsToNameTokens(ICompilationUnit unit, String nameToken, IProgressMonitor monitor) throws JavaModelException, MalformedTreeException, BadLocationException { String varName = nameToken.startsWith("/") ? nameToken.substring(1): nameToken; Document document = new Document(unit.getSource()); CompilationUnit astRoot = initAstRoot(unit, monitor); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); MethodDeclaration method = findMethod(astRoot, getNameTokenMethod(varName)); if (method != null) { logger.severe("FYI: the method in nameTokens already exists." + method.toString()); return; } List types = astRoot.types(); ASTNode rootNode = (ASTNode) types.get(0); ListRewrite listRewrite = rewrite.getListRewrite(rootNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); ASTNode fieldNode = rewrite.createStringPlaceholder("public static final String " + varName + " = \"" + nameToken + "\";", ASTNode.EMPTY_STATEMENT); StringBuilder nameTokenMethod = new StringBuilder(); nameTokenMethod.append("public static String ").append(getNameTokenMethod(varName)).append("() {\n") .append("return " + varName + ";\n").append("}\n"); ASTNode methodNode = rewrite.createStringPlaceholder(nameTokenMethod.toString(), ASTNode.EMPTY_STATEMENT); listRewrite.insertFirst(fieldNode, null); listRewrite.insertLast(methodNode, null); TextEdit edits = rewrite.rewriteAST(document, unit.getJavaProject().getOptions(true)); edits.apply(document); String newSource = new CodeFormattingUtil(getJavaProject(), monitor).formatCodeJavaClass(document); IBuffer buffer = unit.getBuffer(); buffer.setContents(newSource); buffer.save(monitor, true); }

Target:
private void addMethodsToNameTokens(ICompilationUnit unit, String nameToken, IProgressMonitor monitor) throws JavaModelException, MalformedTreeException, BadLocationException { String fieldName = getFieldNameFromNameToken(nameToken); Document document = new Document(unit.getSource()); CompilationUnit astRoot = initAstRoot(unit, monitor); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); MethodDeclaration method = findMethod(astRoot, getNameTokenMethod(fieldName)); if (method != null) { logger.severe("FYI: the method in nameTokens already exists." + method.toString()); return; } List types = astRoot.types(); ASTNode rootNode = (ASTNode) types.get(0); ListRewrite listRewrite = rewrite.getListRewrite(rootNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); ASTNode fieldNode = rewrite.createStringPlaceholder("public static final String " + fieldName + " = \"" + nameToken + "\";", ASTNode.EMPTY_STATEMENT); StringBuilder nameTokenMethod = new StringBuilder(); nameTokenMethod.append("public static String ").append(getNameTokenMethod(fieldName)).append("() {\n") .append("return " + fieldName + ";\n").append("}\n"); ASTNode methodNode = rewrite.createStringPlaceholder(nameTokenMethod.toString(), ASTNode.EMPTY_STATEMENT); listRewrite.insertFirst(fieldNode, null); listRewrite.insertLast(methodNode, null); TextEdit edits = rewrite.rewriteAST(document, unit.getJavaProject().getOptions(true)); edits.apply(document); String newSource = new CodeFormattingUtil(getJavaProject(), monitor).formatCodeJavaClass(document); IBuffer buffer = unit.getBuffer(); buffer.setContents(newSource); buffer.save(monitor, true); }