Example usage for org.eclipse.jdt.core Flags isVarargs

List of usage examples for org.eclipse.jdt.core Flags isVarargs

Introduction

In this page you can find the example usage for org.eclipse.jdt.core Flags isVarargs.

Prototype

public static boolean isVarargs(int flags) 

Source Link

Document

Returns whether the given integer has the AccVarargs bit set.

Usage

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

License:Open Source License

public static boolean isVarargs(IMethod method) throws JavaModelException {
    return Flags.isVarargs(method.getFlags());
}

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/*from   ww  w .  j  a va 2 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:com.codenvy.ide.ext.java.server.internal.core.BinaryMethod.java

License:Open Source License

protected void toStringName(StringBuffer buffer, int flags) {
    buffer.append(getElementName());/*from  ww  w .j ava2 s . c om*/
    buffer.append('(');
    String[] parameters = getParameterTypes();
    int length;
    if (parameters != null && (length = parameters.length) > 0) {
        boolean isVarargs = Flags.isVarargs(flags);
        for (int i = 0; i < length; i++) {
            try {
                if (i < length - 1) {
                    buffer.append(Signature.toString(parameters[i]));
                    buffer.append(", "); //$NON-NLS-1$
                } else if (isVarargs) {
                    // remove array from signature
                    String parameter = parameters[i].substring(1);
                    buffer.append(Signature.toString(parameter));
                    buffer.append(" ..."); //$NON-NLS-1$
                } else {
                    buffer.append(Signature.toString(parameters[i]));
                }
            } catch (IllegalArgumentException e) {
                // parameter signature is malformed
                buffer.append("*** invalid signature: "); //$NON-NLS-1$
                buffer.append(parameters[i]);
            }
        }
    }
    buffer.append(')');
    if (this.occurrenceCount > 1) {
        buffer.append("#"); //$NON-NLS-1$
        buffer.append(this.occurrenceCount);
    }
}

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 ava 2 s  . co  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.JavadocContents.java

License:Open Source License

private String computeMethodAnchorPrefixEnd(BinaryMethod method) throws JavaModelException {
    String typeQualifiedName = null;
    if (this.type.isMember()) {
        IType currentType = this.type;
        StringBuffer buffer = new StringBuffer();
        while (currentType != null) {
            buffer.insert(0, currentType.getElementName());
            currentType = currentType.getDeclaringType();
            if (currentType != null) {
                buffer.insert(0, '.');
            }/*from w  w  w.  jav a  2 s.c o m*/
        }
        typeQualifiedName = new String(buffer.toString());
    } else {
        typeQualifiedName = this.type.getElementName();
    }

    String methodName = method.getElementName();
    if (method.isConstructor()) {
        methodName = typeQualifiedName;
    }
    IBinaryMethod info = (IBinaryMethod) method.getElementInfo();

    char[] genericSignature = info.getGenericSignature();
    String anchor = null;
    if (genericSignature != null) {
        genericSignature = CharOperation.replaceOnCopy(genericSignature, '/', '.');
        anchor = Util.toAnchor(0, genericSignature, methodName, Flags.isVarargs(method.getFlags()));
        if (anchor == null)
            throw new JavaModelException(
                    new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, method));
    } else {
        anchor = Signature.toString(method.getSignature().replace('/', '.'), methodName, null, true, false,
                Flags.isVarargs(method.getFlags()));
    }
    IType declaringType = this.type;
    if (declaringType.isMember()) {
        // might need to remove a part of the signature corresponding to the synthetic argument (only for constructor)
        if (method.isConstructor() && !Flags.isStatic(declaringType.getFlags())) {
            int indexOfOpeningParen = anchor.indexOf('(');
            if (indexOfOpeningParen == -1) {
                // should not happen as this is a method signature
                return null;
            }
            int index = indexOfOpeningParen;
            indexOfOpeningParen++;
            int indexOfComma = anchor.indexOf(',', index);
            if (indexOfComma != -1) {
                index = indexOfComma + 2;
            } else {
                // no argument, but a synthetic argument
                index = anchor.indexOf(')', index);
            }
            anchor = anchor.substring(0, indexOfOpeningParen) + anchor.substring(index);
        }
    }
    return anchor + JavadocConstants.ANCHOR_PREFIX_END;
}

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());/*from  w w  w  .  j  a  v a  2  s  . c  o m*/

    /*
     * 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/*from  w ww  .  j a va  2  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) {
        LOG.error(e.getMessage(), e); // NotExistsException will not reach this point
    }
}

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

License:Open Source License

/**
 * Appends the label for a method. Considers the M_* flags.
 *
 * @param method the element to render/*www. j  a  v a2  s  .  com*/
 * @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, USE_RESOLVED) && method.isResolved()
                ? new BindingKey(method.getKey())
                : null;
        String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;

        // type parameters
        if (getFlag(flags, 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, 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, M_FULLY_QUALIFIED)) {
            appendTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }

        fBuffer.append(getElementName(method));

        // constructor type arguments
        if (getFlag(flags, 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, M_PARAMETER_TYPES | M_PARAMETER_NAMES)) {
            String[] types = null;
            int nParams = 0;
            boolean renderVarargs = false;
            boolean isPolymorphic = false;
            if (getFlag(flags, 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 && method
                        .getAnnotation("java.lang.invoke.MethodHandle$PolymorphicSignature").exists()) {
                    renderVarargs = false;
                    isPolymorphic = true;
                }
            }
            String[] names = null;
            if (getFlag(flags, 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, M_PARAMETER_ANNOTATIONS)) {
                annotatedParameters = method.getParameters();
            }

            for (int i = 0; i < nParams; i++) {
                if (i > 0) {
                    fBuffer.append(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(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(ELLIPSIS_STRING);
            }
        }
        fBuffer.append(')');

        if (getFlag(flags, 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(COMMA_STRING);
                    }
                    appendTypeSignatureLabel(method, types[i], flags);
                }
            }
        }

        if (getFlag(flags, 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, M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) {
            int offset = fBuffer.length();
            fBuffer.append(DECL_STRING);
            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig)
                    : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, flags);
        }

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

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

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

