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

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

Introduction

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

Prototype

public static int getArrayCount(String typeSignature) throws IllegalArgumentException 

Source Link

Document

Returns the array count (array nesting depth) of the given type signature.

Usage

From source file:at.bestsolution.efxclipse.tooling.model.internal.utils.PropertiesUtil.java

License:Open Source License

public static Map<String, IFXProperty> resolveProperties(FXClass fxClass) throws JavaModelException {
    Map<String, IFXProperty> rv = new HashMap<String, IFXProperty>();

    if ("java.lang.Object".equals(fxClass.getFQN())) {
        return rv;
    }//from   ww w.  ja va2s .com

    Map<String, IMethod> beanProperties = new HashMap<String, IMethod>();
    Map<String, IMethod> builderProperties = new HashMap<String, IMethod>();

    String builder = fxClass.getType().getFullyQualifiedName() + "Builder";
    IType builderType = fxClass.getJavaProject().findType(builder);
    if (builderType != null) {
        for (IMethod m : builderType.getMethods()) {
            // Only public and none static methods
            if (!Flags.isPublic(m.getFlags()) || Flags.isStatic(m.getFlags())) {
                continue;
            }

            String name = m.getElementName();

            // omit the build method
            if ("build".equals(name) || "applyTo".equals(name)) {
                continue;
            }

            if (m.getParameterNames().length == 1) {
                String param = m.getParameterTypes()[0];
                if (Signature.getArrayCount(param) == 0) {
                    builderProperties.put(name, m);
                }
            }
        }
    }

    for (IMethod m : fxClass.getType().getMethods()) {
        // Only public and none static methods
        if (!Flags.isPublic(m.getFlags()) || Flags.isStatic(m.getFlags())) {
            continue;
        }

        String name = m.getElementName();

        // do not use impl methods they are private
        if (name.startsWith("getImpl") || name.startsWith("isImpl") || name.startsWith("setImpl")
                || name.contains("Unmodifiable")) {
            continue;
        }

        if (name.startsWith("get") || name.startsWith("is")) {
            name = name.startsWith("get") ? name.substring(3) : name.substring(2);
            name = Character.toLowerCase(name.charAt(0)) + name.substring(1);

            // Only set if there's not already one stored
            if (!beanProperties.containsKey(name)) {
                beanProperties.put(name, m);
            }
        } else if (m.getElementName().startsWith("set") && m.getParameters().length == 1) {
            name = name.substring(3);
            name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
            beanProperties.put(name, m);
        }
    }

    for (Entry<String, IMethod> e : beanProperties.entrySet()) {
        FXProperty p = getProperty(fxClass, e.getKey(), e.getValue());
        if (p != null) {
            rv.put(e.getKey(), p);
        }
    }

    for (Entry<String, IMethod> e : builderProperties.entrySet()) {
        IFXProperty p = rv.get(e.getKey());
        if (p == null) {
            continue;
        }

        if (!(p instanceof IFXCollectionProperty)) {
            if (!p.isSetable()) {
                p = getProperty(fxClass, e.getKey(), e.getValue());
                if (p != null) {
                    rv.put(e.getKey(), p);
                }
            }
        }
    }

    return rv;
}

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

License:Open Source License

