Example usage for org.eclipse.jdt.internal.compiler.ast TypeReference NO_TYPE_ARGUMENTS

List of usage examples for org.eclipse.jdt.internal.compiler.ast TypeReference NO_TYPE_ARGUMENTS

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast TypeReference NO_TYPE_ARGUMENTS.

Prototype

TypeReference[] NO_TYPE_ARGUMENTS

To view the source code for org.eclipse.jdt.internal.compiler.ast TypeReference NO_TYPE_ARGUMENTS.

Click Source Link

Usage

From source file:com.android.tools.lint.psi.EcjPsiBuilder.java

License:Apache License

@SuppressWarnings("SameParameterValue")
@NonNull/*from   w  w w  . ja  v  a 2s .  c  o  m*/
private EcjPsiReferenceList toTypeReferenceList(@NonNull EcjPsiSourceElement parent,
        @Nullable TypeReference reference, @NonNull Role role) {
    TypeReference[] references = reference == null ? TypeReference.NO_TYPE_ARGUMENTS
            : new TypeReference[] { reference };
    return toTypeReferenceList(parent, references, role);
}

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 {/* w w w.  j a  va 2  s.co  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.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void checkForDiamond(TypeReference allocType) {
    if (allocType instanceof ParameterizedSingleTypeReference) {
        ParameterizedSingleTypeReference type = (ParameterizedSingleTypeReference) allocType;
        if (type.typeArguments == TypeReference.NO_TYPE_ARGUMENTS) {
            if (this.options.sourceLevel < ClassFileConstants.JDK1_7) {
                problemReporter().diamondNotBelow17(allocType);
            }//from   w w  w .jav  a 2s  .c  om
            if (this.options.sourceLevel > ClassFileConstants.JDK1_4) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=351965
                type.bits |= ASTNode.IsDiamond;
            } // else don't even bother to recognize this as <>
        }
    } else if (allocType instanceof ParameterizedQualifiedTypeReference) {
        ParameterizedQualifiedTypeReference type = (ParameterizedQualifiedTypeReference) allocType;
        if (type.typeArguments[type.typeArguments.length - 1] == TypeReference.NO_TYPE_ARGUMENTS) { // Don't care for X<>.Y<> and X<>.Y<String>
            if (this.options.sourceLevel < ClassFileConstants.JDK1_7) {
                problemReporter().diamondNotBelow17(allocType, type.typeArguments.length - 1);
            }
            if (this.options.sourceLevel > ClassFileConstants.JDK1_4) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=351965
                type.bits |= ASTNode.IsDiamond;
            } // else don't even bother to recognize this as <>
        }
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected TypeReference getTypeReferenceForGenericType(int dim, int identifierLength, int numberOfIdentifiers) {
    if (identifierLength == 1 && numberOfIdentifiers == 1) {
        int currentTypeArgumentsLength = this.genericsLengthStack[this.genericsLengthPtr--];
        TypeReference[] typeArguments = null;
        if (currentTypeArgumentsLength < 0) {
            typeArguments = TypeReference.NO_TYPE_ARGUMENTS;
        } else {//  ww w .ja  v a 2s  .  c  o m
            typeArguments = new TypeReference[currentTypeArgumentsLength];
            this.genericsPtr -= currentTypeArgumentsLength;
            System.arraycopy(this.genericsStack, this.genericsPtr + 1, typeArguments, 0,
                    currentTypeArgumentsLength);
        }
        ParameterizedSingleTypeReference parameterizedSingleTypeReference = new ParameterizedSingleTypeReference(
                this.identifierStack[this.identifierPtr], typeArguments, dim,
                this.identifierPositionStack[this.identifierPtr--]);
        if (dim != 0) {
            parameterizedSingleTypeReference.sourceEnd = this.endStatementPosition;
        }
        /* We used to eagerly mark the PSTR as constituting diamond usage if we encountered <>, but that is too eager and
           complicates error handling by making it hard to distinguish legitimate use cases from ill formed ones. We are
           more discriminating now and tag a type as being diamond only where <> can legally occur. 
           See https://bugs.eclipse.org/bugs/show_bug.cgi?id=339478#c11
        */
        return parameterizedSingleTypeReference;
    } else {
        TypeReference[][] typeArguments = new TypeReference[numberOfIdentifiers][];
        char[][] tokens = new char[numberOfIdentifiers][];
        long[] positions = new long[numberOfIdentifiers];
        int index = numberOfIdentifiers;
        int currentIdentifiersLength = identifierLength;
        while (index > 0) {
            int currentTypeArgumentsLength = this.genericsLengthStack[this.genericsLengthPtr--];
            if (currentTypeArgumentsLength > 0) {
                this.genericsPtr -= currentTypeArgumentsLength;
                System.arraycopy(this.genericsStack, this.genericsPtr + 1,
                        typeArguments[index - 1] = new TypeReference[currentTypeArgumentsLength], 0,
                        currentTypeArgumentsLength);
            } else if (currentTypeArgumentsLength < 0) {
                // diamond case for qualified type reference (java.util.ArrayList<>)
                typeArguments[index - 1] = TypeReference.NO_TYPE_ARGUMENTS;
            }
            switch (currentIdentifiersLength) {
            case 1:
                // we are in a case A<B>.C<D> or A<B>.C<D>
                tokens[index - 1] = this.identifierStack[this.identifierPtr];
                positions[index - 1] = this.identifierPositionStack[this.identifierPtr--];
                break;
            default:
                // we are in a case A.B.C<B>.C<D> or A.B.C<B>...
                this.identifierPtr -= currentIdentifiersLength;
                System.arraycopy(this.identifierStack, this.identifierPtr + 1, tokens,
                        index - currentIdentifiersLength, currentIdentifiersLength);
                System.arraycopy(this.identifierPositionStack, this.identifierPtr + 1, positions,
                        index - currentIdentifiersLength, currentIdentifiersLength);
            }
            index -= currentIdentifiersLength;
            if (index > 0) {
                currentIdentifiersLength = this.identifierLengthStack[this.identifierLengthPtr--];
            }
        }
        ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference = new ParameterizedQualifiedTypeReference(
                tokens, typeArguments, dim, positions);
        if (dim != 0) {
            parameterizedQualifiedTypeReference.sourceEnd = this.endStatementPosition;
        }
        /* We used to eagerly mark the PQTR as constituting diamond usage if we encountered <>, but that is too eager and
           complicates error handling by making it hard to distinguish legitimate use cases from ill formed ones. We are
           more discriminating now and tag a type as being diamond only where <> can legally occur. 
           See https://bugs.eclipse.org/bugs/show_bug.cgi?id=339478#c11
        */
        return parameterizedQualifiedTypeReference;
    }
}