From source file:io.sarl.eclipse.util.Jdt2Ecore.java

License:Apache License

/** Analyzing the type hierarchy of the given element, and
 * extract any type-related information.
 *
 * <p>The type finder could be obtained with {@link #toTypeFinder(IJavaProject)}.
 *
 * @param typeFinder - the type finder to be used for finding the type definitions.
 * @param finalOperations - filled with the final operations inherited by the element.
 * @param overridableOperations - filled with the oervrideable operations inherited by the element.
 * @param inheritedFields - filled with the fields inherited by the element.
 * @param operationsToImplement - filled with the abstract operations inherited by the element.
 * @param superConstructors - filled with the construstors of the super type.
 * @param superClass - the name of the super class.
 * @param superInterfaces - the super interfaces.
 * @return the status of the operation.//  w w w .ja v  a  2s  .  co m
 * @throws JavaModelException if the Java model is invalid.
 * @see #toTypeFinder(IJavaProject)
 */
@SuppressWarnings({ "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity", "checkstyle:nestedifdepth",
        "checkstyle:parameternumber" })
public IStatus populateInheritanceContext(TypeFinder typeFinder, Map<ActionPrototype, IMethod> finalOperations,
        Map<ActionPrototype, IMethod> overridableOperations, Map<String, IField> inheritedFields,
        Map<ActionPrototype, IMethod> operationsToImplement,
        Map<ActionParameterTypes, IMethod> superConstructors, String superClass, List<String> superInterfaces)
        throws JavaModelException {
    final Set<ActionPrototype> treatedElements = new TreeSet<>();
    final SARLEclipsePlugin plugin = SARLEclipsePlugin.getDefault();
    final List<IStatus> statuses = new ArrayList<>();
    // Get the operations that must be implemented
    if (operationsToImplement != null) {
        final SuperTypeIterator typeIterator = new SuperTypeIterator(typeFinder, true, superInterfaces);
        while (typeIterator.hasNext()) {
            final IType type = typeIterator.next();
            for (final IMethod operation : type.getMethods()) {
                if (!Flags.isStatic(operation.getFlags()) && !Flags.isFinal(operation.getFlags())
                        && !operation.isLambdaMethod() && !operation.isConstructor()) {
                    final ActionParameterTypes sig = this.actionPrototypeProvider.createParameterTypes(
                            Flags.isVarargs(operation.getFlags()), getFormalParameterProvider(operation));
                    final ActionPrototype actionKey = this.actionPrototypeProvider
                            .createActionPrototype(operation.getElementName(), sig);
                    if (treatedElements.add(actionKey)) {
                        if (Flags.isDefaultMethod(operation.getFlags())) {
                            if (!overridableOperations.containsKey(actionKey)) {
                                overridableOperations.put(actionKey, operation);
                            }
                        } else {
                            if (!operationsToImplement.containsKey(actionKey)) {
                                operationsToImplement.put(actionKey, operation);
                            }
                        }
                    }
                }
            }
        }
        statuses.addAll(typeIterator.getStatuses());
    }

    // Check on the implemented features, inherited from the super type
    if (isValidSuperType(superClass)) {
        final SuperTypeIterator typeIterator = new SuperTypeIterator(typeFinder, false, superClass);
        while (typeIterator.hasNext()) {
            final IType type = typeIterator.next();
            final boolean checkForConstructors = superConstructors != null
                    && type.getFullyQualifiedName().equals(superClass);
            for (final IMethod operation : type.getMethods()) {
                if (!Flags.isStatic(operation.getFlags()) && !operation.isLambdaMethod()
                        && isVisible(typeFinder, type, operation)) {
                    if (!operation.isConstructor() && !Utils.isHiddenMember(operation.getElementName())) {
                        final ActionParameterTypes sig = this.actionPrototypeProvider.createParameterTypes(
                                Flags.isVarargs(operation.getFlags()), getFormalParameterProvider(operation));
                        final ActionPrototype actionKey = this.actionPrototypeProvider
                                .createActionPrototype(operation.getElementName(), sig);
                        if (treatedElements.add(actionKey)) {
                            final int flags = operation.getFlags();
                            if (Flags.isAbstract(flags) && !Flags.isDefaultMethod(flags)) {
                                if (operationsToImplement != null) {
                                    operationsToImplement.put(actionKey, operation);
                                }
                            } else if (Flags.isFinal(flags)) {
                                if (finalOperations != null) {
                                    finalOperations.put(actionKey, operation);
                                }
                                if (operationsToImplement != null) {
                                    operationsToImplement.remove(actionKey);
                                }
                            } else {
                                if (overridableOperations != null) {
                                    overridableOperations.put(actionKey, operation);
                                }
                                if (operationsToImplement != null) {
                                    operationsToImplement.remove(actionKey);
                                }
                            }
                        }
                    } else if (checkForConstructors && operation.isConstructor() && superConstructors != null) {
                        final ActionParameterTypes sig = this.actionPrototypeProvider.createParameterTypes(
                                Flags.isVarargs(operation.getFlags()), getFormalParameterProvider(operation));
                        superConstructors.put(sig, operation);
                    }
                }
            }

            if (inheritedFields != null) {
                for (final IField field : type.getFields()) {
                    if (!Flags.isStatic(field.getFlags()) && !Utils.isHiddenMember(field.getElementName())
                            && isVisible(typeFinder, type, field)) {
                        inheritedFields.putIfAbsent(field.getElementName(), field);
                    }
                }
            }
        }
        statuses.addAll(typeIterator.getStatuses());
    }
    if (statuses.isEmpty()) {
        return plugin.createOkStatus();
    }
    if (statuses.size() == 1) {
        return statuses.get(0);
    }
    return plugin.createMultiStatus(statuses);
}

