Document document = new Document(source);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(asyncContents);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
AST ast = astRoot.getAST();
astRoot.recordModifications();
// Modify imports (+AsyncCallback, -RemoteService, -*Exception)
List imports = astRoot.imports();
List importsToBeRemoved = new ArrayList();
for (Iterator j = imports.iterator(); j.hasNext();) {
ImportDeclaration anImportDecl = (ImportDeclaration) j.next();
String importName = anImportDecl.getName().getFullyQualifiedName();
if (importName.endsWith("Exception") || //$NON-NLS-1$
importName.equals("com.google.gwt.core.client.GWT") || //$NON-NLS-1$
importName.equals("com.google.gwt.user.client.rpc.ServiceDefTarget") || //$NON-NLS-1$
importName.equals("com.google.gwt.user.client.rpc.RemoteService")//$NON-NLS-1$
)
importsToBeRemoved.add(anImportDecl);
}
imports.removeAll(importsToBeRemoved);
ImportDeclaration importDecl = ast.newImportDeclaration();
importDecl.setName(ast.newName("com.google.gwt.user.client.rpc.AsyncCallback")); //$NON-NLS-1$
astRoot.imports().add(importDecl);
// Add Async to the name
TypeDeclaration aRemoteService = (TypeDeclaration) astRoot.types().get(0);
String remoteServiceAsyncName = aRemoteService.getName().getFullyQualifiedName() + "Async"; //$NON-NLS-1$
aRemoteService.setName(astRoot.getAST().newSimpleName(remoteServiceAsyncName));
// Remote all interfaces
aRemoteService.superInterfaceTypes().clear();
// Change methods, fields and inner classes
List bodyDeclarations = aRemoteService.bodyDeclarations();
List declarationsToDelete = new ArrayList();
for (Iterator k = bodyDeclarations.iterator(); k.hasNext();) {
Object currDeclaration = k.next();
if (currDeclaration instanceof MethodDeclaration) {
// Make return type void
MethodDeclaration aMethod = (MethodDeclaration) currDeclaration;
aMethod.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
// Add AsyncCallback parameter
SingleVariableDeclaration asyncCallbackParam = ast.newSingleVariableDeclaration();
asyncCallbackParam.setName(ast.newSimpleName("callback")); //$NON-NLS-1$
asyncCallbackParam.setType(ast.newSimpleType(ast.newName("AsyncCallback"))); //$NON-NLS-1$
aMethod.parameters().add(asyncCallbackParam);
// Remove throws
aMethod.thrownExceptions().clear();