Example usage for org.eclipse.jdt.core Signature getTypeVariable

List of usage examples for org.eclipse.jdt.core Signature getTypeVariable

Introduction

In this page you can find the example usage for org.eclipse.jdt.core Signature getTypeVariable.

Prototype

public static String getTypeVariable(String formalTypeParameterSignature) throws IllegalArgumentException 

Source Link

Document

Extracts the type variable name from the given formal type parameter signature.

Usage

From source file:at.bestsolution.fxide.jdt.text.viewersupport.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends labels for type parameters from a signature.
 *
 * @param typeParamSigs the type parameter signature
 * @param flags flags with render options
 *///w w w  .j  a va2  s.com
private void appendTypeParameterSignaturesLabel(String[] typeParamSigs, long flags) {
    if (typeParamSigs.length > 0) {
        fBuffer.append(getLT());
        for (int i = 0; i < typeParamSigs.length; i++) {
            if (i > 0) {
                fBuffer.append(JavaElementLabels.COMMA_STRING);
            }
            fBuffer.append(Signature.getTypeVariable(typeParamSigs[i]));
        }
        fBuffer.append(getGT());
    }
}

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

License:Open Source License

public ITypeParameter[] getTypeParameters() throws JavaModelException {
    String[] typeParameterSignatures = getTypeParameterSignatures();
    int length = typeParameterSignatures.length;
    if (length == 0)
        return TypeParameter.NO_TYPE_PARAMETERS;
    ITypeParameter[] typeParameters = new ITypeParameter[length];
    for (int i = 0; i < typeParameterSignatures.length; i++) {
        String typeParameterName = Signature.getTypeVariable(typeParameterSignatures[i]);
        typeParameters[i] = new TypeParameter(this, manager, typeParameterName);
    }/*from w w w  .jav  a2  s . co m*/
    return typeParameters;
}

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

License:Open Source License

/**
 * Creates the handles and infos for the type parameter of the given binary member.
 * Adds new handles to the given vector.
 *//* w w w . java 2  s. c  o m*/
private void generateTypeParameterInfos(BinaryMember parent, char[] signature, HashMap newElements,
        ArrayList typeParameterHandles) {
    if (signature == null)
        return;
    char[][] typeParameterSignatures = Signature.getTypeParameters(signature);
    for (int i = 0, typeParameterCount = typeParameterSignatures.length; i < typeParameterCount; i++) {
        char[] typeParameterSignature = typeParameterSignatures[i];
        char[] typeParameterName = Signature.getTypeVariable(typeParameterSignature);
        CharOperation.replace(typeParameterSignature, '/', '.');
        char[][] typeParameterBoundSignatures = Signature.getTypeParameterBounds(typeParameterSignature);
        int boundLength = typeParameterBoundSignatures.length;
        char[][] typeParameterBounds = new char[boundLength][];
        for (int j = 0; j < boundLength; j++) {
            typeParameterBounds[j] = Signature.toCharArray(typeParameterBoundSignatures[j]);
        }
        TypeParameter typeParameter = new TypeParameter(parent, parent.manager, new String(typeParameterName));
        TypeParameterElementInfo info = new TypeParameterElementInfo();
        info.bounds = typeParameterBounds;
        info.boundsSignatures = typeParameterBoundSignatures;
        typeParameterHandles.add(typeParameter);

        // ensure that 2 binary methods with the same signature but with different return types have different occurence counts.
        // (case of bridge methods in 1.5)
        while (newElements.containsKey(typeParameter))
            typeParameter.occurrenceCount++;

        newElements.put(typeParameter, info);
    }
    //   throw new UnsupportedOperationException();
}

From source file:com.codenvy.ide.ext.java.server.SourcesFromBytecodeGenerator.java

License:Open Source License

/**
 * Appends labels for type parameters from a signature.
 *
 * @param typeParamSigs/*  w  ww .j  a  v a2  s .  co  m*/
 *         the type parameter signature
 */
