Example usage for org.eclipse.jdt.core ITypeParameter getBounds

List of usage examples for org.eclipse.jdt.core ITypeParameter getBounds

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeParameter getBounds.

Prototype

String[] getBounds() throws JavaModelException;

Source Link

Document

Returns the names of the class and interface bounds of this type parameter.

Usage

From source file:at.bestsolution.fxide.jdt.corext.util.MethodOverrideTester.java

License:Open Source License

private String getTypeParameterErasure(ITypeParameter typeParameter, IType context) throws JavaModelException {
    String[] bounds = typeParameter.getBounds();
    if (bounds.length > 0) {
        return getErasedTypeName(Signature.createTypeSignature(bounds[0], false), context);
    }/*from   w  w  w  .  j a v  a 2 s.c om*/
    return "Object"; //$NON-NLS-1$
}

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

private AbstractMethodDeclaration convert(IMethod method, IType type) throws JavaModelException {

    AbstractMethodDeclaration methodDeclaration;

    org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = null;

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert type parameters */
        ITypeParameter[] typeParameters = method.getTypeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            int parameterCount = typeParameters.length;
            typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(),
                        stringArrayToCharArray(typeParameter.getBounds()), 0, 0);
            }//from w w w .j  a  v a 2 s  .  c o m
        }
    }

    if (method.isConstructor()) {
        ConstructorDeclaration decl = new ConstructorDeclaration(this.compilationResult);
        decl.bits &= ~ASTNode.IsDefaultConstructor;
        decl.typeParameters = typeParams;
        methodDeclaration = decl;
    } else {
        MethodDeclaration decl = type.isAnnotation() ? new AnnotationMethodDeclaration(this.compilationResult)
                : new MethodDeclaration(this.compilationResult);
        /* convert return type */
        TypeReference typeReference = createTypeReference(method.getReturnType());
        if (typeReference == null)
            return null;
        decl.returnType = typeReference;
        decl.typeParameters = typeParams;
        methodDeclaration = decl;
    }
    methodDeclaration.selector = method.getElementName().toCharArray();
    int flags = method.getFlags();
    boolean isVarargs = Flags.isVarargs(flags);
    methodDeclaration.modifiers = flags & ~Flags.AccVarargs;

    /* convert arguments */
    String[] argumentTypeNames = method.getParameterTypes();
    String[] argumentNames = method.getParameterNames();
    int argumentCount = argumentTypeNames == null ? 0 : argumentTypeNames.length;
    // Ignore synthetic arguments (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=212224)
    int startIndex = (method.isConstructor() && type.isMember() && !Flags.isStatic(type.getFlags())) ? 1 : 0;
    argumentCount -= startIndex;
    methodDeclaration.arguments = new Argument[argumentCount];
    for (int i = 0; i < argumentCount; i++) {
        String argumentTypeName = argumentTypeNames[startIndex + i];
        TypeReference typeReference = createTypeReference(argumentTypeName);
        if (typeReference == null)
            return null;
        if (isVarargs && i == argumentCount - 1) {
            typeReference.bits |= ASTNode.IsVarArgs;
        }
        methodDeclaration.arguments[i] = new Argument(argumentNames[i].toCharArray(), 0, typeReference,
                ClassFileConstants.AccDefault);
        // do not care whether was final or not
    }

    /* convert thrown exceptions */
    String[] exceptionTypeNames = method.getExceptionTypes();
    int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
    if (exceptionCount > 0) {
        methodDeclaration.thrownExceptions = new TypeReference[exceptionCount];
        for (int i = 0; i < exceptionCount; i++) {
            TypeReference typeReference = createTypeReference(exceptionTypeNames[i]);
            if (typeReference == null)
                return null;
            methodDeclaration.thrownExceptions[i] = typeReference;
        }
    }
    return methodDeclaration;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

