Convert Eclipse JDT ITypeBinding to Type

I am looking for a general way to convert an instance org.eclipse.jdt.core.dom.ITypeBindingto an instance org.eclipse.jdt.core.dom.Type. Although I believe there must be some kind of API call for this, I cannot find it.

There are various ways to do this manually, depending on the specific type.

Is there a general way to take ITypeBindingand get a Typewithout all these special cases? Accepting value Stringand returning a is Typealso acceptable.

Update

From the answer so far, it seems I have to handle all these special cases. Here is the first attempt to do this. I am sure that this is not entirely correct, so the rating is evaluated:

public static Type typeFromBinding(AST ast, ITypeBinding typeBinding) {
    if( ast == null ) 
        throw new NullPointerException("ast is null");
    if( typeBinding == null )
        throw new NullPointerException("typeBinding is null");

    if( typeBinding.isPrimitive() ) {
        return ast.newPrimitiveType(
            PrimitiveType.toCode(typeBinding.getName()));
    }

    if( typeBinding.isCapture() ) {
        ITypeBinding wildCard = typeBinding.getWildcard();
        WildcardType capType = ast.newWildcardType();
        ITypeBinding bound = wildCard.getBound();
        if( bound != null ) {
            capType.setBound(typeFromBinding(ast, bound)),
                wildCard.isUpperbound());
        }
        return capType;
    }

    if( typeBinding.isArray() ) {
        Type elType = typeFromBinding(ast, typeBinding.getElementType());
        return ast.newArrayType(elType, typeBinding.getDimensions());
    }

    if( typeBinding.isParameterizedType() ) {
        ParameterizedType type = ast.newParameterizedType(
            typeFromBinding(ast, typeBinding.getErasure()));

        @SuppressWarnings("unchecked")
        List<Type> newTypeArgs = type.typeArguments();
        for( ITypeBinding typeArg : typeBinding.getTypeArguments() ) {
            newTypeArgs.add(typeFromBinding(ast, typeArg));
        }

        return type;
    }

    // simple or raw type
    String qualName = typeBinding.getQualifiedName();
    if( "".equals(qualName) ) {
        throw new IllegalArgumentException("No name for type binding.");
    }
    return ast.newSimpleType(ast.newName(qualName));
}
+5
source share
2 answers

, . org.eclipse.jdt.core.dom.rewrite.ImportRewrite, . Type addImport (ITypeBinding, AST), node, .

+2

, "" .

0

All Articles