Example usage for org.eclipse.jdt.core IType getTypeParameter

List of usage examples for org.eclipse.jdt.core IType getTypeParameter

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getTypeParameter.

Prototype

ITypeParameter getTypeParameter(String name);

Source Link

Document

Returns the type parameter declared in this type with the given name.

Usage

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

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor
 */// w ww  .  j  a  va 2  s.co  m
public void enterType(TypeInfo typeInfo) {

    this.typeDepth++;
    if (this.typeDepth == this.types.length) { // need to grow
        System.arraycopy(this.types, 0, this.types = new IType[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.typeNameRanges, 0, this.typeNameRanges = new SourceRange[this.typeDepth * 2], 0,
                this.typeDepth);
        System.arraycopy(this.typeDeclarationStarts, 0,
                this.typeDeclarationStarts = new int[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberName, 0, this.memberName = new String[this.typeDepth * 2], 0,
                this.typeDepth);
        System.arraycopy(this.memberDeclarationStart, 0,
                this.memberDeclarationStart = new int[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberNameRange, 0, this.memberNameRange = new SourceRange[this.typeDepth * 2], 0,
                this.typeDepth);
        System.arraycopy(this.methodParameterTypes, 0,
                this.methodParameterTypes = new char[this.typeDepth * 2][][], 0, this.typeDepth);
        System.arraycopy(this.methodParameterNames, 0,
                this.methodParameterNames = new char[this.typeDepth * 2][][], 0, this.typeDepth);
        System.arraycopy(this.typeModifiers, 0, this.typeModifiers = new int[this.typeDepth * 2], 0,
                this.typeDepth);
    }
    if (typeInfo.name.length == 0) {
        this.anonymousCounter++;
        if (this.anonymousCounter == this.anonymousClassName) {
            this.types[this.typeDepth] = getType(this.binaryType.getElementName());
        } else {
            this.types[this.typeDepth] = getType(new String(typeInfo.name));
        }
    } else {
        this.types[this.typeDepth] = getType(new String(typeInfo.name));
    }
    this.typeNameRanges[this.typeDepth] = new SourceRange(typeInfo.nameSourceStart,
            typeInfo.nameSourceEnd - typeInfo.nameSourceStart + 1);
    this.typeDeclarationStarts[this.typeDepth] = typeInfo.declarationStart;

    IType currentType = this.types[this.typeDepth];

    // type parameters
    if (typeInfo.typeParameters != null) {
        for (int i = 0, length = typeInfo.typeParameters.length; i < length; i++) {
            TypeParameterInfo typeParameterInfo = typeInfo.typeParameters[i];
            ITypeParameter typeParameter = currentType.getTypeParameter(new String(typeParameterInfo.name));
            setSourceRange(typeParameter,
                    new SourceRange(typeParameterInfo.declarationStart,
                            typeParameterInfo.declarationEnd - typeParameterInfo.declarationStart + 1),
                    new SourceRange(typeParameterInfo.nameSourceStart,
                            typeParameterInfo.nameSourceEnd - typeParameterInfo.nameSourceStart + 1));
        }
    }

    // type modifiers
    this.typeModifiers[this.typeDepth] = typeInfo.modifiers;

    // categories
    addCategories(currentType, typeInfo.categories);
}

From source file:com.google.inject.tools.ideplugin.eclipse.TypeUtil.java

License:Open Source License

/**
 * @param owningType -- type relative to which typeSignature will be resolved
 * @param typeSignature -- non-array type signature
 * @return the resolved type signature if possible or typeSignature if not
 *///  w w  w  . jav  a 2 s.  c  om
private static String resolveSignatureRelative(final IType owningType, final String typeSignature,
        final boolean eraseTypeParameters) {
    // if already fully resolved, return the input
    if (typeSignature.charAt(0) == Signature.C_RESOLVED) {
        return typeSignature;
    }

    List<String> typeParameters = new ArrayList<String>();

    IType resolvedType = resolveTypeRelative(owningType, typeSignature);

    if (resolvedType != null) {
        if (!eraseTypeParameters) {
            // ensure that type parameters are resolved recursively
            for (String typeParam : Signature.getTypeArguments(typeSignature)) {
                typeParam = Signature.removeCapture(typeParam);
                // check and remove bound wildcarding (extends/super/?)
                if (Signature.getTypeSignatureKind(typeParam) == Signature.WILDCARD_TYPE_SIGNATURE) {
                    // convert ? to Object, strip extends/super
                    if (typeParam.charAt(0) == Signature.C_STAR) {
                        typeParam = TypeConstants.TYPE_JAVAOBJECT;
                    } else {
                        typeParam = typeParam.substring(1);
                    }
                }
                final String resolvedParameter = resolveSignatureRelative(
                        // use the enclosing type, 
                        // *not* the resolved type because 
                        // we need to resolve in that context
                        owningType, typeParam, eraseTypeParameters);
                typeParameters.add(resolvedParameter);
            }
        }

        final String resolvedTypeSignature = Signature.createTypeSignature(resolvedType.getFullyQualifiedName(),
                true);

        if (typeParameters.size() > 0 && !eraseTypeParameters) {
            StringBuffer sb = new StringBuffer(resolvedTypeSignature);

            if (sb.charAt(sb.length() - 1) == ';') {
                sb = sb.delete(sb.length() - 1, sb.length());
            }

            sb.append("<"); //$NON-NLS-1$
            for (String param : typeParameters) {
                //System.out.println("type param: "+resolvedType.getTypeParameter(param));
                sb.append(param);
            }

            // replace the dangling ',' with the closing ">"
            sb.append(">;"); //$NON-NLS-1$
            return sb.toString();
        }

        return resolvedTypeSignature;
    }

    if (Signature.getTypeSignatureKind(typeSignature) == Signature.CLASS_TYPE_SIGNATURE
            || Signature.getTypeSignatureKind(typeSignature) == Signature.TYPE_VARIABLE_SIGNATURE) {
        // if we are unable to resolve, check to see if the owning type has
        // a parameter by this name
        ITypeParameter typeParam = owningType.getTypeParameter(Signature.getSignatureSimpleName(typeSignature));

        // if we have a type parameter and it hasn't been resolved to a type,
        // then assume it is a method template placeholder (i.e. T in ArrayList).
        // at runtime these unresolved parameter variables are effectively 
        // turned into Object's.  For example, think List.add(E o).  At runtime,
        // E will behave exactly like java.lang.Object in that signature
        if (typeParam.exists()) {
            return TypeConstants.TYPE_JAVAOBJECT;
        }
    }

    return typeSignature;
}

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);//  w w  w.j a v  a2 s  . 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: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./* w ww .  j a v a2  s .c  o  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.che.jdt.refactoring.RenameTypeParameterTest.java

License:Open Source License

private void helper1(String parameterName, String newParameterName, String typeName, boolean references)
        throws Exception {
    IType declaringType = getType(createCUfromTestFile(getPackageP(), "A"), typeName);
    RenameTypeParameterProcessor processor = new RenameTypeParameterProcessor(
            declaringType.getTypeParameter(parameterName));
    RenameRefactoring refactoring = new RenameRefactoring(processor);
    processor.setNewElementName(newParameterName);
    processor.setUpdateReferences(references);
    RefactoringStatus result = performRefactoring(refactoring);
    assertNotNull("precondition was supposed to fail", result);
}

From source file:org.eclipse.che.jdt.refactoring.RenameTypeParameterTest.java

License:Open Source License

private void helper2(String parameterName, String newParameterName, String typeName, boolean references)
        throws Exception {
    ParticipantTesting.reset();/* w ww  .  j  a v a  2 s.c o m*/
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType declaringType = getType(cu, typeName);
    ITypeParameter typeParameter = declaringType.getTypeParameter(parameterName);
    RenameTypeParameterProcessor processor = new RenameTypeParameterProcessor(typeParameter);
    RenameRefactoring refactoring = new RenameRefactoring(processor);
    processor.setNewElementName(newParameterName);
    processor.setUpdateReferences(references);

    RefactoringStatus result = performRefactoring(refactoring);
    assertEquals("was supposed to pass", null, result);
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());

    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());

    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());

    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());

    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}