private StringBuffer internalGetSubstitutedTypeName(String typeSig, IMember context, boolean erasure,
        StringBuffer buf) throws JavaModelException {
    int sigKind = Signature.getTypeSignatureKind(typeSig);
    switch (sigKind) {
    case Signature.BASE_TYPE_SIGNATURE:
        return buf.append(Signature.toString(typeSig));
    case Signature.ARRAY_TYPE_SIGNATURE:
        internalGetSubstitutedTypeName(Signature.getElementType(typeSig), context, erasure, buf);
        for (int i = Signature.getArrayCount(typeSig); i > 0; i--) {
            buf.append('[').append(']');
        }/*ww w  . j a  va2s  .  co  m*/
        return buf;
    case Signature.CLASS_TYPE_SIGNATURE: {
        String erasureSig = Signature.getTypeErasure(typeSig);
        String erasureName = Signature.getSimpleName(Signature.toString(erasureSig));

        char ch = erasureSig.charAt(0);
        if (ch == Signature.C_RESOLVED) {
            buf.append(erasureName);
        } else if (ch == Signature.C_UNRESOLVED) { // could be a type variable
            if (erasure) {
                buf.append(getVariableErasure(context, erasureName));
            } else {
                buf.append(getVariableSubstitution(context, erasureName));
            }
        } else {
            Assert.isTrue(false, "Unknown class type signature"); //$NON-NLS-1$
        }
        if (!erasure) {
            String[] typeArguments = Signature.getTypeArguments(typeSig);
            if (typeArguments.length > 0) {
                buf.append('<');
                for (int i = 0; i < typeArguments.length; i++) {
                    if (i > 0) {
                        buf.append(',');
                    }
                    internalGetSubstitutedTypeName(typeArguments[i], context, erasure, buf);
                }
                buf.append('>');
            }
        }
        return buf;
    }
    case Signature.TYPE_VARIABLE_SIGNATURE:
        String varName = Signature.toString(typeSig);
        if (erasure) {
            return buf.append(getVariableErasure(context, varName));
        } else {
            return buf.append(getVariableSubstitution(context, varName));
        }
    case Signature.WILDCARD_TYPE_SIGNATURE: {
        buf.append('?');
        char ch = typeSig.charAt(0);
        if (ch == Signature.C_STAR) {
            return buf;
        } else if (ch == Signature.C_EXTENDS) {
            buf.append(" extends "); //$NON-NLS-1$
        } else {
            buf.append(" super "); //$NON-NLS-1$
        }
        return internalGetSubstitutedTypeName(typeSig.substring(1), context, erasure, buf);
    }
    case Signature.CAPTURE_TYPE_SIGNATURE:
        return internalGetSubstitutedTypeName(typeSig.substring(1), context, erasure, buf);
    default:
        Assert.isTrue(false, "Unhandled type signature kind"); //$NON-NLS-1$
        return buf;
    }
}

From source file:at.bestsolution.fxide.jdt.editor.internal.SignatureUtil.java

License:Open Source License

/**
 * Returns the qualified signature corresponding to
 * <code>signature</code>./*w  w  w  .  j  a va 2  s.com*/
 *
 * @param signature the signature to qualify
 * @param context the type inside which an unqualified type will be
 *        resolved to find the qualifier, or <code>null</code> if no
 *        context is available
 * @return the qualified signature
 */
public static String qualifySignature(final String signature, final IType context) {
    if (context == null)
        return signature;

    String qualifier = Signature.getSignatureQualifier(signature);
    if (qualifier.length() > 0)
        return signature;

    String elementType = Signature.getElementType(signature);
    String erasure = Signature.getTypeErasure(elementType);
    String simpleName = Signature.getSignatureSimpleName(erasure);
    String genericSimpleName = Signature.getSignatureSimpleName(elementType);

    int dim = Signature.getArrayCount(signature);

    try {
        String[][] strings = context.resolveType(simpleName);
        if (strings != null && strings.length > 0)
            qualifier = strings[0][0];
    } catch (JavaModelException e) {
        // ignore - not found
    }

    if (qualifier.length() == 0)
        return signature;

    String qualifiedType = Signature.toQualifiedName(new String[] { qualifier, genericSimpleName });
    String qualifiedSignature = Signature.createTypeSignature(qualifiedType, true);
    String newSignature = Signature.createArraySignature(qualifiedSignature, dim);

    return newSignature;
}

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

License:Open Source License

/**
 * Appends the label for a method. Considers the M_* flags.
 *
 * @param method the element to render/* w w  w . ja v  a2 s .  c om*/
 * @param flags the rendering flags. Flags with names starting with 'M_' are considered.
 */
