Example usage for org.eclipse.jdt.internal.compiler.lookup TypeBinding isAnonymousType

List of usage examples for org.eclipse.jdt.internal.compiler.lookup TypeBinding isAnonymousType

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup TypeBinding isAnonymousType.

Prototype

public final boolean isAnonymousType() 

Source Link

Usage

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

public static TypeReference makeType(TypeBinding binding, ASTNode pos, boolean allowCompound) {
    int dims = binding.dimensions();
    binding = binding.leafComponentType();

    // Primitives

    char[] base = null;

    switch (binding.id) {
    case TypeIds.T_int:
        base = TypeConstants.INT;/*www .  j a  v  a2  s . co m*/
        break;
    case TypeIds.T_long:
        base = TypeConstants.LONG;
        break;
    case TypeIds.T_short:
        base = TypeConstants.SHORT;
        break;
    case TypeIds.T_byte:
        base = TypeConstants.BYTE;
        break;
    case TypeIds.T_double:
        base = TypeConstants.DOUBLE;
        break;
    case TypeIds.T_float:
        base = TypeConstants.FLOAT;
        break;
    case TypeIds.T_boolean:
        base = TypeConstants.BOOLEAN;
        break;
    case TypeIds.T_char:
        base = TypeConstants.CHAR;
        break;
    case TypeIds.T_void:
        base = TypeConstants.VOID;
        break;
    case TypeIds.T_null:
        return null;
    }

    if (base != null) {
        if (dims > 0) {
            TypeReference result = new ArrayTypeReference(base, dims, pos(pos));
            setGeneratedBy(result, pos);
            return result;
        }
        TypeReference result = new SingleTypeReference(base, pos(pos));
        setGeneratedBy(result, pos);
        return result;
    }

    if (binding.isAnonymousType()) {
        ReferenceBinding ref = (ReferenceBinding) binding;
        ReferenceBinding[] supers = ref.superInterfaces();
        if (supers == null || supers.length == 0)
            supers = new ReferenceBinding[] { ref.superclass() };
        if (supers[0] == null) {
            TypeReference result = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(pos, 3));
            setGeneratedBy(result, pos);
            return result;
        }
        return makeType(supers[0], pos, false);
    }

    if (binding instanceof CaptureBinding) {
        return makeType(((CaptureBinding) binding).wildcard, pos, allowCompound);
    }

    if (binding.isUnboundWildcard()) {
        if (!allowCompound) {
            TypeReference result = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(pos, 3));
            setGeneratedBy(result, pos);
            return result;
        } else {
            Wildcard out = new Wildcard(Wildcard.UNBOUND);
            setGeneratedBy(out, pos);
            out.sourceStart = pos.sourceStart;
            out.sourceEnd = pos.sourceEnd;
            return out;
        }
    }

    if (binding.isWildcard()) {
        WildcardBinding wildcard = (WildcardBinding) binding;
        if (wildcard.boundKind == Wildcard.EXTENDS) {
            if (!allowCompound) {
                return makeType(wildcard.bound, pos, false);
            } else {
                Wildcard out = new Wildcard(Wildcard.EXTENDS);
                setGeneratedBy(out, pos);
                out.bound = makeType(wildcard.bound, pos, false);
                out.sourceStart = pos.sourceStart;
                out.sourceEnd = pos.sourceEnd;
                return out;
            }
        } else if (allowCompound && wildcard.boundKind == Wildcard.SUPER) {
            Wildcard out = new Wildcard(Wildcard.SUPER);
            setGeneratedBy(out, pos);
            out.bound = makeType(wildcard.bound, pos, false);
            out.sourceStart = pos.sourceStart;
            out.sourceEnd = pos.sourceEnd;
            return out;
        } else {
            TypeReference result = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(pos, 3));
            setGeneratedBy(result, pos);
            return result;
        }
    }

    // Keep moving up via 'binding.enclosingType()' and gather generics from each binding. We stop after a local type, or a static type, or a top-level type.
    // Finally, add however many nullTypeArgument[] arrays as that are missing, inverse the list, toArray it, and use that as PTR's typeArgument argument.

    List<TypeReference[]> params = new ArrayList<TypeReference[]>();
    /* Calculate generics */ {
        TypeBinding b = binding;
        while (true) {
            boolean isFinalStop = b.isLocalType() || !b.isMemberType() || b.enclosingType() == null;

            TypeReference[] tyParams = null;
            if (b instanceof ParameterizedTypeBinding) {
                ParameterizedTypeBinding paramized = (ParameterizedTypeBinding) b;
                if (paramized.arguments != null) {
                    tyParams = new TypeReference[paramized.arguments.length];
                    for (int i = 0; i < tyParams.length; i++) {
                        tyParams[i] = makeType(paramized.arguments[i], pos, true);
                    }
                }
            }

            params.add(tyParams);
            if (isFinalStop)
                break;
            b = b.enclosingType();
        }
    }

    char[][] parts;

    if (binding.isTypeVariable()) {
        parts = new char[][] { binding.shortReadableName() };
    } else if (binding.isLocalType()) {
        parts = new char[][] { binding.sourceName() };
    } else {
        String[] pkg = new String(binding.qualifiedPackageName()).split("\\.");
        String[] name = new String(binding.qualifiedSourceName()).split("\\.");
        if (pkg.length == 1 && pkg[0].isEmpty())
            pkg = new String[0];
        parts = new char[pkg.length + name.length][];
        int ptr;
        for (ptr = 0; ptr < pkg.length; ptr++)
            parts[ptr] = pkg[ptr].toCharArray();
        for (; ptr < pkg.length + name.length; ptr++)
            parts[ptr] = name[ptr - pkg.length].toCharArray();
    }

    while (params.size() < parts.length)
        params.add(null);
    Collections.reverse(params);

    boolean isParamized = false;

    for (TypeReference[] tyParams : params) {
        if (tyParams != null) {
            isParamized = true;
            break;
        }
    }
    if (isParamized) {
        if (parts.length > 1) {
            TypeReference[][] typeArguments = params.toArray(new TypeReference[0][]);
            TypeReference result = new ParameterizedQualifiedTypeReference(parts, typeArguments, dims,
                    poss(pos, parts.length));
            setGeneratedBy(result, pos);
            return result;
        }
        TypeReference result = new ParameterizedSingleTypeReference(parts[0], params.get(0), dims, pos(pos));
        setGeneratedBy(result, pos);
        return result;
    }

    if (dims > 0) {
        if (parts.length > 1) {
            TypeReference result = new ArrayQualifiedTypeReference(parts, dims, poss(pos, parts.length));
            setGeneratedBy(result, pos);
            return result;
        }
        TypeReference result = new ArrayTypeReference(parts[0], dims, pos(pos));
        setGeneratedBy(result, pos);
        return result;
    }

    if (parts.length > 1) {
        TypeReference result = new QualifiedTypeReference(parts, poss(pos, parts.length));
        setGeneratedBy(result, pos);
        return result;
    }
    TypeReference result = new SingleTypeReference(parts[0], pos(pos));
    setGeneratedBy(result, pos);
    return result;
}

