Example usage for org.eclipse.jdt.core IMethod getReturnType

List of usage examples for org.eclipse.jdt.core IMethod getReturnType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IMethod getReturnType.

Prototype

String getReturnType() throws JavaModelException;

Source Link

Document

Returns the type signature of the return value of this method.

Usage

From source file:ar.com.tadp.xml.rinzo.jdt.wizards.NewJAXBParserWizard.java

License:Open Source License

private RootTypeFactoryMethod getRootType() {
    try {//from w w  w  .j  a  v a  2 s. c o m
        IMethod[] methods = this.newParserPage.getPackageFragmentRoot()
                .getPackageFragment(this.newParserPage.getPackage()).getCompilationUnit("ObjectFactory.java")
                .getType("ObjectFactory").getMethods();
        for (IMethod method : methods) {
            String returnType = method.getReturnType();
            if (returnType.contains("JAXBElement")) {
                String rootType = returnType.substring(returnType.indexOf("<"), returnType.lastIndexOf(">"));
                rootType = rootType.substring(rootType.indexOf("Q") + 1, rootType.lastIndexOf(";"));
                return new RootTypeFactoryMethod(method.getElementName(), rootType);
            }
        }
        for (IMethod method : methods) {
            String returnType = method.getReturnType();
            if (!returnType.contains(".") && method.getElementName().startsWith("create")) {
                String rootType = returnType.substring(returnType.indexOf("Q") + 1,
                        returnType.lastIndexOf(";"));
                return new RootTypeFactoryMethod(method.getElementName(), rootType);
            }
        }

    } catch (JavaModelException e) {
        XMLEditorPlugin.log(e);
    }
    return null;
}

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.util.JDTHelper.java

License:Open Source License

private TypeData createData(List<IMethod> allMethods, IJavaProject jproject) throws JavaModelException {
    TypeData d = new TypeData();
    for (IMethod m : allMethods) {
        if (!Flags.isPublic(m.getFlags())) {
            continue;
        }//from   ww w.jav  a 2 s .  co m

        if (m.getElementName().startsWith("impl_") || m.getElementName().startsWith("getImpl_")) {
            continue;
        }

        if (m.getElementName().startsWith("get") && m.getParameterNames().length == 0) {
            String returnSignature = Signature.toString(m.getReturnType());
            if (returnSignature.startsWith("javafx.event.EventHandler<? super ")
                    || returnSignature.startsWith("javafx.event.EventHandler<")) {
                String eventType;
                if (returnSignature.startsWith("javafx.event.EventHandler<? super ")) {
                    eventType = returnSignature.substring("javafx.event.EventHandler<? super ".length(),
                            returnSignature.length() - 1);
                } else {
                    eventType = returnSignature.substring("javafx.event.EventHandler<".length(),
                            returnSignature.length() - 1);
                }

                EventValueProperty p = new EventValueProperty(m, extractAttributename(m.getElementName()),
                        m.getParent().getElementName(), eventType);
                d.properties.add(p);

            } else {
                String propName = extractAttributename(m.getElementName());
                String ownerName = m.getParent().getElementName();
                boolean isReadonly = isReadonlySetter(propName, allMethods);

                if ("double".equals(returnSignature) || "float".equals(returnSignature)) {
                    if (!isReadonly) {
                        FloatingValueProperty p = new FloatingValueProperty(m, propName, ownerName,
                                returnSignature);
                        d.properties.add(p);
                    }
                } else if ("int".equals(returnSignature) || "long".equals(returnSignature)
                        || "short".equals(returnSignature) || "byte".equals(returnSignature)
                        || "char".equals(returnSignature)) {
                    if (!isReadonly) {
                        IntegerValueProperty p = new IntegerValueProperty(m, propName, ownerName,
                                returnSignature);
                        d.properties.add(p);
                    }
                } else {
                    IType type;
                    if (returnSignature.indexOf('<') == -1) {
                        type = jproject.findType(returnSignature);
                    } else {
                        type = jproject.findType(returnSignature.substring(0, returnSignature.indexOf('<')));
                    }

                    if (type == null) {
                        continue;
                    }

                    if (type.isEnum()) {
                        if (!isReadonly) {
                            EnumValueProperty p = new EnumValueProperty(m, propName, ownerName, returnSignature,
                                    type);
                            d.properties.add(p);
                        }
                    } else {
                        boolean isLists = false;
                        boolean isMap = false;
                        if ("java.util.List".equals(type.getFullyQualifiedName())) {
                            isLists = true;
                        } else {
                            for (String i : type.getSuperInterfaceNames()) {
                                if (i.equals("java.util.List")) {
                                    isLists = true;
                                }
                            }
                        }

                        if (!isLists) {
                            if ("java.util.Map".equals(type.getFullyQualifiedName())) {
                                isMap = true;
                            } else {
                                for (String i : type.getSuperInterfaceNames()) {
                                    if (i.equals("java.util.Map")) {
                                        isMap = true;
                                    }
                                }
                            }
                        }

                        if (isLists) {
                            String listType;
                            if (returnSignature.indexOf('<') != -1) {
                                listType = returnSignature.substring(returnSignature.indexOf('<') + 1,
                                        returnSignature.lastIndexOf('>'));
                            } else {
                                listType = "?";
                            }

                            if (!propName.endsWith("Unmodifiable")) {
                                ListValueProperty p = new ListValueProperty(m, propName, ownerName, listType,
                                        isReadonly);
                                d.properties.add(p);
                            }
                        } else if (isMap) {
                            MapValueProperty p = new MapValueProperty(m, propName, ownerName);
                            d.properties.add(p);
                        } else if (type.getFullyQualifiedName().equals("java.lang.String")) {
                            if (!isReadonly) {
                                StringValueProperty p = new StringValueProperty(m, propName, ownerName,
                                        returnSignature);
                                d.properties.add(p);
                            }
                        } else {
                            if (!isReadonly) {
                                List<Proposal> props = getProposals(type, jproject);
                                ElementValueProperty p = new ElementValueProperty(m, propName, ownerName,
                                        returnSignature, props);
                                d.properties.add(p);
                            }
                        }
                    }
                }
            }
        } else if (m.getElementName().startsWith("is") && m.getParameterNames().length == 0
                && "Z".equals(m.getReturnType())) {
            String propName = extractAttributename(m.getElementName());
            boolean isReadonly = isReadonlySetter(propName, allMethods);

            if (!isReadonly) {
                BooleanValueProperty p = new BooleanValueProperty(m, propName, m.getParent().getElementName(),
                        "boolean");
                d.properties.add(p);
            }
        }
    }
    return d;
}

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