public void appendMethodLabel(IMethod method, long flags) {
    try {
        BindingKey resolvedKey = getFlag(flags, JavaElementLabels.USE_RESOLVED) && method.isResolved()
                ? new BindingKey(method.getKey())
                : null;
        String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;

        // type parameters
        if (getFlag(flags, JavaElementLabels.M_PRE_TYPE_PARAMETERS)) {
            if (resolvedKey != null) {
                if (resolvedKey.isParameterizedMethod()) {
                    String[] typeArgRefs = resolvedKey.getTypeArguments();
                    if (typeArgRefs.length > 0) {
                        appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
                        fBuffer.append(' ');
                    }
                } else {
                    String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                    if (typeParameterSigs.length > 0) {
                        appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
                        fBuffer.append(' ');
                    }
                }
            } else if (method.exists()) {
                ITypeParameter[] typeParameters = method.getTypeParameters();
                if (typeParameters.length > 0) {
                    appendTypeParametersLabels(typeParameters, flags);
                    fBuffer.append(' ');
                }
            }
        }

        // return type
        if (getFlag(flags, JavaElementLabels.M_PRE_RETURNTYPE) && method.exists() && !method.isConstructor()) {
            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig)
                    : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, flags);
            fBuffer.append(' ');
        }

        // qualification
        if (getFlag(flags, JavaElementLabels.M_FULLY_QUALIFIED)) {
            appendTypeLabel(method.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }

        fBuffer.append(getElementName(method));

        // constructor type arguments
        if (getFlag(flags, JavaElementLabels.T_TYPE_PARAMETERS) && method.exists() && method.isConstructor()) {
            if (resolvedSig != null && resolvedKey.isParameterizedType()) {
                BindingKey declaringType = resolvedKey.getDeclaringType();
                if (declaringType != null) {
                    String[] declaringTypeArguments = declaringType.getTypeArguments();
                    appendTypeArgumentSignaturesLabel(method, declaringTypeArguments, flags);
                }
            }
        }

        // parameters
        fBuffer.append('(');
        String[] declaredParameterTypes = method.getParameterTypes();
        if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES)) {
            String[] types = null;
            int nParams = 0;
            boolean renderVarargs = false;
            boolean isPolymorphic = false;
            if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES)) {
                if (resolvedSig != null) {
                    types = Signature.getParameterTypes(resolvedSig);
                } else {
                    types = declaredParameterTypes;
                }
                nParams = types.length;
                renderVarargs = method.exists() && Flags.isVarargs(method.getFlags());
                if (renderVarargs && resolvedSig != null && declaredParameterTypes.length == 1
                        && JavaModelUtil.isPolymorphicSignature(method)) {
                    renderVarargs = false;
                    isPolymorphic = true;
                }
            }
            String[] names = null;
            if (getFlag(flags, JavaElementLabels.M_PARAMETER_NAMES) && method.exists()) {
                names = method.getParameterNames();
                if (isPolymorphic) {
                    // handled specially below
                } else if (types == null) {
                    nParams = names.length;
                } else { // types != null
                    if (nParams != names.length) {
                        if (resolvedSig != null && types.length > names.length) {
                            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=99137
                            nParams = names.length;
                            String[] typesWithoutSyntheticParams = new String[nParams];
                            System.arraycopy(types, types.length - nParams, typesWithoutSyntheticParams, 0,
                                    nParams);
                            types = typesWithoutSyntheticParams;
                        } else {
                            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=101029
                            // JavaPlugin.logErrorMessage("JavaElementLabels: Number of param types(" + nParams + ") != number of names(" + names.length + "): " + method.getElementName());   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                            names = null; // no names rendered
                        }
                    }
                }
            }

            ILocalVariable[] annotatedParameters = null;
            if (nParams > 0 && getFlag(flags, JavaElementLabels.M_PARAMETER_ANNOTATIONS)) {
                annotatedParameters = method.getParameters();
            }

            for (int i = 0; i < nParams; i++) {
                if (i > 0) {
                    fBuffer.append(JavaElementLabels.COMMA_STRING);
                }
                if (annotatedParameters != null && i < annotatedParameters.length) {
                    appendAnnotationLabels(annotatedParameters[i].getAnnotations(), flags);
                }

                if (types != null) {
                    String paramSig = types[i];
                    if (renderVarargs && (i == nParams - 1)) {
                        int newDim = Signature.getArrayCount(paramSig) - 1;
                        appendTypeSignatureLabel(method, Signature.getElementType(paramSig), flags);
                        for (int k = 0; k < newDim; k++) {
                            fBuffer.append('[').append(']');
                        }
                        fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
                    } else {
                        appendTypeSignatureLabel(method, paramSig, flags);
                    }
                }
                if (names != null) {
                    if (types != null) {
                        fBuffer.append(' ');
                    }
                    if (isPolymorphic) {
                        fBuffer.append(names[0] + i);
                    } else {
                        fBuffer.append(names[i]);
                    }
                }
            }
        } else {
            if (declaredParameterTypes.length > 0) {
                fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
            }
        }
        fBuffer.append(')');

        if (getFlag(flags, JavaElementLabels.M_EXCEPTIONS)) {
            String[] types;
            if (resolvedKey != null) {
                types = resolvedKey.getThrownExceptions();
            } else {
                types = method.exists() ? method.getExceptionTypes() : new String[0];
            }
            if (types.length > 0) {
                fBuffer.append(" throws "); //$NON-NLS-1$
                for (int i = 0; i < types.length; i++) {
                    if (i > 0) {
                        fBuffer.append(JavaElementLabels.COMMA_STRING);
                    }
                    appendTypeSignatureLabel(method, types[i], flags);
                }
            }
        }

        if (getFlag(flags, JavaElementLabels.M_APP_TYPE_PARAMETERS)) {
            int offset = fBuffer.length();
            if (resolvedKey != null) {
                if (resolvedKey.isParameterizedMethod()) {
                    String[] typeArgRefs = resolvedKey.getTypeArguments();
                    if (typeArgRefs.length > 0) {
                        fBuffer.append(' ');
                        appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
                    }
                } else {
                    String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                    if (typeParameterSigs.length > 0) {
                        fBuffer.append(' ');
                        appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
                    }
                }
            } else if (method.exists()) {
                ITypeParameter[] typeParameters = method.getTypeParameters();
                if (typeParameters.length > 0) {
                    fBuffer.append(' ');
                    appendTypeParametersLabels(typeParameters, flags);
                }
            }
            //            if (getFlag(flags, JavaElementLabels.COLORIZE) && offset != fBuffer.length()) {
            //               fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
            //            }
        }

        if (getFlag(flags, JavaElementLabels.M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.DECL_STRING);
            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig)
                    : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, flags);
            //            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            //               fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
            //            }
        }

        // category
        if (getFlag(flags, JavaElementLabels.M_CATEGORY) && method.exists())
            appendCategoryLabel(method, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.M_POST_QUALIFIED)) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            appendTypeLabel(method.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            //            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            //               fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            //            }
        }

    } catch (JavaModelException e) {
        //TODO
        e.printStackTrace();
    }
}

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