private TypeDeclaration convert(IType type, IType alreadyComputedMember,
        TypeDeclaration alreadyComputedMemberDeclaration) throws JavaModelException {
    /* create type declaration - can be member type */
    TypeDeclaration typeDeclaration = new TypeDeclaration(this.compilationResult);

    if (type.getDeclaringType() != null) {
        typeDeclaration.bits |= ASTNode.IsMemberType;
    }//from w  w w. ja v  a2  s . c  om
    typeDeclaration.name = type.getElementName().toCharArray();
    typeDeclaration.modifiers = type.getFlags();

    /* set superclass and superinterfaces */
    if (type.getSuperclassName() != null) {
        TypeReference typeReference = createTypeReference(type.getSuperclassTypeSignature());
        if (typeReference != null) {
            typeDeclaration.superclass = typeReference;
            typeDeclaration.superclass.bits |= ASTNode.IsSuperType;
        }
    }

    String[] interfaceTypes = type.getSuperInterfaceTypeSignatures();
    int interfaceCount = interfaceTypes == null ? 0 : interfaceTypes.length;
    typeDeclaration.superInterfaces = new TypeReference[interfaceCount];
    int count = 0;
    for (int i = 0; i < interfaceCount; i++) {
        TypeReference typeReference = createTypeReference(interfaceTypes[i]);
        if (typeReference != null) {
            typeDeclaration.superInterfaces[count] = typeReference;
            typeDeclaration.superInterfaces[count++].bits |= ASTNode.IsSuperType;
        }
    }
    if (count != interfaceCount) {
        System.arraycopy(typeDeclaration.fields, 0,
                typeDeclaration.superInterfaces = new TypeReference[interfaceCount], 0, interfaceCount);
    }

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {

        /* convert type parameters */
        ITypeParameter[] typeParameters = type.getTypeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            int parameterCount = typeParameters.length;
            org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(),
                        stringArrayToCharArray(typeParameter.getBounds()), 0, 0);
            }

            typeDeclaration.typeParameters = typeParams;
        }
    }

    /* convert member types */
    IType[] memberTypes = type.getTypes();
    int memberTypeCount = memberTypes == null ? 0 : memberTypes.length;
    typeDeclaration.memberTypes = new TypeDeclaration[memberTypeCount];
    for (int i = 0; i < memberTypeCount; i++) {
        if (alreadyComputedMember != null && alreadyComputedMember.getFullyQualifiedName()
                .equals(memberTypes[i].getFullyQualifiedName())) {
            typeDeclaration.memberTypes[i] = alreadyComputedMemberDeclaration;
        } else {
            typeDeclaration.memberTypes[i] = convert(memberTypes[i], null, null);
        }
        typeDeclaration.memberTypes[i].enclosingType = typeDeclaration;
    }

    /* convert fields */
    IField[] fields = type.getFields();
    int fieldCount = fields == null ? 0 : fields.length;
    typeDeclaration.fields = new FieldDeclaration[fieldCount];
    count = 0;
    for (int i = 0; i < fieldCount; i++) {
        FieldDeclaration fieldDeclaration = convert(fields[i], type);
        if (fieldDeclaration != null) {
            typeDeclaration.fields[count++] = fieldDeclaration;
        }
    }
    if (count != fieldCount) {
        System.arraycopy(typeDeclaration.fields, 0, typeDeclaration.fields = new FieldDeclaration[count], 0,
                count);
    }

    /* convert methods - need to add default constructor if necessary */
    IMethod[] methods = type.getMethods();
    int methodCount = methods == null ? 0 : methods.length;

    /* source type has a constructor ?           */
    /* by default, we assume that one is needed. */
    int neededCount = 1;
    for (int i = 0; i < methodCount; i++) {
        if (methods[i].isConstructor()) {
            neededCount = 0;
            // Does not need the extra constructor since one constructor already exists.
            break;
        }
    }
    boolean isInterface = type.isInterface();
    neededCount = isInterface ? 0 : neededCount;
    typeDeclaration.methods = new AbstractMethodDeclaration[methodCount + neededCount];
    if (neededCount != 0) { // add default constructor in first position
        typeDeclaration.methods[0] = typeDeclaration.createDefaultConstructor(false, false);
    }
    boolean hasAbstractMethods = false;
    count = 0;
    for (int i = 0; i < methodCount; i++) {
        AbstractMethodDeclaration method = convert(methods[i], type);
        if (method != null) {
            boolean isAbstract;
            if ((isAbstract = method.isAbstract()) || isInterface) { // fix-up flag
                method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
            }
            if (isAbstract) {
                hasAbstractMethods = true;
            }
            typeDeclaration.methods[neededCount + (count++)] = method;
        }
    }
    if (count != methodCount) {
        System.arraycopy(typeDeclaration.methods, 0,
                typeDeclaration.methods = new AbstractMethodDeclaration[count + neededCount], 0,
                count + neededCount);
    }
    if (hasAbstractMethods) {
        typeDeclaration.bits |= ASTNode.HasAbstractMethods;
    }
    return typeDeclaration;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.NamedMember.java

