Example usage for org.eclipse.jdt.core.dom WildcardType setBound

List of usage examples for org.eclipse.jdt.core.dom WildcardType setBound

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom WildcardType setBound.

Prototype

public void setBound(Type type, boolean isUpperBound) 

Source Link

Document

Sets the bound of this wildcard type to the given type and marks it as an upper or a lower bound.

Usage

From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java

License:Open Source License

/**
 * Create a new Type for the given type binding.  If the binding
 * represents a parameterized type reference, the returned Type
 * is a ParameterizedType with the same type parameters.  Otherwise
 * the returned Type is a SimpleType.//from w w  w.j a  v  a2 s  .  co  m
 */
@SuppressWarnings("unchecked")
private Type createType(AST ast, ITypeBinding scope, ITypeBinding type) {
    Type newType;
    if (type.isArray()) {
        newType = ast.newArrayType(createType(ast, scope, type.getElementType()), type.getDimensions());
    } else if (type.isPrimitive()) {
        newType = ast.newPrimitiveType(PrimitiveType.toCode(type.getName()));
    } else if (type.isWildcardType()) {
        WildcardType wildType = ast.newWildcardType();
        ITypeBinding bound = type.getBound();
        if (bound != null) {
            wildType.setBound(createType(ast, scope, bound), type.isUpperbound());
        }
        newType = wildType;
    } else if (!type.isParameterizedType()) {
        String name = inScope(type, scope) ? type.getName() : type.getQualifiedName();
        newType = ast.newSimpleType(ast.newName(name));
    } else {
        ITypeBinding erasure = type.getErasure();
        String name = inScope(type, scope) ? erasure.getName() : erasure.getQualifiedName();
        Type rawType = ast.newSimpleType(ast.newName(name));
        ParameterizedType paramType = ast.newParameterizedType(rawType);
        ITypeBinding[] typeArgs = type.getTypeArguments();
        for (ITypeBinding param : typeArgs) {
            paramType.typeArguments().add(createType(ast, scope, param));
        }
        newType = paramType;
    }
    generatedTypes.put(newType, type);
    return newType;
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public Type convertType(TypeReference typeReference) {
    if (typeReference instanceof Wildcard) {
        final Wildcard wildcard = (Wildcard) typeReference;
        final WildcardType wildcardType = new WildcardType(this.ast);
        if (wildcard.bound != null) {
            final Type bound = convertType(wildcard.bound);
            wildcardType.setBound(bound, wildcard.kind == Wildcard.EXTENDS);
            int start = wildcard.sourceStart;
            wildcardType.setSourceRange(start, bound.getStartPosition() + bound.getLength() - start);
        } else {/*from  w ww . ja  v  a  2  s  . c  o m*/
            final int start = wildcard.sourceStart;
            final int end = wildcard.sourceEnd;
            wildcardType.setSourceRange(start, end - start + 1);
        }
        if (this.resolveBindings) {
            recordNodes(wildcardType, typeReference);
        }
        return wildcardType;
    }
    Type type = null;
    int sourceStart = -1;
    int length = 0;
    int dimensions = typeReference.dimensions();
    if (typeReference instanceof org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) {
        // this is either an ArrayTypeReference or a SingleTypeReference
        char[] name = ((org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) typeReference)
                .getTypeName()[0];
        sourceStart = typeReference.sourceStart;
        length = typeReference.sourceEnd - typeReference.sourceStart + 1;
        // need to find out if this is an array type of primitive types or not
        if (isPrimitiveType(name)) {
            int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length);
            if (end == -1) {
                end = sourceStart + length - 1;
            }
            final PrimitiveType primitiveType = new PrimitiveType(this.ast);
            primitiveType.setPrimitiveTypeCode(getPrimitiveTypeCode(name));
            primitiveType.setSourceRange(sourceStart, end - sourceStart + 1);
            type = primitiveType;
        } else if (typeReference instanceof ParameterizedSingleTypeReference) {
            ParameterizedSingleTypeReference parameterizedSingleTypeReference = (ParameterizedSingleTypeReference) typeReference;
            final SimpleName simpleName = new SimpleName(this.ast);
            simpleName.internalSetIdentifier(new String(name));
            int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length);
            if (end == -1) {
                end = sourceStart + length - 1;
            }
            simpleName.setSourceRange(sourceStart, end - sourceStart + 1);
            switch (this.ast.apiLevel) {
            case AST.JLS2_INTERNAL:
                SimpleType simpleType = new SimpleType(this.ast);
                simpleType.setName(simpleName);
                simpleType.setFlags(simpleType.getFlags() | ASTNode.MALFORMED);
                simpleType.setSourceRange(sourceStart, end - sourceStart + 1);
                type = simpleType;
                if (this.resolveBindings) {
                    this.recordNodes(simpleName, typeReference);
                }
                break;
            default:
                simpleType = new SimpleType(this.ast);
                simpleType.setName(simpleName);
                simpleType.setSourceRange(simpleName.getStartPosition(), simpleName.getLength());
                final ParameterizedType parameterizedType = new ParameterizedType(this.ast);
                parameterizedType.setType(simpleType);
                type = parameterizedType;
                TypeReference[] typeArguments = parameterizedSingleTypeReference.typeArguments;
                if (typeArguments != null) {
                    Type type2 = null;
                    for (int i = 0, max = typeArguments.length; i < max; i++) {
                        type2 = convertType(typeArguments[i]);
                        ((ParameterizedType) type).typeArguments().add(type2);
                        end = type2.getStartPosition() + type2.getLength() - 1;
                    }
                    end = retrieveClosingAngleBracketPosition(end + 1);
                    type.setSourceRange(sourceStart, end - sourceStart + 1);
                } else {
                    type.setSourceRange(sourceStart, end - sourceStart + 1);
                }
                if (this.resolveBindings) {
                    this.recordNodes(simpleName, typeReference);
                    this.recordNodes(simpleType, typeReference);
                }
            }
        } else {
            final SimpleName simpleName = new SimpleName(this.ast);
            simpleName.internalSetIdentifier(new String(name));
            // we need to search for the starting position of the first brace in order to set the proper length
            // PR http://dev.eclipse.org/bugs/show_bug.cgi?id=10759
            int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length);
            if (end == -1) {
                end = sourceStart + length - 1;
            }
            simpleName.setSourceRange(sourceStart, end - sourceStart + 1);
            final SimpleType simpleType = new SimpleType(this.ast);
            simpleType.setName(simpleName);
            type = simpleType;
            type.setSourceRange(sourceStart, end - sourceStart + 1);
            type = simpleType;
            if (this.resolveBindings) {
                this.recordNodes(simpleName, typeReference);
            }
        }
        if (dimensions != 0) {
            type = this.ast.newArrayType(type, dimensions);
            type.setSourceRange(sourceStart, length);
            ArrayType subarrayType = (ArrayType) type;
            int index = dimensions - 1;
            while (index > 0) {
                subarrayType = (ArrayType) subarrayType.getComponentType();
                int end = retrieveProperRightBracketPosition(index, sourceStart);
                subarrayType.setSourceRange(sourceStart, end - sourceStart + 1);
                index--;
            }
            if (this.resolveBindings) {
                // store keys for inner types
                completeRecord((ArrayType) type, typeReference);
            }
        }
    } else {
        if (typeReference instanceof ParameterizedQualifiedTypeReference) {
            ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference = (ParameterizedQualifiedTypeReference) typeReference;
            char[][] tokens = parameterizedQualifiedTypeReference.tokens;
            TypeReference[][] typeArguments = parameterizedQualifiedTypeReference.typeArguments;
            long[] positions = parameterizedQualifiedTypeReference.sourcePositions;
            sourceStart = (int) (positions[0] >>> 32);
            switch (this.ast.apiLevel) {
            case AST.JLS2_INTERNAL: {
                char[][] name = ((org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) typeReference)
                        .getTypeName();
                int nameLength = name.length;
                sourceStart = (int) (positions[0] >>> 32);
                length = (int) (positions[nameLength - 1] & 0xFFFFFFFF) - sourceStart + 1;
                Name qualifiedName = this.setQualifiedNameNameAndSourceRanges(name, positions, typeReference);
                final SimpleType simpleType = new SimpleType(this.ast);
                simpleType.setName(qualifiedName);
                simpleType.setSourceRange(sourceStart, length);
                type = simpleType;
            }
                break;
            default:
                if (typeArguments != null) {
                    int numberOfEnclosingType = 0;
                    int startingIndex = 0;
                    int endingIndex = 0;
                    for (int i = 0, max = typeArguments.length; i < max; i++) {
                        if (typeArguments[i] != null) {
                            numberOfEnclosingType++;
                        } else if (numberOfEnclosingType == 0) {
                            endingIndex++;
                        }
                    }
                    Name name = null;
                    if (endingIndex - startingIndex == 0) {
                        final SimpleName simpleName = new SimpleName(this.ast);
                        simpleName.internalSetIdentifier(new String(tokens[startingIndex]));
                        recordPendingNameScopeResolution(simpleName);
                        int start = (int) (positions[startingIndex] >>> 32);
                        int end = (int) positions[startingIndex];
                        simpleName.setSourceRange(start, end - start + 1);
                        simpleName.index = 1;
                        name = simpleName;
                        if (this.resolveBindings) {
                            recordNodes(simpleName, typeReference);
                        }
                    } else {
                        name = this.setQualifiedNameNameAndSourceRanges(tokens, positions, endingIndex,
                                typeReference);
                    }
                    SimpleType simpleType = new SimpleType(this.ast);
                    simpleType.setName(name);
                    int start = (int) (positions[startingIndex] >>> 32);
                    int end = (int) positions[endingIndex];
                    simpleType.setSourceRange(start, end - start + 1);
                    ParameterizedType parameterizedType = new ParameterizedType(this.ast);
                    parameterizedType.setType(simpleType);
                    if (this.resolveBindings) {
                        recordNodes(simpleType, typeReference);
                        recordNodes(parameterizedType, typeReference);
                    }
                    start = simpleType.getStartPosition();
                    end = start + simpleType.getLength() - 1;
                    for (int i = 0, max = typeArguments[endingIndex].length; i < max; i++) {
                        final Type type2 = convertType(typeArguments[endingIndex][i]);
                        parameterizedType.typeArguments().add(type2);
                        end = type2.getStartPosition() + type2.getLength() - 1;
                    }
                    int indexOfEnclosingType = 1;
                    parameterizedType.index = indexOfEnclosingType;
                    end = retrieveClosingAngleBracketPosition(end + 1);
                    length = end + 1;
                    parameterizedType.setSourceRange(start, end - start + 1);
                    startingIndex = endingIndex + 1;
                    Type currentType = parameterizedType;
                    while (startingIndex < typeArguments.length) {
                        SimpleName simpleName = new SimpleName(this.ast);
                        simpleName.internalSetIdentifier(new String(tokens[startingIndex]));
                        simpleName.index = startingIndex + 1;
                        start = (int) (positions[startingIndex] >>> 32);
                        end = (int) positions[startingIndex];
                        simpleName.setSourceRange(start, end - start + 1);
                        recordPendingNameScopeResolution(simpleName);
                        QualifiedType qualifiedType = new QualifiedType(this.ast);
                        qualifiedType.setQualifier(currentType);
                        qualifiedType.setName(simpleName);
                        if (this.resolveBindings) {
                            recordNodes(simpleName, typeReference);
                            recordNodes(qualifiedType, typeReference);
                        }
                        start = currentType.getStartPosition();
                        end = simpleName.getStartPosition() + simpleName.getLength() - 1;
                        qualifiedType.setSourceRange(start, end - start + 1);
                        indexOfEnclosingType++;
                        if (typeArguments[startingIndex] != null) {
                            qualifiedType.index = indexOfEnclosingType;
                            ParameterizedType parameterizedType2 = new ParameterizedType(this.ast);
                            parameterizedType2.setType(qualifiedType);
                            parameterizedType2.index = indexOfEnclosingType;
                            if (this.resolveBindings) {
                                recordNodes(parameterizedType2, typeReference);
                            }
                            for (int i = 0, max = typeArguments[startingIndex].length; i < max; i++) {
                                final Type type2 = convertType(typeArguments[startingIndex][i]);
                                parameterizedType2.typeArguments().add(type2);
                                end = type2.getStartPosition() + type2.getLength() - 1;
                            }
                            end = retrieveClosingAngleBracketPosition(end + 1);
                            length = end + 1;
                            parameterizedType2.setSourceRange(start, end - start + 1);
                            currentType = parameterizedType2;
                        } else {
                            currentType = qualifiedType;
                            qualifiedType.index = indexOfEnclosingType;
                        }
                        startingIndex++;
                    }
                    if (this.resolveBindings) {
                        this.recordNodes(currentType, typeReference);
                    }
                    type = currentType;
                    length -= sourceStart;
                }
            }
        } else if (typeReference instanceof org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) {
            char[][] name = ((org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) typeReference)
                    .getTypeName();
            int nameLength = name.length;
            long[] positions = ((org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) typeReference).sourcePositions;
            sourceStart = (int) (positions[0] >>> 32);
            length = (int) (positions[nameLength - 1] & 0xFFFFFFFF) - sourceStart + 1;
            final Name qualifiedName = this.setQualifiedNameNameAndSourceRanges(name, positions, typeReference);
            final SimpleType simpleType = new SimpleType(this.ast);
            simpleType.setName(qualifiedName);
            type = simpleType;
            type.setSourceRange(sourceStart, length);
        } else {
            TypeReference[] typeReferences = ((org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) typeReference).typeReferences;
            switch (this.ast.apiLevel) {
            case AST.JLS2_INTERNAL:
            case AST.JLS3:
                // recovery
                type = this.convertType(typeReferences[0]);
                int start = typeReference.sourceStart;
                int endPosition = typeReference.sourceEnd;
                length = endPosition - start + 1;
                type.setSourceRange(start, length);
                type.setFlags(type.getFlags() | ASTNode.MALFORMED);
                break;
            default:
                // union type reference
                final UnionType unionType = new UnionType(this.ast);
                for (int i = 0, max = typeReferences.length; i < max; i++) {
                    unionType.types().add(this.convertType(typeReferences[i]));
                }
                type = unionType;
                List types = unionType.types();
                int size = types.size();
                start = ((Type) types.get(0)).getStartPosition();
                Type lastType = (Type) types.get(size - 1);
                endPosition = lastType.getStartPosition() + lastType.getLength();
                length = endPosition - start; /* + 1 - 1 == 0 */
                type.setSourceRange(start, length);
            }
        }

        length = typeReference.sourceEnd - sourceStart + 1;
        if (dimensions != 0) {
            type = this.ast.newArrayType(type, dimensions);
            if (this.resolveBindings) {
                completeRecord((ArrayType) type, typeReference);
            }
            int end = retrieveEndOfDimensionsPosition(sourceStart + length, this.compilationUnitSourceLength);
            if (end != -1) {
                type.setSourceRange(sourceStart, end - sourceStart + 1);
            } else {
                type.setSourceRange(sourceStart, length);
            }
            ArrayType subarrayType = (ArrayType) type;
            int index = dimensions - 1;
            while (index > 0) {
                subarrayType = (ArrayType) subarrayType.getComponentType();
                end = retrieveProperRightBracketPosition(index, sourceStart);
                subarrayType.setSourceRange(sourceStart, end - sourceStart + 1);
                index--;
            }
        }
    }
    if (this.resolveBindings) {
        this.recordNodes(type, typeReference);
    }
    boolean sawDiamond = false;
    if (typeReference instanceof ParameterizedSingleTypeReference) {
        ParameterizedSingleTypeReference pstr = (ParameterizedSingleTypeReference) typeReference;
        if (pstr.typeArguments == TypeReference.NO_TYPE_ARGUMENTS) {
            sawDiamond = true;
        }
    } else if (typeReference instanceof ParameterizedQualifiedTypeReference) {
        ParameterizedQualifiedTypeReference pqtr = (ParameterizedQualifiedTypeReference) typeReference;
        for (int i = 0, len = pqtr.typeArguments.length; i < len; i++) {
            if (pqtr.typeArguments[i] == TypeReference.NO_TYPE_ARGUMENTS) {
                sawDiamond = true;
                break;
            }
        }
    }
    if (sawDiamond) {
        switch (this.ast.apiLevel) {
        case AST.JLS2_INTERNAL:
        case AST.JLS3:
            type.setFlags(type.getFlags() | ASTNode.MALFORMED);
        }
    }
    return type;
}