License:Open Source License

protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags) {
    int sigKind = Signature.getTypeSignatureKind(typeSig);
    switch (sigKind) {
    case Signature.BASE_TYPE_SIGNATURE:
        fBuffer.append(Signature.toString(typeSig));
        break;//ww  w  . j  a va 2  s.c  o m
    case Signature.ARRAY_TYPE_SIGNATURE:
        appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags);
        for (int dim = Signature.getArrayCount(typeSig); dim > 0; dim--) {
            fBuffer.append('[').append(']');
        }
        break;
    case Signature.CLASS_TYPE_SIGNATURE:
        String baseType = getSimpleTypeName(enclosingElement, typeSig);
        fBuffer.append(baseType);

        String[] typeArguments = Signature.getTypeArguments(typeSig);
        appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags);
        break;
    case Signature.TYPE_VARIABLE_SIGNATURE:
        fBuffer.append(getSimpleTypeName(enclosingElement, typeSig));
        break;
    case Signature.WILDCARD_TYPE_SIGNATURE:
        char ch = typeSig.charAt(0);
        if (ch == Signature.C_STAR) { //workaround for bug 85713
            fBuffer.append('?');
        } else {
            if (ch == Signature.C_EXTENDS) {
                fBuffer.append("? extends "); //$NON-NLS-1$
                appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
            } else if (ch == Signature.C_SUPER) {
                fBuffer.append("? super "); //$NON-NLS-1$
                appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
            }
        }
        break;
    case Signature.CAPTURE_TYPE_SIGNATURE:
        appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
        break;
    case Signature.INTERSECTION_TYPE_SIGNATURE:
        String[] typeBounds = Signature.getIntersectionTypeBounds(typeSig);
        appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags);
        break;
    default:
        // unknown
    }
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavaDocLocations.java