License:Open Source License

private void appendTypeParameters(StringBuffer buffer) throws JavaModelException {
    ITypeParameter[] typeParameters = getTypeParameters();
    int length = typeParameters.length;
    if (length == 0)
        return;// w w w . j a  va  2 s.c om
    buffer.append('<');
    for (int i = 0; i < length; i++) {
        ITypeParameter typeParameter = typeParameters[i];
        buffer.append(typeParameter.getElementName());
        String[] bounds = typeParameter.getBounds();
        int boundsLength = bounds.length;
        if (boundsLength > 0) {
            buffer.append(" extends "); //$NON-NLS-1$
            for (int j = 0; j < boundsLength; j++) {
                buffer.append(bounds[j]);
                if (j < boundsLength - 1)
                    buffer.append(" & "); //$NON-NLS-1$
            }
        }
        if (i < length - 1)
            buffer.append(", "); //$NON-NLS-1$
    }
    buffer.append('>');
}

From source file:com.codenvy.ide.ext.java.server.internal.core.NamedMember.java

License:Open Source License

protected String getKey(IMethod method, boolean forceOpen) throws JavaModelException {
    StringBuffer key = new StringBuffer();

    // declaring class
    String declaringKey = getKey((IType) method.getParent(), forceOpen);
    key.append(declaringKey);//ww w  .j a  va  2s. c  o  m

    // selector
    key.append('.');
    String selector = method.getElementName();
    key.append(selector);

    // type parameters
    if (forceOpen) {
        ITypeParameter[] typeParameters = method.getTypeParameters();
        int length = typeParameters.length;
        if (length > 0) {
            key.append('<');
            for (int i = 0; i < length; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                String[] bounds = typeParameter.getBounds();
                int boundsLength = bounds.length;
                char[][] boundSignatures = new char[boundsLength][];
                for (int j = 0; j < boundsLength; j++) {
                    boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j].toCharArray(),
                            method.isBinary());
                    CharOperation.replace(boundSignatures[j], '.', '/');
                }
                char[] sig = Signature.createTypeParameterSignature(
                        typeParameter.getElementName().toCharArray(), boundSignatures);
                key.append(sig);
            }
            key.append('>');
        }
    }

    // parameters
    key.append('(');
    String[] parameters = method.getParameterTypes();
    for (int i = 0, length = parameters.length; i < length; i++)
        key.append(parameters[i].replace('.', '/'));
    key.append(')');

    // return type
    if (forceOpen)
        key.append(method.getReturnType().replace('.', '/'));
    else
        key.append('V');

    return key.toString();
}

From source file:com.mountainminds.eclemma.internal.core.analysis.SignatureResolver.java

License:Open Source License