License:Open Source License

@Override
public IType getElementType() {
    if (elementType == null) {
        try {/*from  w w  w  . ja v  a  2s.c  o  m*/
            IMethod m = (IMethod) getJavaElement();
            String signature;

            if (isSetable()) {
                signature = m.getParameterTypes()[0];
            } else {
                signature = m.getReturnType();
            }

            //TODO if the value is a generic parameter we need to resolve it
            //using the class' generic parameter
            String genericType = Signature.toString(signature);

            String eType;
            if (genericType.contains("extends")) {
                eType = genericType.substring(genericType.indexOf("extends") + "extends".length(),
                        genericType.indexOf('>'));
            } else if (genericType.contains("super")) {
                eType = genericType.substring(genericType.indexOf("super") + "super".length(),
                        genericType.indexOf('>'));
            } else {
                eType = genericType.substring(genericType.indexOf('<') + 1, genericType.lastIndexOf('>'));
                eType = Signature.getTypeErasure(eType);
            }

            eType = eType.trim();

            IType t = (IType) m.getParent();
            String fqnType = Util.getFQNType(t, eType);
            if (fqnType == null) {
                return null;
            }
            elementType = getFXClass().getJavaProject().findType(fqnType);
        } catch (JavaModelException e) {
            // TODO Auto-generated method stub
            e.printStackTrace();
        }
    }

    return elementType;
}

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

License:Open Source License

public IType getEventType() {
    if (eventType == null) {
        try {/*from   w w  w . ja v a  2s .co m*/
            IMethod m = (IMethod) getJavaElement();
            String signature;

            if (isSetable()) {
                signature = m.getParameterTypes()[0];
            } else {
                signature = m.getReturnType();
            }

            IType t = (IType) m.getParent();
            String fqnType = Util.toFQN(t, signature);
            eventType = getFXClass().getJavaProject().findType(fqnType);
        } catch (JavaModelException e) {
            // TODO Auto-generated method stub
            e.printStackTrace();
        }
    }

    return eventType;
}

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

License:Open Source License