License:Open Source License

private static void appendMethodReference(IMethod meth, StringBuffer buf) throws JavaModelException {
    buf.append(meth.getElementName());/*w  ww  .ja va2s. c om*/

    /*
     * The Javadoc tool for Java SE 8 changed the anchor syntax and now tries to avoid "strange" characters in URLs.
     * This breaks all clients that directly create such URLs.
     * We can't know what format is required, so we just guess by the project's compiler compliance.
     */
    boolean is18OrHigher = JavaModelUtil.is18OrHigher(meth.getJavaProject());
    buf.append(is18OrHigher ? '-' : '(');
    String[] params = meth.getParameterTypes();
    IType declaringType = meth.getDeclaringType();
    boolean isVararg = Flags.isVarargs(meth.getFlags());
    int lastParam = params.length - 1;
    for (int i = 0; i <= lastParam; i++) {
        if (i != 0) {
            buf.append(is18OrHigher ? "-" : ", "); //$NON-NLS-1$ //$NON-NLS-2$
        }
        String curr = Signature.getTypeErasure(params[i]);
        String fullName = JavaModelUtil.getResolvedTypeName(curr, declaringType);
        if (fullName == null) { // e.g. a type parameter "QE;"
            fullName = Signature.toString(Signature.getElementType(curr));
        }
        if (fullName != null) {
            buf.append(fullName);
            int dim = Signature.getArrayCount(curr);
            if (i == lastParam && isVararg) {
                dim--;
            }
            while (dim > 0) {
                buf.append(is18OrHigher ? ":A" : "[]"); //$NON-NLS-1$ //$NON-NLS-2$
                dim--;
            }
            if (i == lastParam && isVararg) {
                buf.append("..."); //$NON-NLS-1$
            }
        }
    }
    buf.append(is18OrHigher ? '-' : ')');
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a method. Considers the M_* flags.
 *
 * @param method the element to render/*ww w  . ja  v  a 2 s.  c o m*/
 * @param flags the rendering flags. Flags with names starting with 'M_' are considered.
 */
public void appendMethodLabel(IMethod method, long flags) {
    try {
        BindingKey resolvedKey = getFlag(flags, JavaElementLabels.USE_RESOLVED) && method.isResolved()
                ? new BindingKey(method.getKey())
                : null;
        String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;

        // type parameters
        if (getFlag(flags, JavaElementLabels.M_PRE_TYPE_PARAMETERS)) {
            if (resolvedKey != null) {
                if (resolvedKey.isParameterizedMethod()) {
                    String[] typeArgRefs = resolvedKey.getTypeArguments();
                    if (typeArgRefs.length > 0) {
                        appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
                        fBuffer.append(' ');
                    }
                } else {
                    String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                    if (typeParameterSigs.length > 0) {
                        appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
                        fBuffer.append(' ');
                    }
                }
            } else if (method.exists()) {
                ITypeParameter[] typeParameters = method.getTypeParameters();
                if (typeParameters.length > 0) {
                    appendTypeParametersLabels(typeParameters, flags);
                    fBuffer.append(' ');
                }
            }
        }

        // return type
        if (getFlag(flags, JavaElementLabels.M_PRE_RETURNTYPE) && method.exists() && !method.isConstructor()) {
            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig)
                    : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, flags);
            fBuffer.append(' ');
        }

        // qualification
        if (getFlag(flags, JavaElementLabels.M_FULLY_QUALIFIED)) {
            appendTypeLabel(method.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }

        fBuffer.append(getElementName(method));

        // constructor type arguments
        if (getFlag(flags, JavaElementLabels.T_TYPE_PARAMETERS) && method.exists() && method.isConstructor()) {
            if (resolvedSig != null && resolvedKey.isParameterizedType()) {
                BindingKey declaringType = resolvedKey.getDeclaringType();
                if (declaringType != null) {
                    String[] declaringTypeArguments = declaringType.getTypeArguments();
                    appendTypeArgumentSignaturesLabel(method, declaringTypeArguments, flags);
                }
            }
        }

        // parameters
        fBuffer.append('(');
        String[] declaredParameterTypes = method.getParameterTypes();
        if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES)) {
            String[] types = null;
            int nParams = 0;
            boolean renderVarargs = false;
            boolean isPolymorphic = false;
            if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES)) {
                if (resolvedSig != null) {
                    types = Signature.getParameterTypes(resolvedSig);
                } else {
                    types = declaredParameterTypes;
                }
                nParams = types.length;
                renderVarargs = method.exists() && Flags.isVarargs(method.getFlags());
                if (renderVarargs && resolvedSig != null && declaredParameterTypes.length == 1
                        && JavaModelUtil.isPolymorphicSignature(method)) {
                    renderVarargs = false;
                    isPolymorphic = true;
                }
            }
            String[] names = null;
            if (getFlag(flags, JavaElementLabels.M_PARAMETER_NAMES) && method.exists()) {
                names = method.getParameterNames();
                if (isPolymorphic) {
                    // handled specially below
                } else if (types == null) {
                    nParams = names.length;
                } else { // types != null
                    if (nParams != names.length) {
                        if (resolvedSig != null && types.length > names.length) {
                            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=99137
                            nParams = names.length;
                            String[] typesWithoutSyntheticParams = new String[nParams];
                            System.arraycopy(types, types.length - nParams, typesWithoutSyntheticParams, 0,
                                    nParams);
                            types = typesWithoutSyntheticParams;
                        } else {
                            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=101029
                            // JavaPlugin.logErrorMessage("JavaElementLabels: Number of param types(" + nParams + ") != number of names(" + names.length + "): " + method.getElementName());   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                            names = null; // no names rendered
                        }
                    }
                }
            }

            ILocalVariable[] annotatedParameters = null;
            if (nParams > 0 && getFlag(flags, JavaElementLabels.M_PARAMETER_ANNOTATIONS)) {
                annotatedParameters = method.getParameters();
            }

            for (int i = 0; i < nParams; i++) {
                if (i > 0) {
                    fBuffer.append(JavaElementLabels.COMMA_STRING);
                }
                if (annotatedParameters != null && i < annotatedParameters.length) {
                    appendAnnotationLabels(annotatedParameters[i].getAnnotations(), flags);
                }

                if (types != null) {
                    String paramSig = types[i];
                    if (renderVarargs && (i == nParams - 1)) {
                        int newDim = Signature.getArrayCount(paramSig) - 1;
                        appendTypeSignatureLabel(method, Signature.getElementType(paramSig), flags);
                        for (int k = 0; k < newDim; k++) {
                            fBuffer.append('[').append(']');
                        }
                        fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
                    } else {
                        appendTypeSignatureLabel(method, paramSig, flags);
                    }
                }
                if (names != null) {
                    if (types != null) {
                        fBuffer.append(' ');
                    }
                    if (isPolymorphic) {
                        fBuffer.append(names[0] + i);
                    } else {
                        fBuffer.append(names[i]);
                    }
                }
            }
        } else {
            if (declaredParameterTypes.length > 0) {
                fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
            }
        }
        fBuffer.append(')');

        if (getFlag(flags, JavaElementLabels.M_EXCEPTIONS)) {
            String[] types;
            if (resolvedKey != null) {
                types = resolvedKey.getThrownExceptions();
            } else {
                types = method.exists() ? method.getExceptionTypes() : new String[0];
            }
            if (types.length > 0) {
                fBuffer.append(" throws "); //$NON-NLS-1$
                for (int i = 0; i < types.length; i++) {
                    if (i > 0) {
                        fBuffer.append(JavaElementLabels.COMMA_STRING);
                    }
                    appendTypeSignatureLabel(method, types[i], flags);
                }
            }
        }

        if (getFlag(flags, JavaElementLabels.M_APP_TYPE_PARAMETERS)) {
            int offset = fBuffer.length();
            if (resolvedKey != null) {
                if (resolvedKey.isParameterizedMethod()) {
                    String[] typeArgRefs = resolvedKey.getTypeArguments();
                    if (typeArgRefs.length > 0) {
                        fBuffer.append(' ');
                        appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
                    }
                } else {
                    String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                    if (typeParameterSigs.length > 0) {
                        fBuffer.append(' ');
                        appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
                    }
                }
            } else if (method.exists()) {
                ITypeParameter[] typeParameters = method.getTypeParameters();
                if (typeParameters.length > 0) {
                    fBuffer.append(' ');
                    appendTypeParametersLabels(typeParameters, flags);
                }
            }
            if (getFlag(flags, JavaElementLabels.COLORIZE) && offset != fBuffer.length()) {
                //               fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
            }
        }

        if (getFlag(flags, JavaElementLabels.M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.DECL_STRING);
            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig)
                    : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, flags);
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                //               fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
            }
        }

        // category
        if (getFlag(flags, JavaElementLabels.M_CATEGORY) && method.exists())
            appendCategoryLabel(method, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.M_POST_QUALIFIED)) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            appendTypeLabel(method.getDeclaringType(),
                    JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                //               fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            }
        }

    } catch (JavaModelException e) {
        LOG.error(e.getMessage(), e); // NotExistsException will not reach this point
    }
}

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