private static final boolean resolveTypeParameter(final IType context, final ITypeParameter[] typeParameters,
        final String identifier, final StringBuffer result) throws JavaModelException {
    for (final ITypeParameter p : typeParameters) {
        if (identifier.equals(p.getElementName())) {
            final String[] bounds = p.getBounds();
            if (bounds.length == 0) {
                result.append(OBJECT);//  w  ww . j  ava  2s  . co  m
                return true;
            } else {
                return resolveType(context, bounds[0], result);
            }
        }
    }
    return false;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

private static void appendGenericType(StringBuffer sb, IMethod iMethod, String unresolvedType)
        throws JavaModelException {
    IType declaringType = iMethod.getDeclaringType();

    // unresolvedType is here like "QA;" => we remove "Q" and ";"
    if (unresolvedType.length() < 3) {
        // ???? something wrong here ....
        sb.append(unresolvedType);/*from   ww w.j  ava 2s. co m*/
        return;
    }
    unresolvedType = unresolvedType.substring(1, unresolvedType.length() - 1);

    ITypeParameter typeParameter = iMethod.getTypeParameter(unresolvedType);
    if (typeParameter == null || !typeParameter.exists()) {
        typeParameter = declaringType.getTypeParameter(unresolvedType);
    }

    String[] bounds = typeParameter.getBounds();
    if (bounds.length == 0) {
        sb.append("Ljava/lang/Object;");
    } else {
        for (int i = 0; i < bounds.length; i++) {
            String simplyName = bounds[i];
            simplyName = Signature.C_UNRESOLVED + simplyName + Signature.C_NAME_END;
            String resolvedType = getResolvedType(simplyName, declaringType);
            sb.append(resolvedType);
        }
    }
}

From source file:edu.uci.ics.sourcerer.extractor.ast.ClassFileExtractor.java

License:Open Source License

private String getTypeParam(ITypeParameter typeParam) {
    try {//from  w  ww.jav  a  2 s.  c  o  m
        StringBuilder builder = new StringBuilder();
        builder.append('<').append(typeParam.getElementName());
        boolean first = true;

        for (String bound : typeParam.getBounds()) {
            if (first) {
                first = false;
                builder.append('+');
            } else {
                builder.append('&');
            }
            builder.append(bound.replace(" extends ", "+"));
        }

        builder.append('>');
        return builder.toString();
    } catch (JavaModelException e) {
        e.printStackTrace();
        System.out.println("bad");
        return null;
    }
}

From source file:org.eclim.plugin.jdt.util.TypeUtils.java

License:Open Source License

/**
 * Converts the supplied type signature with generic information to the most
 * basic type that it supports.//from  www .  jav a 2s  .co  m
 *
 * @param type The parent IType.
 * @param typeSignature The type signature.
 * @return The base type.
 */
public static String getBaseTypeFromGeneric(IType type, String typeSignature) throws Exception {
    int arrayCount = Signature.getArrayCount(typeSignature);
    if (arrayCount > 0) {
        for (int ii = 0; ii < arrayCount; ii++) {
            typeSignature = Signature.getElementType(typeSignature);
        }
    }

    String result = null;
    ITypeParameter param = type.getTypeParameter(Signature.getSignatureSimpleName(typeSignature));
    if (param.exists()) {
        result = param.getBounds()[0];
    } else {
        result = Signature.getSignatureSimpleName(Signature.getTypeErasure(typeSignature));
    }

    if (arrayCount > 0) {
        for (int ii = 0; ii < arrayCount; ii++) {
            result = result + "[]";
        }
    }
    return result;
}

From source file:org.eclipse.eclemma.internal.core.analysis.SignatureResolver.java

License:Open Source License

private static final boolean resolveTypeParameter(final IType context, final ITypeParameter[] typeParameters,
        final String identifier, final StringBuilder result) throws JavaModelException {
    for (final ITypeParameter p : typeParameters) {
        if (identifier.equals(p.getElementName())) {
            final String[] bounds = p.getBounds();
            if (bounds.length == 0) {
                result.append(OBJECT);//from   w ww.jav a2  s .c o m
                return true;
            } else {
                return resolveType(context, bounds[0], result);
            }
        }
    }
    return false;
}