From source file:io.sarl.eclipse.util.Jdt2Ecore.java

License:Apache License

/** Create the JvmConstructor for the given JDT constructor.
 *
 * @param constructor - the JDT constructor.
 * @param context - the context of the constructor.
 * @return the JvmConstructor/*from  w  w w.j  ava  2s  . c  o m*/
 * @throws JavaModelException if the Java model is invalid.
 */
public JvmConstructor getJvmConstructor(IMethod constructor, XtendTypeDeclaration context)
        throws JavaModelException {
    if (constructor.isConstructor()) {
        final JvmType type = this.typeReferences
                .findDeclaredType(constructor.getDeclaringType().getFullyQualifiedName(), context);
        if (type instanceof JvmDeclaredType) {
            final JvmDeclaredType declaredType = (JvmDeclaredType) type;
            final ActionParameterTypes jdtSignature = this.actionPrototypeProvider.createParameterTypes(
                    Flags.isVarargs(constructor.getFlags()), getFormalParameterProvider(constructor));
            for (final JvmConstructor jvmConstructor : declaredType.getDeclaredConstructors()) {
                final ActionParameterTypes jvmSignature = this.actionPrototypeProvider
                        .createParameterTypesFromJvmModel(jvmConstructor.isVarArgs(),
                                jvmConstructor.getParameters());
                if (jvmSignature.equals(jdtSignature)) {
                    return jvmConstructor;
                }
            }
        }
    }
    return null;
}