License:Open Source License

protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags,
        StringBuilder builder) {//from   w w  w .  j a v  a 2s . com
    int sigKind = Signature.getTypeSignatureKind(typeSig);
    switch (sigKind) {
    case Signature.BASE_TYPE_SIGNATURE:
        builder.append(Signature.toString(typeSig));
        break;
    case Signature.ARRAY_TYPE_SIGNATURE:
        appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags, builder);
        for (int dim = Signature.getArrayCount(typeSig); dim > 0; dim--) {
            builder.append('[').append(']');
        }
        break;
    case Signature.CLASS_TYPE_SIGNATURE:
        String baseType = getSimpleTypeName(enclosingElement, typeSig);
        builder.append(baseType);

        String[] typeArguments = Signature.getTypeArguments(typeSig);
        appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags, builder);
        break;
    case Signature.TYPE_VARIABLE_SIGNATURE:
        builder.append(getSimpleTypeName(enclosingElement, typeSig));
        break;
    case Signature.WILDCARD_TYPE_SIGNATURE:
        char ch = typeSig.charAt(0);
        if (ch == Signature.C_STAR) { //workaround for bug 85713
            builder.append('?');
        } else {
            if (ch == Signature.C_EXTENDS) {
                builder.append("? extends "); //$NON-NLS-1$
                appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags, builder);
            } else if (ch == Signature.C_SUPER) {
                builder.append("? super "); //$NON-NLS-1$
                appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags, builder);
            }
        }
        break;
    case Signature.CAPTURE_TYPE_SIGNATURE:
        appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags, builder);
        break;
    case Signature.INTERSECTION_TYPE_SIGNATURE:
        String[] typeBounds = Signature.getIntersectionTypeBounds(typeSig);
        appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags, builder);
        break;
    default:
        // unknown
    }
}