From source file:spoon.support.compiler.jdt.ReferenceBuilder.java

License:Open Source License

/**
 * @param resolveGeneric if true then it never returns CtTypeParameterReference, but it's superClass instead
 *///from w  w  w . j a  v  a2 s. co m
<T> CtTypeReference<T> getTypeReference(TypeBinding binding, boolean resolveGeneric) {
    if (binding == null) {
        return null;
    }

    CtTypeReference<?> ref;

    if (binding instanceof RawTypeBinding) {
        ref = getTypeReference(((ParameterizedTypeBinding) binding).genericType());
    } else if (binding instanceof ParameterizedTypeBinding) {
        if (binding.actualType() != null && binding.actualType() instanceof LocalTypeBinding) {
            // When we define a nested class in a method and when the enclosing class of this method
            // is a parameterized type binding, JDT give a ParameterizedTypeBinding for the nested class
            // and hide the real class in actualType().
            ref = getTypeReference(binding.actualType());
        } else {
            ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
            this.exploringParameterizedBindings.put(binding, ref);
            if (binding.isAnonymousType()) {
                ref.setSimpleName("");
            } else {
                ref.setSimpleName(String.valueOf(binding.sourceName()));
                if (binding.enclosingType() != null) {
                    ref.setDeclaringType(getTypeReference(binding.enclosingType()));
                } else {
                    ref.setPackage(getPackageReference(binding.getPackage()));
                }
            }
        }
        if (binding.actualType() instanceof MissingTypeBinding) {
            ref = getTypeReference(binding.actualType());
        }

        if (((ParameterizedTypeBinding) binding).arguments != null) {
            for (TypeBinding b : ((ParameterizedTypeBinding) binding).arguments) {
                if (bindingCache.containsKey(b)) {
                    ref.addActualTypeArgument(getCtCircularTypeReference(b));
                } else {
                    if (!this.exploringParameterizedBindings.containsKey(b)) {
                        this.exploringParameterizedBindings.put(b, null);
                        CtTypeReference typeRefB = getTypeReference(b);
                        this.exploringParameterizedBindings.put(b, typeRefB);
                        ref.addActualTypeArgument(typeRefB);
                    } else {
                        CtTypeReference typeRefB = this.exploringParameterizedBindings.get(b);
                        if (typeRefB != null) {
                            ref.addActualTypeArgument(typeRefB.clone());
                        }
                    }
                }
            }
        }
    } else if (binding instanceof MissingTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.sourceName()));
        ref.setPackage(getPackageReference(binding.getPackage()));
        if (!this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports) {
            final CtReference declaring = this.getDeclaringReferenceFromImports(binding.sourceName());
            if (declaring instanceof CtPackageReference) {
                ref.setPackage((CtPackageReference) declaring);
            } else if (declaring instanceof CtTypeReference) {
                ref.setDeclaringType((CtTypeReference) declaring);
            }
        }
    } else if (binding instanceof BinaryTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.enclosingType() != null) {
            ref.setDeclaringType(getTypeReference(binding.enclosingType()));
        } else {
            ref.setPackage(getPackageReference(binding.getPackage()));
        }
        ref.setSimpleName(new String(binding.sourceName()));
    } else if (binding instanceof TypeVariableBinding) {
        boolean oldBounds = bounds;

        if (binding instanceof CaptureBinding) {
            ref = this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
            bounds = true;
        } else {
            TypeVariableBinding typeParamBinding = (TypeVariableBinding) binding;
            if (resolveGeneric) {
                //it is called e.g. by ExecutableReference, which must not use CtParameterTypeReference
                //but it needs it's bounding type instead
                ReferenceBinding superClass = typeParamBinding.superclass;
                ReferenceBinding[] superInterfaces = typeParamBinding.superInterfaces();

                CtTypeReference refSuperClass = null;

                // if the type parameter has a super class other than java.lang.Object, we get it
                // superClass.superclass() is null if it's java.lang.Object
                if (superClass != null && !(superClass.superclass() == null)) {

                    // this case could happen with Enum<E extends Enum<E>> for example:
                    // in that case we only want to have E -> Enum -> E
                    // to conserve the same behavior as JavaReflectionTreeBuilder
                    if (!(superClass instanceof ParameterizedTypeBinding)
                            || !this.exploringParameterizedBindings.containsKey(superClass)) {
                        refSuperClass = this.getTypeReference(superClass, resolveGeneric);
                    }

                    // if the type parameter has a super interface, then we'll get it too, as a superclass
                    // type parameter can only extends an interface or a class, so we don't make the distinction
                    // in Spoon. Moreover we can only have one extends in a type parameter.
                } else if (superInterfaces != null && superInterfaces.length == 1) {
                    refSuperClass = this.getTypeReference(superInterfaces[0], resolveGeneric);
                }
                if (refSuperClass == null) {
                    refSuperClass = this.jdtTreeBuilder.getFactory().Type().getDefaultBoundingType();
                }
                ref = refSuperClass.clone();
            } else {
                ref = this.jdtTreeBuilder.getFactory().Core().createTypeParameterReference();
                ref.setSimpleName(new String(binding.sourceName()));
            }
        }
        TypeVariableBinding b = (TypeVariableBinding) binding;
        if (bounds) {
            if (b instanceof CaptureBinding && ((CaptureBinding) b).wildcard != null) {
                bounds = oldBounds;
                return getTypeReference(((CaptureBinding) b).wildcard, resolveGeneric);
            } else if (b.superclass != null && b.firstBound == b.superclass) {
                bounds = false;
                bindingCache.put(binding, ref);
                if (ref instanceof CtWildcardReference) {
                    ((CtWildcardReference) ref).setBoundingType(getTypeReference(b.superclass, resolveGeneric));
                }
                bounds = oldBounds;
            }
        }
        if (bounds && b.superInterfaces != null && b.superInterfaces != Binding.NO_SUPERINTERFACES) {
            bindingCache.put(binding, ref);
            List<CtTypeReference<?>> bounds = new ArrayList<>();
            CtTypeParameterReference typeParameterReference = (CtTypeParameterReference) ref;
            if (!(typeParameterReference.isDefaultBoundingType())) { // if it's object we can ignore it
                bounds.add(typeParameterReference.getBoundingType());
            }
            for (ReferenceBinding superInterface : b.superInterfaces) {
                bounds.add(getTypeReference(superInterface, resolveGeneric));
            }
            if (ref instanceof CtWildcardReference) {
                ((CtWildcardReference) ref).setBoundingType(this.jdtTreeBuilder.getFactory().Type()
                        .createIntersectionTypeReferenceWithBounds(bounds));
            }
        }
        if (binding instanceof CaptureBinding) {
            bounds = false;
        }
    } else if (binding instanceof BaseTypeBinding) {
        String name = new String(binding.sourceName());
        //always create new TypeReference, because clonning from a cache clones invalid SourcePosition
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(name);
    } else if (binding instanceof WildcardBinding) {
        WildcardBinding wildcardBinding = (WildcardBinding) binding;
        CtWildcardReference wref = this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
        ref = wref;

        if (wildcardBinding.boundKind == Wildcard.SUPER) {
            wref.setUpper(false);
        }

        if (wildcardBinding.bound != null) {
            if (bindingCache.containsKey(wildcardBinding.bound)) {
                wref.setBoundingType(getCtCircularTypeReference(wildcardBinding.bound));
            } else {
                wref.setBoundingType(getTypeReference(((WildcardBinding) binding).bound));
            }
        }
    } else if (binding instanceof LocalTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.isAnonymousType()) {
            ref.setSimpleName(JDTTreeBuilderHelper
                    .computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
            ref.setDeclaringType(getTypeReference(binding.enclosingType()));
        } else {
            ref.setSimpleName(new String(binding.sourceName()));
            if (((LocalTypeBinding) binding).enclosingMethod == null && binding.enclosingType() != null
                    && binding.enclosingType() instanceof LocalTypeBinding) {
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            } else if (binding.enclosingMethod() != null) {
                ref.setSimpleName(JDTTreeBuilderHelper
                        .computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            }
        }
    } else if (binding instanceof SourceTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.isAnonymousType()) {
            ref.setSimpleName(JDTTreeBuilderHelper
                    .computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
            ref.setDeclaringType(getTypeReference(binding.enclosingType()));
        } else {
            ref.setSimpleName(new String(binding.sourceName()));
            if (binding.enclosingType() != null) {
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            } else {
                ref.setPackage(getPackageReference(binding.getPackage()));
            }
        }
    } else if (binding instanceof ArrayBinding) {
        CtArrayTypeReference<Object> arrayref;
        arrayref = this.jdtTreeBuilder.getFactory().Core().createArrayTypeReference();
        ref = arrayref;
        for (int i = 1; i < binding.dimensions(); i++) {
            CtArrayTypeReference<Object> tmp = this.jdtTreeBuilder.getFactory().Core()
                    .createArrayTypeReference();
            arrayref.setComponentType(tmp);
            arrayref = tmp;
        }
        arrayref.setComponentType(getTypeReference(binding.leafComponentType(), resolveGeneric));
    } else if (binding instanceof PolyTypeBinding) {
        // JDT can't resolve the type of this binding and we only have a string.
        // In this case, we return a type Object because we can't know more about it.
        ref = this.jdtTreeBuilder.getFactory().Type().objectType();
    } else if (binding instanceof ProblemReferenceBinding) {
        // Spoon is able to analyze also without the classpath
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        char[] readableName = binding.readableName();
        StringBuilder sb = new StringBuilder();
        for (int i = readableName.length - 1; i >= 0; i--) {
            char c = readableName[i];
            if (c == '.') {
                break;
            }
            sb.append(c);
        }
        sb.reverse();
        ref.setSimpleName(sb.toString());
        final CtReference declaring = this.getDeclaringReferenceFromImports(binding.sourceName());
        setPackageOrDeclaringType(ref, declaring);
    } else if (binding instanceof JDTTreeBuilder.SpoonReferenceBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.sourceName()));
        ref.setDeclaringType(getTypeReference(binding.enclosingType()));
    } else if (binding instanceof IntersectionTypeBinding18) {
        List<CtTypeReference<?>> bounds = new ArrayList<>();
        for (ReferenceBinding superInterface : binding.getIntersectingTypes()) {
            bounds.add(getTypeReference(superInterface));
        }
        ref = this.jdtTreeBuilder.getFactory().Type().createIntersectionTypeReferenceWithBounds(bounds);
    } else {
        throw new RuntimeException("Unknown TypeBinding: " + binding.getClass() + " " + binding);
    }
    bindingCache.remove(binding);
    this.exploringParameterizedBindings.remove(binding);
    return (CtTypeReference<T>) ref;
}