From source file:org.moe.natjgen.TypeResolver.java

License:Apache License

@SuppressWarnings("unchecked")
private static org.eclipse.jdt.core.dom.Type _CreateObjCGenericType(TypeResolver resolver,
        AbstractUnitManager manager, Type baseType, SimpleType simpleBaseType) throws GeneratorException {
    // Check for errors
    if (resolver == null || manager == null || baseType == null) {
        throw new NullPointerException();
    }//  w  ww  . jav  a  2  s.  c  om
    if (baseType.getKind() != Type.ObjCObject) {
        throw new GeneratorException("Unsupported type in template (kind: " + baseType.getKind()
                + ", spelling: " + baseType.getTypeSpelling() + ")");
    }
    final ObjCClassManager clazz = manager.getGenerator().getClass(baseType.getElementName());
    if (clazz == null) {
        throw new RuntimeException("Couldn't find class with name: " + baseType.getElementName());
    }

    // Return simple type if class doesn't have generics
    if (clazz.genericParamTypes.size() == 0) {
        return simpleBaseType;
    }

    // Return wildcarded type 'SimpleType<?>' if class has generics, but it
    // is not specified on the type
    if (baseType.getObjCTypeArgs().size() == 0) {
        final int count = clazz.genericParamTypes.size();
        ParameterizedType parameterizedType = manager.getAST().newParameterizedType(simpleBaseType);
        for (int i = 0; i < count; ++i) {
            parameterizedType.typeArguments().add(manager.getAST().newWildcardType());
        }
        return parameterizedType;
    }

    // Handle possible error
    final int count = baseType.getObjCTypeArgs().size();
    if (count != clazz.genericParamTypes.size()) {
        // Something is very wrong, but still try not to fail
        return simpleBaseType;
    }

    // Try to create parameterized type
    ParameterizedType parameterizedType = manager.getAST().newParameterizedType(simpleBaseType);
    for (int i = 0; i < count; ++i) {
        try {
            final Type argType = baseType.getObjCTypeArgs().get(i);
            final ObjCGenericParamType paramType = clazz.genericParamTypes.get(i);
            org.eclipse.jdt.core.dom.Type type = CreateObjCGenericTypeArgument(resolver, manager, argType);
            if (!(type instanceof WildcardType)
                    && (argType.getKind() != Type.ObjCId || argType.getObjCGenericParamType() == null)) {
                if (paramType.isInvariant()) {
                    // Nothing to be done
                } else if (paramType.isCovariant()) {
                    WildcardType wildcardType = manager.getAST().newWildcardType();
                    if (!type.toString().equals("String")) {
                        if (!type.isSimpleType() || !type.toString().equals("Object")) {
                            wildcardType.setBound(type, true);
                        }
                        type = wildcardType;
                    }
                } else if (paramType.isContravariant()) {
                    WildcardType wildcardType = manager.getAST().newWildcardType();
                    wildcardType.setBound(type, false);
                    type = wildcardType;
                } else {
                    throw new RuntimeException("Illegal ObjC type variance");
                }
            }
            parameterizedType.typeArguments().add(type);
        } catch (GeneratorException ex) {
            // Something is very wrong, but still try not to fail
            return simpleBaseType;
        }
    }
    return parameterizedType;
}