From source file:com.drgarbage.core.ActionUtils.java

License:Apache License

/**
 * Resolves a type name in the context of the declaring type.
 * /*from w ww  . j  av a 2s.c o m*/
 * @param refTypeSig the type name in signature notation (for example 'QVector') this can also be an array type, but dimensions will be ignored.
 * @param declaringType the context for resolving (type where the reference was made in)
 * @return returns the fully qualified type name or build-in-type name. if a unresolved type couldn't be resolved null is returned
 */
public static String getResolvedTypeName(String refTypeSig, IType declaringType)
        throws JavaModelException, IllegalArgumentException {
    int arrayCount = Signature.getArrayCount(refTypeSig);

    /*
     * use the last element for resolving the type
     * For example: QString [QString or [[QString
     * The last element is always 'Q'
     */
    char type = refTypeSig.charAt(arrayCount);
    if (type == Signature.C_UNRESOLVED) {
        String name = ""; //$NON-NLS-1$
        int bracket = refTypeSig.indexOf(Signature.C_GENERIC_START, arrayCount + 1);
        if (bracket > 0)
            name = refTypeSig.substring(arrayCount + 1, bracket);
        else {
            int semi = refTypeSig.indexOf(Signature.C_SEMICOLON, arrayCount + 1);
            if (semi == -1) {
                throw new IllegalArgumentException();
            }
            name = refTypeSig.substring(arrayCount + 1, semi);
        }
        String[][] resolvedNames = declaringType.resolveType(name);
        if (resolvedNames != null && resolvedNames.length > 0) {
            StringBuffer res = new StringBuffer();
            for (int i = 0; i < arrayCount; i++) {
                res.append('[');
            }
            res.append(concatenateName(resolvedNames[0][0], resolvedNames[0][1]));
            return res.toString();
        }
        throw new IllegalArgumentException();
    } else {
        return refTypeSig;
    }
}