From source file:org.eclipse.jst.jsf.common.util.TypeUtil.java

License:Open Source License

/**
 * @param owningType -- type relative to which typeSignature will be resolved
 * @param typeSignature -- non-array type signature
 * @return the resolved type signature if possible or typeSignature if not
 *//*from   w ww. j a  v a 2  s  .c o  m*/
private static String resolveSignatureRelative(final IType owningType, final String typeSignature,
        final boolean eraseTypeParameters) {
    // if already fully resolved, return the input
    if (typeSignature.charAt(0) == Signature.C_RESOLVED) {
        return typeSignature;
    }

    List<String> typeParameters = new ArrayList<String>();

    IType resolvedType = resolveTypeRelative(owningType, typeSignature);

    if (resolvedType != null) {
        if (!eraseTypeParameters) {
            // ensure that type parameters are resolved recursively
            for (String typeParam : Signature.getTypeArguments(typeSignature)) {
                typeParam = Signature.removeCapture(typeParam);
                // check and remove bound wildcarding (extends/super/?)
                if (Signature.getTypeSignatureKind(typeParam) == Signature.WILDCARD_TYPE_SIGNATURE) {
                    // convert ? to Object, strip extends/super
                    if (typeParam.charAt(0) == Signature.C_STAR) {
                        typeParam = TypeConstants.TYPE_JAVAOBJECT;
                    } else {
                        typeParam = typeParam.substring(1);
                    }
                }
                final String resolvedParameter = resolveSignatureRelative(
                        // use the enclosing type, 
                        // *not* the resolved type because 
                        // we need to resolve in that context
                        owningType, typeParam, eraseTypeParameters);
                typeParameters.add(resolvedParameter);
            }
        }

        final String resolvedTypeSignature = Signature.createTypeSignature(resolvedType.getFullyQualifiedName(),
                true);

        if (typeParameters.size() > 0 && !eraseTypeParameters) {
            StringBuffer sb = new StringBuffer(resolvedTypeSignature);

            if (sb.charAt(sb.length() - 1) == ';') {
                sb = sb.delete(sb.length() - 1, sb.length());
            }

            sb.append("<"); //$NON-NLS-1$
            for (String param : typeParameters) {
                //System.out.println("type param: "+resolvedType.getTypeParameter(param));
                sb.append(param);
            }

            // replace the dangling ',' with the closing ">"
            sb.append(">;"); //$NON-NLS-1$
            return sb.toString();
        }

        return resolvedTypeSignature;
    }

    if (Signature.getTypeSignatureKind(typeSignature) == Signature.CLASS_TYPE_SIGNATURE
            || Signature.getTypeSignatureKind(typeSignature) == Signature.TYPE_VARIABLE_SIGNATURE) {
        // if we are unable to resolve, check to see if the owning type has
        // a parameter by this name
        ITypeParameter typeParam = owningType.getTypeParameter(Signature.getSignatureSimpleName(typeSignature));

        // if we have a type parameter and it hasn't been resolved to a type,
        // then assume it is a method template placeholder (i.e. T in ArrayList).
        // at runtime these unresolved parameter variables are effectively 
        // turned into Object's.  For example, think List.add(E o).  At runtime,
        // E will behave exactly like java.lang.Object in that signature
        if (typeParam.exists()) {
            return TypeConstants.TYPE_JAVAOBJECT;
        }

        // TODO: is there a better way to handle a failure to resolve
        // than just garbage out?
        //JSFCommonPlugin.log(new Exception("Failed to resolve type: "+typeSignature), "Failed to resolve type: "+typeSignature); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return typeSignature;
}

From source file:org.eclipse.objectteams.otdt.debug.ui.internal.actions.OTToggleBreakpointAdapter.java

License:Open Source License

/**
 * Returns the resolved type signature for the given signature in the given
 * method, or <code>null</code> if unable to resolve.
 * //from w w  w  . j av  a2s  .com
 * @param method method containing the type signature
 * @param typeSignature the type signature to resolve
 * @return the resolved type signature
 * @throws JavaModelException
 */
private static String resolveTypeSignature(IMethod method, String typeSignature) throws JavaModelException {
    int count = Signature.getArrayCount(typeSignature);
    String elementTypeSignature = Signature.getElementType(typeSignature);
    if (elementTypeSignature.length() == 1) {
        // no need to resolve primitive types
        return typeSignature;
    }
    String elementTypeName = Signature.toString(elementTypeSignature);
    IType type = method.getDeclaringType();
    String[][] resolvedElementTypeNames = type.resolveType(elementTypeName);
    if (resolvedElementTypeNames == null || resolvedElementTypeNames.length != 1) {
        // check if type parameter
        ITypeParameter typeParameter = method.getTypeParameter(elementTypeName);
        if (!typeParameter.exists()) {
            typeParameter = type.getTypeParameter(elementTypeName);
        }
        if (typeParameter.exists()) {
            String[] bounds = typeParameter.getBounds();
            if (bounds.length == 0) {
                return "Ljava/lang/Object;"; //$NON-NLS-1$
            }
            String bound = Signature.createTypeSignature(bounds[0], false);
            return Signature.createArraySignature(resolveTypeSignature(method, bound), count);
        }
        // the type name cannot be resolved
        return null;
    }

    String[] types = resolvedElementTypeNames[0];
    types[1] = types[1].replace('.', '$');

    String resolvedElementTypeName = Signature.toQualifiedName(types);
    String resolvedElementTypeSignature = EMPTY_STRING;
    if (types[0].equals(EMPTY_STRING)) {
        resolvedElementTypeName = resolvedElementTypeName.substring(1);
        resolvedElementTypeSignature = Signature.createTypeSignature(resolvedElementTypeName, true);
    } else {
        resolvedElementTypeSignature = Signature.createTypeSignature(resolvedElementTypeName, true).replace('.',
                '/');
    }

    return Signature.createArraySignature(resolvedElementTypeSignature, count);
}

From source file:org.eclipse.recommenders.jdt.JavaElementsFinder.java

License:Open Source License

/**
 *
 * @param typeSignature/*from  ww w.  ja v  a 2s.  com*/
 *            e.g., QList;
 * @param enclosing
 * @return
 */
public static Optional<ITypeName> resolveType(char[] typeSignature, @Nullable IJavaElement enclosing) {
    typeSignature = CharOperation.replaceOnCopy(typeSignature, '.', '/');
    VmTypeName res = null;
    try {
        int dimensions = Signature.getArrayCount(typeSignature);
        outer: switch (typeSignature[dimensions]) {

        case Signature.C_BOOLEAN:
        case Signature.C_BYTE:
        case Signature.C_CHAR:
        case Signature.C_DOUBLE:
        case Signature.C_FLOAT:
        case Signature.C_INT:
        case Signature.C_LONG:
        case Signature.C_SHORT:
        case Signature.C_VOID:
            // take the whole string including any arrays
            res = VmTypeName.get(new String(typeSignature, 0, typeSignature.length));
            break;
        case Signature.C_RESOLVED:
            // take the whole string including any arrays but remove the trailing ';'
            res = VmTypeName.get(new String(typeSignature, 0, typeSignature.length - 1 /* ';' */));
            break;
        case Signature.C_UNRESOLVED:
            if (enclosing == null) {
                break;
            }
            // take the whole string (e.g. QList; or [QList;)
            String unresolved = new String(typeSignature, dimensions + 1,
                    typeSignature.length - (dimensions + 2 /* 'Q' + ';' */));
            IType ancestor = (IType) enclosing.getAncestor(IJavaElement.TYPE);
            if (ancestor == null) {
                break;
            }
            final String[][] resolvedNames = ancestor.resolveType(unresolved);
            if (isEmpty(resolvedNames)) {
                break;
            }
            String array = repeat('[', dimensions);
            final String pkg = resolvedNames[0][0].replace('.', '/');
            final String name = resolvedNames[0][1].replace('.', '$');
            res = VmTypeName.get(array + 'L' + pkg + '/' + name);
            break;
        case Signature.C_TYPE_VARIABLE:
            String varName = new String(typeSignature, dimensions + 1,
                    typeSignature.length - (dimensions + 2 /* 'Q' + ';' */));
            array = repeat('[', dimensions);

            for (IJavaElement cur = enclosing; cur instanceof IType
                    || cur instanceof IMethod; cur = cur.getParent()) {
                switch (cur.getElementType()) {
                case TYPE: {
                    IType type = (IType) cur;
                    ITypeParameter param = type.getTypeParameter(varName);
                    if (param.exists()) {
                        String[] signatures = getBoundSignatures(param);
                        if (isEmpty(signatures)) {
                            res = VmTypeName.OBJECT;
                            break outer;
                        }
                        // XXX we only consider the first type.
                        char[] append = array.concat(signatures[0]).toCharArray();
                        return resolveType(append, type);
                    }
                }
                case METHOD: {
                    IMethod method = (IMethod) cur;
                    ITypeParameter param = method.getTypeParameter(varName);
                    if (param.exists()) {
                        String[] signatures = getBoundSignatures(param);
                        if (isEmpty(signatures)) {
                            res = dimensions == 0 ? OBJECT
                                    : VmTypeName.get(repeat('[', dimensions) + OBJECT.getIdentifier());
                            break outer;
                        }
                        // XXX we only consider the first type.
                        char[] append = array.concat(signatures[0]).toCharArray();
                        return resolveType(append, method);
                    }
                }
                }
            }

            break;
        default:
            break;
        }
    } catch (Exception e) {
        Logs.log(LogMessages.ERROR_FAILED_TO_CREATE_TYPENAME, e,
                charToString(typeSignature) + (enclosing != null ? " in " + enclosing.getElementName() : ""));
    }
    return Optional.<ITypeName>fromNullable(res);
}

From source file:org.eclipse.wb.internal.core.utils.jdt.core.CodeUtils.java

License:Open Source License

/**
 * If given name is name of generic {@link TypeVariable}, returns its bounds.
 *///from  w ww  .  j  a v a 2  s  .co  m
private static String getResolvedTypeName_resolveTypeVariable(IType context, String name)
        throws JavaModelException {
    ITypeParameter typeParameter = context.getTypeParameter(name);
    if (typeParameter.exists()) {
        String[] bounds = typeParameter.getBounds();
        if (bounds.length != 0) {
            name = bounds[0];
        } else {
            name = "java.lang.Object";
        }
    }
    return name;
}