Example usage for org.eclipse.jdt.core.dom.rewrite ImportRewrite addImportFromSignature

List of usage examples for org.eclipse.jdt.core.dom.rewrite ImportRewrite addImportFromSignature

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom.rewrite ImportRewrite addImportFromSignature.

Prototype

public Type addImportFromSignature(String typeSig, AST ast) 

Source Link

Document

Adds a new import to the rewriter's record and returns a Type node that can be used in the code as a reference to the type.

Usage

From source file:com.google.gwt.eclipse.core.util.Util.java

License:Open Source License

/**
 * Creates a GWT RPC async callback parameter declaration based on the sync
 * method return type./*from  w w w.  j a va 2s .co m*/
 *
 * @param ast {@link AST} associated with the destination compilation unit
 * @param syncReturnType the sync method return type
 * @param callbackParameterName name of the callback parameter
 * @param imports {@link ImportsRewrite} for the destination compilation unit
 * @return callback paramter declaration
 */
@SuppressWarnings("unchecked")
public static SingleVariableDeclaration createAsyncCallbackParameter(AST ast, Type syncReturnType,
        String callbackParameterName, ImportRewrite imports) {
    ITypeBinding syncReturnTypeBinding = syncReturnType.resolveBinding();

    SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration();

    String gwtCallbackTypeSig = Signature
            .createTypeSignature(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME, true);
    Type gwtCallbackType = imports.addImportFromSignature(gwtCallbackTypeSig, ast);

    if (syncReturnTypeBinding.isPrimitive()) {
        String wrapperName = JavaASTUtils.getWrapperTypeName(syncReturnTypeBinding.getName());
        String wrapperTypeSig = Signature.createTypeSignature(wrapperName, true);
        syncReturnType = imports.addImportFromSignature(wrapperTypeSig, ast);
    } else {
        syncReturnType = JavaASTUtils.normalizeTypeAndAddImport(ast, syncReturnType, imports);
    }

    ParameterizedType type = ast.newParameterizedType(gwtCallbackType);
    List<Type> typeArgs = type.typeArguments();
    typeArgs.add(syncReturnType);

    parameter.setType(type);
    parameter.setName(ast.newSimpleName(callbackParameterName));

    return parameter;
}