From source file:spoon.test.prettyprinter.testclasses.ComplexClass.java

License:Open Source License

@SuppressWarnings("unchecked")
<T> CtTypeReference<T> getTypeReference(TypeBinding binding) {
    if (binding == null) {
        return null;
    }//from ww  w . j ava  2s. c  om

    CtTypeReference<?> ref = null;

    if (binding instanceof RawTypeBinding) {
        ref = getTypeReference(((ParameterizedTypeBinding) binding).genericType());
    } else if (binding instanceof ParameterizedTypeBinding) {
        if (binding.actualType() != null && binding.actualType() instanceof LocalTypeBinding) {
            // When we define a nested class in a method and when the enclosing class of this method
            // is a parameterized type binding, JDT give a ParameterizedTypeBinding for the nested class
            // and hide the real class in actualType().
            ref = getTypeReference(binding.actualType());
        } else {
            ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
            this.exploringParameterizedBindings.put(binding, ref);
            if (binding.isAnonymousType()) {
                ref.setSimpleName("");
            } else {
                ref.setSimpleName(String.valueOf(binding.sourceName()));
                if (binding.enclosingType() != null) {
                    ref.setDeclaringType(getTypeReference(binding.enclosingType()));
                } else {
                    ref.setPackage(getPackageReference(binding.getPackage()));
                }
            }
        }
        if (binding.actualType() instanceof MissingTypeBinding) {
            ref = getTypeReference(binding.actualType());
        }

        if (((ParameterizedTypeBinding) binding).arguments != null) {
            for (TypeBinding b : ((ParameterizedTypeBinding) binding).arguments) {
                if (bindingCache.containsKey(b)) {
                    ref.addActualTypeArgument(getCtCircularTypeReference(b));
                } else {
                    if (!this.exploringParameterizedBindings.containsKey(b)) {
                        this.exploringParameterizedBindings.put(b, null);
                        CtTypeReference typeRefB = getTypeReference(b);
                        this.exploringParameterizedBindings.put(b, typeRefB);
                        ref.addActualTypeArgument(typeRefB);
                    } else {
                        CtTypeReference typeRefB = this.exploringParameterizedBindings.get(b);
                        if (typeRefB != null) {
                            ref.addActualTypeArgument(typeRefB.clone());
                        }
                    }
                }
            }
        }
    } else if (binding instanceof MissingTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.sourceName()));
        ref.setPackage(getPackageReference(binding.getPackage()));
        if (!this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports) {
            final CtReference declaring = this.getDeclaringReferenceFromImports(binding.sourceName());
            if (declaring instanceof CtPackageReference) {
                ref.setPackage((CtPackageReference) declaring);
            } else if (declaring instanceof CtTypeReference) {
                ref.setDeclaringType((CtTypeReference) declaring);
            }
        }
    } else if (binding instanceof BinaryTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.enclosingType() != null) {
            ref.setDeclaringType(getTypeReference(binding.enclosingType()));
        } else {
            ref.setPackage(getPackageReference(binding.getPackage()));
        }
        ref.setSimpleName(new String(binding.sourceName()));
    } else if (binding instanceof TypeVariableBinding) {
        boolean oldBounds = bounds;

        if (binding instanceof CaptureBinding) {
            ref = this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
            bounds = true;
        } else {
            TypeVariableBinding typeParamBinding = (TypeVariableBinding) binding;
            ReferenceBinding superClass = typeParamBinding.superclass;
            ReferenceBinding[] superInterfaces = typeParamBinding.superInterfaces();

            CtTypeReference refSuperClass = null;

            // if the type parameter has a super class other than java.lang.Object, we get it
            // superClass.superclass() is null if it's java.lang.Object
            if (superClass != null && !(superClass.superclass() == null)) {

                // this case could happen with Enum<E extends Enum<E>> for example:
                // in that case we only want to have E -> Enum -> E
                // to conserve the same behavior as JavaReflectionTreeBuilder
                if (!(superClass instanceof ParameterizedTypeBinding)
                        || !this.exploringParameterizedBindings.containsKey(superClass)) {
                    refSuperClass = this.getTypeReference(superClass);
                }

                // if the type parameter has a super interface, then we'll get it too, as a superclass
                // type parameter can only extends an interface or a class, so we don't make the distinction
                // in Spoon. Moreover we can only have one extends in a type parameter.
            } else if (superInterfaces != null && superInterfaces.length == 1) {
                refSuperClass = this.getTypeReference(superInterfaces[0]);
            }

            ref = this.jdtTreeBuilder.getFactory().Core().createTypeParameterReference();
            ref.setSimpleName(new String(binding.sourceName()));

            if (refSuperClass != null) {
                ((CtTypeParameterReference) ref).addBound(refSuperClass);
            }
        }
        TypeVariableBinding b = (TypeVariableBinding) binding;
        if (bounds) {
            if (b instanceof CaptureBinding && ((CaptureBinding) b).wildcard != null) {
                bounds = oldBounds;
                return getTypeReference(((CaptureBinding) b).wildcard);
            } else if (b.superclass != null && b.firstBound == b.superclass) {
                bounds = false;
                bindingCache.put(binding, ref);
                ((CtTypeParameterReference) ref).setBoundingType(getTypeReference(b.superclass));
                bounds = oldBounds;
            }
        }
        if (bounds && b.superInterfaces != null && b.superInterfaces != Binding.NO_SUPERINTERFACES) {
            bounds = false;
            bindingCache.put(binding, ref);
            List<CtTypeReference<?>> bounds = new ArrayList<>();
            CtTypeParameterReference typeParameterReference = (CtTypeParameterReference) ref;
            if (!(typeParameterReference.isDefaultBoundingType())) { // if it's object we can ignore it
                bounds.add(typeParameterReference.getBoundingType());
            }
            for (ReferenceBinding superInterface : b.superInterfaces) {
                bounds.add(getTypeReference(superInterface));
            }
            ((CtTypeParameterReference) ref).setBoundingType(
                    this.jdtTreeBuilder.getFactory().Type().createIntersectionTypeReferenceWithBounds(bounds));
        }
        if (binding instanceof CaptureBinding) {
            bounds = false;
        }
    } else if (binding instanceof BaseTypeBinding) {
        String name = new String(binding.sourceName());
        //always create new TypeReference, because clonning from a cache clones invalid SourcePosition
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(name);
    } else if (binding instanceof WildcardBinding) {
        WildcardBinding wildcardBinding = (WildcardBinding) binding;
        ref = this.jdtTreeBuilder.getFactory().Core().createWildcardReference();

        if (wildcardBinding.boundKind == Wildcard.SUPER && ref instanceof CtTypeParameterReference) {
            ((CtTypeParameterReference) ref).setUpper(false);
        }

        if (wildcardBinding.bound != null && ref instanceof CtTypeParameterReference) {
            if (bindingCache.containsKey(wildcardBinding.bound)) {
                ((CtTypeParameterReference) ref)
                        .setBoundingType(getCtCircularTypeReference(wildcardBinding.bound));
            } else {

                ((CtTypeParameterReference) ref)
                        .setBoundingType(getTypeReference(((WildcardBinding) binding).bound));
            }
        }
    } else if (binding instanceof LocalTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.isAnonymousType()) {
            ref.setSimpleName(JDTTreeBuilderHelper
                    .computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
            ref.setDeclaringType(getTypeReference((binding.enclosingType())));
        } else {
            ref.setSimpleName(new String(binding.sourceName()));
            if (((LocalTypeBinding) binding).enclosingMethod == null && binding.enclosingType() != null
                    && binding.enclosingType() instanceof LocalTypeBinding) {
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            } else if (binding.enclosingMethod() != null) {
                ref.setSimpleName(JDTTreeBuilderHelper
                        .computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            }
        }
    } else if (binding instanceof SourceTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.isAnonymousType()) {
            ref.setSimpleName(JDTTreeBuilderHelper
                    .computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
            ref.setDeclaringType(getTypeReference((binding.enclosingType())));
        } else {
            ref.setSimpleName(new String(binding.sourceName()));
            if (binding.enclosingType() != null) {
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            } else {
                ref.setPackage(getPackageReference(binding.getPackage()));
            }
            // if(((SourceTypeBinding) binding).typeVariables!=null &&
            // ((SourceTypeBinding) binding).typeVariables.length>0){
            // for (TypeBinding b : ((SourceTypeBinding)
            // binding).typeVariables) {
            // ref.getActualTypeArguments().add(getTypeReference(b));
            // }
            // }
        }
    } else if (binding instanceof ArrayBinding) {
        CtArrayTypeReference<Object> arrayref;
        arrayref = this.jdtTreeBuilder.getFactory().Core().createArrayTypeReference();
        ref = arrayref;
        for (int i = 1; i < binding.dimensions(); i++) {
            CtArrayTypeReference<Object> tmp = this.jdtTreeBuilder.getFactory().Core()
                    .createArrayTypeReference();
            arrayref.setComponentType(tmp);
            arrayref = tmp;
        }
        arrayref.setComponentType(getTypeReference(binding.leafComponentType()));
    } else if (binding instanceof PolyTypeBinding) {
        // JDT can't resolve the type of this binding and we only have a string.
        // In this case, we return a type Object because we can't know more about it.
        ref = this.jdtTreeBuilder.getFactory().Type().objectType();
    } else if (binding instanceof ProblemReferenceBinding) {
        // Spoon is able to analyze also without the classpath
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.readableName()));
        final CtReference declaring = this.getDeclaringReferenceFromImports(binding.sourceName());
        setPackageOrDeclaringType(ref, declaring);
    } else if (binding instanceof JDTTreeBuilder.SpoonReferenceBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.sourceName()));
        ref.setDeclaringType(getTypeReference(binding.enclosingType()));
    } else if (binding instanceof IntersectionTypeBinding18) {
        List<CtTypeReference<?>> bounds = new ArrayList<>();
        for (ReferenceBinding superInterface : binding.getIntersectingTypes()) {
            bounds.add(getTypeReference(superInterface));
        }
        ref = this.jdtTreeBuilder.getFactory().Type().createIntersectionTypeReferenceWithBounds(bounds);
    } else {
        throw new RuntimeException("Unknown TypeBinding: " + binding.getClass() + " " + binding);
    }
    bindingCache.remove(binding);
    this.exploringParameterizedBindings.remove(binding);
    return (CtTypeReference<T>) ref;
}