private static FXProperty getProperty(FXClass fxClass, String name, IMethod m) throws JavaModelException {
    FXProperty p = null;/*from   ww w  .j  a va 2  s  .  c  om*/

    String signature;

    if (m.getElementName().startsWith("get") || m.getElementName().startsWith("is")) {
        signature = m.getReturnType();
    } else {
        // setter or builder method
        signature = m.getParameterTypes()[0];
    }

    String genericType = Signature.toString(signature);

    if (FXPrimitiveProperty.isPrimitive(genericType)) {
        p = new FXPrimitiveProperty(fxClass, name, m, Type.parseType(genericType), false);
    } else {
        String erasedFQNType = Util.getFQNType((IType) m.getParent(), Signature.getTypeErasure(genericType));
        if (erasedFQNType != null) {
            if (FXEventHandlerProperty.isEventHandler(fxClass.getJavaProject(), erasedFQNType)) {
                p = new FXEventHandlerProperty(fxClass, name, m, false);
            } else if (FXCollectionProperty.isList(fxClass.getJavaProject(), erasedFQNType)) {
                p = new FXCollectionProperty(fxClass, name, m, erasedFQNType, genericType, false);
            } else if (FXMapProperty.isMap(fxClass.getJavaProject(), erasedFQNType)) {
                p = new FXMapProperty(fxClass, name, m, false);
            } else if (FXEnumProperty.isEnum(fxClass.getJavaProject(), erasedFQNType)) {
                p = new FXEnumProperty(fxClass, name, m, erasedFQNType, false);
            } else {
                p = new FXObjectPoperty(fxClass, name, m, erasedFQNType, false);
            }
        }
    }

    return p;
}

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  .  j av a  2  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, 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:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findActivateMethod(IType implClassType, String name, IProgressMonitor monitor)
        throws JavaModelException {
    IMethod candidate = null;//  w w w.j  a v a2 s  .  c  o m
    int priority = Integer.MAX_VALUE;

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, Integer.MAX_VALUE);

            if (paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (priority > 1 && paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 1;
                continue;
            }

            if (priority > 2 && paramSigs.length == 1 && MAP_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 2;
                continue;
            }

            if (priority > 3 && paramSigs.length >= 2) {
                boolean valid = true;
                for (String paramSig : paramSigs) {
                    if (!COMPONENT_CONTEXT_SIG.equals(paramSig) && !BUNDLE_CONTEXT_SIG.equals(paramSig)
                            && !MAP_SIG.equals(paramSig)) {
                        valid = false;
                        break;
                    }
                }

                if (valid) {
                    candidate = method;
                    priority = 3;
                }

                continue;
            }

            if (priority > 4 && paramSigs.length == 0) {
                candidate = method;
                priority = 4;
                continue;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findDeactivateMethod(IType implClassType, String name, IProgressMonitor monitor)
        throws JavaModelException {
    IMethod candidate = null;//from   w w  w .ja  va2s .c  o m
    int priority = Integer.MAX_VALUE;

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, Integer.MAX_VALUE);

            if (paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (priority > 1 && paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 1;
                continue;
            }

            if (priority > 2 && paramSigs.length == 1 && MAP_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 2;
                continue;
            }

            if (priority > 3 && paramSigs.length == 1 && INT_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 3;
                continue;
            }

            if (priority > 4 && paramSigs.length == 1 && INTEGER_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 4;
                continue;
            }

            if (priority > 5 && paramSigs.length >= 2) {
                boolean valid = true;
                for (String paramSig : paramSigs) {
                    if (!COMPONENT_CONTEXT_SIG.equals(paramSig) && !BUNDLE_CONTEXT_SIG.equals(paramSig)
                            && !MAP_SIG.equals(paramSig) && !INT_SIG.equals(paramSig)
                            && !INTEGER_SIG.equals(paramSig)) {
                        valid = false;
                        break;
                    }
                }

                if (valid) {
                    candidate = method;
                    priority = 5;
                }

                continue;
            }

            if (priority > 6 && paramSigs.length == 0) {
                candidate = method;
                priority = 6;
                continue;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findBindMethod(IType implClassType, String name, String referenceTypeName,
        IProgressMonitor monitor) throws JavaModelException {
    IMethod candidate = null;// www.  ja  v  a 2s  . co m
    int priority = Integer.MAX_VALUE;

    String referenceTypeSig = Signature.createTypeSignature(referenceTypeName, true);
    IType referenceType = null;
    IType arg0Type = null;

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, 2);

            if (paramSigs.length == 1 && SERVICE_REFERENCE_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (priority > 1 && paramSigs.length == 1 && referenceTypeSig.equals(paramSigs[0])) {
                candidate = method;
                priority = 1;
                continue;
            }

            if (priority > 2 && paramSigs.length == 1) {
                if (referenceType == null)
                    referenceType = implClassType.getJavaProject().findType(referenceTypeName, monitor);

                if (arg0Type == null)
                    arg0Type = implClassType.getJavaProject().findType(Signature.toString(paramSigs[0]),
                            monitor);

                if (isAssignableFrom(arg0Type, referenceType, monitor)) {
                    candidate = method;
                    priority = 2;
                }

                continue;
            }

            if (priority > 3 && paramSigs.length == 2 && referenceTypeSig.equals(paramSigs[0])
                    && MAP_SIG.equals(paramSigs[1])) {
                candidate = method;
                priority = 3;
                continue;
            }

            if (priority > 4 && paramSigs.length == 2 && MAP_SIG.equals(paramSigs[1])) {
                if (referenceType == null)
                    referenceType = implClassType.getJavaProject().findType(referenceTypeName, monitor);

                if (arg0Type == null)
                    arg0Type = implClassType.getJavaProject().findType(Signature.toString(paramSigs[0]),
                            monitor);

                if (isAssignableFrom(arg0Type, referenceType, monitor)) {
                    candidate = method;
                    priority = 4;
                }

                continue;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findUpdatedMethod(IType implClassType, String name, IProgressMonitor monitor)
        throws JavaModelException {
    IMethod candidate = null;/* w  ww .  j  a v a2  s.  co m*/

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, 1);

            if (paramSigs.length != 1)
                continue;

            if (SERVICE_REFERENCE_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (candidate == null && MAP_SIG.equals(paramSigs[0])) {
                candidate = method;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}