From source file:com.google.gdt.eclipse.core.java.JavaModelSearch.java

License:Open Source License

private static boolean methodSignaturesEqual(IType type2, String methodName, String[] paramTypes,
        boolean isConstructor, IMethod method2) {
    try {/*from   ww  w  .  ja v a2 s  .  com*/
        // Method names must match, unless we're comparing constructors
        if (!isConstructor && !method2.getElementName().equals(methodName)) {
            return false;
        }

        // Only compare ctors to ctors and methods to methods
        if (isConstructor != method2.isConstructor()) {
            return false;
        }

        // Parameter count must match
        String signature2 = method2.getSignature();
        String[] paramTypes2 = Signature.getParameterTypes(signature2);
        if (paramTypes.length != paramTypes2.length) {
            return false;
        }

        // Compare each parameter type
        for (int i = 0; i < paramTypes.length; i++) {
            String paramType = paramTypes[i];
            String paramType2 = paramTypes2[i];

            // Compare array nesting depth ([] = 1, [][] = 2, etc.)
            if (Signature.getArrayCount(paramType) != Signature.getArrayCount(paramType2)) {
                return false;
            }

            // Remove any array nesting and generic type parameters
            paramType = getBaseType(paramType);
            paramType2 = getBaseType(paramType2);

            // Extract the full type names from the signatures
            String paramTypeName = getQualifiedTypeName(paramType);
            String paramTypeName2 = getQualifiedTypeName(paramType2);

            if (isTypeParameter(method2, paramTypeName2)) {
                // Skip parameters whose type is a generic type parameter of the
                // method we're comparing against, or the method's containing class
                continue;

                /*
                 * TODO: we're currently not checking the bounds of generic type
                 * parameters, so sometimes we may return true here even when the
                 * caller's method signature doesn't match the method we're comparing
                 * against. We could try to add that logic here, or better still, we
                 * could integrate TypeOracle and take advantage of its type searching
                 * capabilities.
                 */
            }

            // If we run into an unresolved parameter type in the method we're
            // searching, we'll need to resolve that before doing the comparison
            if (paramType2.charAt(0) == Signature.C_UNRESOLVED) {
                paramTypeName2 = resolveTypeName(type2, paramTypeName2);
            }

            // Finally, compare the type names
            if (!paramTypeName.equals(paramTypeName2)) {
                return false;
            }
        }

        // We passed all the checks, so the signatures are equal
        return true;

    } catch (JavaModelException e) {
        CorePluginLog.logError(e, "Error comparing method signatures of {0} and {1}", methodName,
                method2.getElementName());
        return false;
    }
}