private void appendTypeParameterSignaturesLabel(String[] typeParamSigs, StringBuilder builder) {
    if (typeParamSigs.length > 0) {
        builder.append(getLT());
        for (int i = 0; i < typeParamSigs.length; i++) {
            if (i > 0) {
                builder.append(JavaElementLabels.COMMA_STRING);
            }
            builder.append(Signature.getTypeVariable(typeParamSigs[i]));
        }
        builder.append(getGT());
    }
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends labels for type parameters from a signature.
 *
 * @param typeParamSigs the type parameter signature
 * @param flags flags with render options
 *//*from www .  ja  v  a 2 s .  c om*/
private void appendTypeParameterSignaturesLabel(String[] typeParamSigs, long flags) {
    if (typeParamSigs.length > 0) {
        fBuffer.append(getLT());
        for (int i = 0; i < typeParamSigs.length; i++) {
            if (i > 0) {
                fBuffer.append(COMMA_STRING);
            }
            fBuffer.append(Signature.getTypeVariable(typeParamSigs[i]));
        }
        fBuffer.append(getGT());
    }
}

From source file:org.eclipse.pde.api.tools.internal.util.Signatures.java

License:Open Source License

/**
 * Appends the given listing of type parameter names to the signature
 * contained in the given buffer/*from  w  ww  . java 2 s  .co m*/
 * 
 * @param buffer
 * @param parameters
 */
public static void appendTypeParameters(StringBuffer buffer, String[] parameters) {
    if (parameters == null) {
        return;
    }
    if (parameters.length == 0) {
        return;
    }
    buffer.append(getLT());
    for (int i = 0; i < parameters.length; i++) {
        if (i > 0) {
            buffer.append(getComma());
        }
        buffer.append(Signature.getTypeVariable(parameters[i]));
    }
    buffer.append(getGT());
}

From source file:org.eclipse.recommenders.completion.rcp.utils.ProposalUtils.java

License:Open Source License

private static char[] resolveTypeVariable(char[] typeVariableName, char[][] typeParameters) {
    for (char[] typeParameter : typeParameters) {
        if (CharOperation.equals(typeVariableName, Signature.getTypeVariable(typeParameter))) {
            char[][] typeParameterBounds = Signature.getTypeParameterBounds(typeParameter);
            if (typeParameterBounds.length > 0) {
                return typeParameterBounds[0];
            } else {
                return JAVA_LANG_OBJECT;
            }/*from ww  w.j a va  2s .com*/
        }
    }
    // No match found. Assume Object.
    return JAVA_LANG_OBJECT;
}

From source file:org.jboss.tools.cdi.core.CDIUtil.java

License:Open Source License

/**
 * Checks if the bean member has a type variable as a type.
 * If the bean member is a field then checks its type.
 * If the bean member is a parameter of a method then checks its type.
 * If the bean member is a method then checks its return type.
 * /*from w w  w. j av a2 s.  co  m*/
 * @param member
 * @param checkGenericMethod if true then checks if this member use a type variable which is declared in the generic method (in case of the member is a method).
 * @return
 */
public static boolean isTypeVariable(IBeanMember member, boolean checkGenericMethod) {
    try {
        String[] typeVariableSegnatures = member.getClassBean().getBeanClass().getTypeParameterSignatures();
        List<String> variables = new ArrayList<String>();
        for (String variableSig : typeVariableSegnatures) {
            variables.add(Signature.getTypeVariable(variableSig));
        }
        if (checkGenericMethod) {
            ITypeParameter[] typeParams = null;
            if (member instanceof IParameter) {
                typeParams = ((IParameter) member).getBeanMethod().getMethod().getTypeParameters();
            }
            if (member instanceof IBeanMethod) {
                typeParams = ((IBeanMethod) member).getMethod().getTypeParameters();
            }
            if (typeParams != null) {
                for (ITypeParameter param : typeParams) {
                    variables.add(param.getElementName());
                }
            }
        }
        String signature = null;
        if (member instanceof IBeanField) {
            signature = ((IBeanField) member).getField().getTypeSignature();
        } else if (member instanceof IParameter) {
            if (((IParameter) member).getType() == null) {
                return false;
            }
            signature = ((IParameter) member).getType().getSignature();
        } else if (member instanceof IBeanMethod) {
            signature = ((IBeanMethod) member).getMethod().getReturnType();
        }
        return isTypeVariable(variables, signature);
    } catch (JavaModelException e) {
        CDICorePlugin.getDefault().logError(e);
    }
    return false;
}

From source file:org.jboss.tools.cdi.internal.core.validation.CDICoreValidator.java

License:Open Source License

private void validateProducer(CDIValidationContext context, IProducer producer) {
    try {//  ww  w.  j  a  v  a  2s  . co  m
        Collection<ITypeDeclaration> typeDeclarations = producer.getAllTypeDeclarations();
        String[] typeVariables = producer.getBeanClass().getTypeParameterSignatures();
        ITypeDeclaration typeDeclaration = null;
        ITextSourceReference typeDeclarationReference = null;
        if (!typeDeclarations.isEmpty()) {
            /*
             * 3.3. Producer methods
             *  - producer method return type contains a wildcard type parameter
             * 
             * 2.2.1 Legal bean types
             *  - a parameterized type that contains a wildcard type parameter is not a legal bean type.
             * 
             * 3.4. Producer fields
             *  - producer field type contains a wildcard type parameter
             */
            typeDeclaration = typeDeclarations.iterator().next();

            typeDeclarationReference = CDIUtil.convertToJavaSourceReference(typeDeclaration,
                    producer.getSourceMember());

            String[] paramTypes = Signature.getTypeArguments(typeDeclaration.getSignature());
            boolean variable = false;
            for (String paramType : paramTypes) {
                if (Signature.getTypeSignatureKind(paramType) == Signature.WILDCARD_TYPE_SIGNATURE) {
                    if (producer instanceof IProducerField) {
                        addProblem(CDIValidationMessages.PRODUCER_FIELD_TYPE_HAS_WILDCARD,
                                CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                                typeDeclarationReference, producer.getResource());
                    } else {
                        addProblem(CDIValidationMessages.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD,
                                CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                                typeDeclarationReference, producer.getResource());
                    }
                } else if (!variable
                        && isTypeVariable(producer, Signature.toString(paramType), typeVariables)) {
                    /*
                     * 3.3. Producer methods
                     *  - producer method with a parameterized return type with a type variable declares any scope other than @Dependent
                     * 
                     * 3.4. Producer fields
                     *  - producer field with a parameterized type with a type variable declares any scope other than @Dependent
                     */
                    variable = true;
                    IAnnotationDeclaration scopeOrStereotypeDeclaration = CDIUtil
                            .getDifferentScopeDeclarationThanDepentend(producer);
                    if (scopeOrStereotypeDeclaration != null) {
                        boolean field = producer instanceof IProducerField;
                        addProblem(
                                field ? CDIValidationMessages.ILLEGAL_SCOPE_FOR_PRODUCER_FIELD
                                        : CDIValidationMessages.ILLEGAL_SCOPE_FOR_PRODUCER_METHOD,
                                field ? CDIPreferences.ILLEGAL_SCOPE_FOR_BEAN
                                        : CDIPreferences.ILLEGAL_SCOPE_FOR_BEAN,
                                scopeOrStereotypeDeclaration, producer.getResource());
                    }
                    break;
                }
            }
        }

        /*
         * 3.3.2. Declaring a producer method
         *  - producer method is annotated @Inject
         */
        IAnnotationDeclaration inject = producer.getAnnotation(CDIConstants.INJECT_ANNOTATION_TYPE_NAME);
        if (inject != null) {
            addProblem(CDIValidationMessages.PRODUCER_ANNOTATED_INJECT,
                    CDIPreferences.PRODUCER_ANNOTATED_INJECT, inject,
                    inject.getResource() != null ? inject.getResource() : producer.getResource(),
                    PRODUCER_ANNOTATED_INJECT_ID);
        }

        if (producer instanceof IProducerField) {
            /*
             * 3.5.1. Declaring a resource
             *  - producer field declaration specifies an EL name (together with one of @Resource, @PersistenceContext, @PersistenceUnit, @EJB, @WebServiceRef)
             */
            IProducerField producerField = (IProducerField) producer;
            if (producerField.getName() != null) {
                IAnnotationDeclaration declaration;
                for (String annotationType : RESOURCE_ANNOTATIONS) {
                    declaration = producerField.getAnnotation(annotationType);
                    if (declaration != null) {
                        IAnnotationDeclaration nameDeclaration = producerField
                                .getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
                        if (nameDeclaration != null) {
                            declaration = nameDeclaration;
                        }
                        addProblem(CDIValidationMessages.RESOURCE_PRODUCER_FIELD_SETS_EL_NAME,
                                CDIPreferences.RESOURCE_PRODUCER_FIELD_SETS_EL_NAME, declaration,
                                producer.getResource());
                    }
                }
            }
            /*
             * 3.4. Producer fields
             *  - producer field type is a type variable
             */
            if (typeVariables.length > 0) {
                String typeSign = producerField.getField().getTypeSignature();
                String typeString = Signature.toString(typeSign);
                for (String variableSig : typeVariables) {
                    String variableName = Signature.getTypeVariable(variableSig);
                    if (typeString.equals(variableName)) {
                        addProblem(CDIValidationMessages.PRODUCER_FIELD_TYPE_IS_VARIABLE,
                                CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                                typeDeclaration != null ? typeDeclarationReference : producer,
                                producer.getResource());
                    }
                }
            }
            /*
             * 3.4.2. Declaring a producer field
             *  - non-static field of a session bean class is annotated @Produces
             */
            if (producer.getClassBean() instanceof ISessionBean
                    && !Flags.isStatic(producerField.getField().getFlags())) {
                addProblem(CDIValidationMessages.ILLEGAL_PRODUCER_FIELD_IN_SESSION_BEAN,
                        CDIPreferences.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN,
                        producer.getProducesAnnotation(), producer.getResource(),
                        ILLEGAL_PRODUCER_FIELD_IN_SESSION_BEAN_ID);
            }
        } else {
            IProducerMethod producerMethod = (IProducerMethod) producer;
            List<IParameter> params = producerMethod.getParameters();
            Collection<ITextSourceReference> observesDeclarations = new ArrayList<ITextSourceReference>();
            Collection<ITextSourceReference> disposalDeclarations = new ArrayList<ITextSourceReference>();
            IAnnotationDeclaration producesDeclaration = producerMethod
                    .getAnnotation(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME);
            if (producesDeclaration != null) {
                observesDeclarations.add(producesDeclaration);
                disposalDeclarations.add(producesDeclaration);
            }
            for (IParameter param : params) {
                /*
                 * 3.3.6. Declaring a disposer method
                 *  - a disposer method is annotated @Produces.
                 * 
                 * 3.3.2. Declaring a producer method
                 *  - a has a parameter annotated @Disposes
                 */
                ITextSourceReference declaration = param
                        .getAnnotationPosition(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME);
                if (declaration != null) {
                    disposalDeclarations.add(declaration);
                }
                /*
                 * 3.3.2. Declaring a producer method
                 *  - a has a parameter annotated @Observers
                 * 
                 * 10.4.2. Declaring an observer method
                 *  - an observer method is annotated @Produces
                 */
                declaration = param.getAnnotationPosition(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME);
                if (declaration != null) {
                    observesDeclarations.add(declaration);
                }
            }
            if (observesDeclarations.size() > 1) {
                for (ITextSourceReference declaration : observesDeclarations) {
                    addProblem(CDIValidationMessages.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_OBSERVES,
                            CDIPreferences.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED, declaration,
                            producer.getResource(), PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_OBSERVES_ID);
                }
            }
            if (disposalDeclarations.size() > 1) {
                for (ITextSourceReference declaration : disposalDeclarations) {
                    addProblem(CDIValidationMessages.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_DISPOSES,
                            CDIPreferences.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED, declaration,
                            producer.getResource(), PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_DISPOSES_ID);
                }
            }

            /*
             * 3.3. Producer methods
             *  - producer method return type is a type variable
             * 
             * 2.2.1 - Legal bean types
             *  - a type variable is not a legal bean type
             */
            String typeSign = producerMethod.getMethod().getReturnType();
            String typeString = Signature.toString(typeSign);
            if (isTypeVariable(producerMethod, typeString, typeVariables)) {
                addProblem(CDIValidationMessages.PRODUCER_METHOD_RETURN_TYPE_IS_VARIABLE,
                        CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                        typeDeclaration != null ? typeDeclarationReference : producer, producer.getResource());
            }
            /*
             * 3.3.2. Declaring a producer method
             *  - non-static method of a session bean class is annotated @Produces, and the method is not a business method of the session bean
             */
            IClassBean classBean = producer.getClassBean();
            if (classBean instanceof ISessionBean) {
                IMethod method = CDIUtil.getBusinessMethodDeclaration((SessionBean) classBean, producerMethod);
                if (method == null) {
                    String bindedErrorMessage = NLS.bind(
                            CDIValidationMessages.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN,
                            new String[] { producerMethod.getMethod().getElementName(),
                                    producer.getBeanClass().getElementName() });
                    addProblem(bindedErrorMessage, CDIPreferences.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN,
                            producer.getProducesAnnotation(), producer.getResource(),
                            ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN_ID);
                    saveAllSuperTypesAsLinkedResources(classBean);
                } else if (!isAsYouTypeValidation() && method != producerMethod.getMethod() && method.exists()
                        && !method.isReadOnly()) {
                    getValidationContext().addLinkedCoreResource(SHORT_ID,
                            classBean.getSourcePath().toOSString(), method.getResource().getFullPath(), false);
                }
            }

            IAnnotationDeclaration sDeclaration = producerMethod.getSpecializesAnnotationDeclaration();
            if (sDeclaration != null) {
                if (Flags.isStatic(producerMethod.getMethod().getFlags())) {
                    /*
                     * 3.3.3. Specializing a producer method
                     *  - method annotated @Specializes is static
                     */
                    addProblem(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_STATIC,
                            CDIPreferences.ILLEGAL_SPECIALIZING_BEAN, sDeclaration, producer.getResource());
                } else {
                    /*
                     * 3.3.3. Specializing a producer method
                     *  - method annotated @Specializes does not directly override another producer method
                     */
                    IMethod superMethod = CDIUtil.getDirectOverridingMethodDeclaration(producerMethod);
                    boolean overrides = false;
                    if (superMethod != null) {
                        IType superType = superMethod.getDeclaringType();
                        if (superType.isBinary()) {
                            IAnnotation[] ants = superMethod.getAnnotations();
                            for (IAnnotation an : ants) {
                                if (CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME.equals(an.getElementName())) {
                                    overrides = true;
                                }
                            }
                        } else {
                            Collection<IBean> beans = context.getCdiProject()
                                    .getBeans(superType.getResource().getFullPath());
                            for (IBean iBean : beans) {
                                if (iBean instanceof IProducerMethod) {
                                    IProducerMethod prMethod = (IProducerMethod) iBean;
                                    if (prMethod.getMethod().isSimilar(superMethod)) {
                                        overrides = true;
                                    }
                                }
                            }
                        }
                    }
                    if (!overrides) {
                        addProblem(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_OVERRIDE,
                                CDIPreferences.ILLEGAL_SPECIALIZING_BEAN, sDeclaration, producer.getResource());
                    }
                    saveAllSuperTypesAsLinkedResources(producer.getClassBean());
                }
            }
        }
    } catch (JavaModelException e) {
        CDICorePlugin.getDefault().logError(e);
    }
}

From source file:org.jboss.tools.cdi.internal.core.validation.CDICoreValidator.java

License:Open Source License

private boolean isTypeVariable(IProducer producer, String type, String[] typeVariables)
        throws JavaModelException {
    if (producer instanceof IProducerMethod) {
        ITypeParameter[] paramTypes = ((IProducerMethod) producer).getMethod().getTypeParameters();
        for (ITypeParameter param : paramTypes) {
            String variableName = param.getElementName();
            if (variableName.equals(type)) {
                return true;
            }/*w w  w . j a v  a 2  s  . c  o  m*/
        }
    }
    if (typeVariables.length > 0) {
        for (String variableSig : typeVariables) {
            String variableName = Signature.getTypeVariable(variableSig);
            if (type.equals(variableName)) {
                return true;
            }
        }
    }
    return false;
}