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

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

Introduction

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

Prototype

IMethod[] findMethods(IMethod method);

Source Link

Document

Finds the methods in this type that correspond to the given method.

Usage

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

License:Open Source License

public void consumeMethod(char[] selector, char[] signature) {
    if (!(this.element instanceof IType))
        return;/*www  .  j a va 2  s .  c o m*/
    String[] parameterTypes = Signature.getParameterTypes(new String(signature));
    IType type = (IType) this.element;
    IMethod method = type.getMethod(new String(selector), parameterTypes);
    IMethod[] methods = type.findMethods(method);
    if (methods.length > 0)
        this.element = methods[0];
}

From source file:com.github.ajaxsys.jdtx.utils.JDTUtils.java

License:Open Source License

/**
 * Find the IMethod in the workspace corresponding to a method selector.
 *
 * TODO: this is way too slow. figure out something better.
 *
 * @return null if not found//  ww  w  . j  a v a  2  s  .com
 */
public static IMethod findJavaMethodInProjects(String klass, String selector,
        Collection<IJavaProject> projects) {

    if (klass == null) {
        throw new IllegalArgumentException("null klass");
    }
    if (projects == null) {
        throw new IllegalArgumentException("null projects");
    }
    IType type = null;
    try {
        type = findJavaClassInProjects(klass, projects);
    } catch (Throwable t) {
        return null;
    }
    if (type == null) {
        return null;
    }
    String name = parseForName(selector, type);
    String[] paramTypes = parseForParameterTypes(selector);
    IMethod m = type.getMethod(name, paramTypes);
    IMethod[] methods = type.findMethods(m);
    if (methods != null && methods.length == 1) {
        return methods[0];
    } else {
        // methods is null. probably got screwed by generics.
        // i spent 5 hours trying to figure out how to fix this correctly
        // and failed. implementing a miserable hack instead.
        // Need to consult a guru to figure out how to do this.
        try {
            List<IMethod> matches = new ArrayList<IMethod>();
            Collection<String> typeParameterNames = getTypeParameterNames(type);
            METHODS: for (IMethod x : type.getMethods()) {
                if (x.getElementName().equals(name)) {
                    if (x.getParameterTypes().length == paramTypes.length) {
                        for (int i = 0; i < x.getParameterTypes().length; i++) {
                            String s1 = Signature
                                    .getTypeErasure(Signature.getSignatureSimpleName(x.getParameterTypes()[i]));
                            String s2 = Signature
                                    .getTypeErasure(Signature.getSignatureSimpleName(paramTypes[i]));
                            if (typeParameterNames.contains(s1)) {
                                // s1 is a type parameter to the class.
                                // optimistically assume
                                // the types match.
                            } else {
                                if (!s1.equals(s2)) {
                                    // no match
                                    continue METHODS;
                                }
                            }
                        }
                        matches.add(x);
                    }
                }
            }
            if (matches.size() == 1) {
                return matches.get(0);
            } else {
                System.err.println("findJavaMethodInWorkspace FAILED TO MATCH " + m);
                return null;
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
            return null;
        }
    }
}

From source file:com.ibm.wala.ide.util.JdtUtil.java

License:Open Source License

/**
 * Find the IMethod in the workspace corresponding to a method selector.
 * //from  w w w  .ja va  2 s.  co m
 * TODO: this is way too slow. figure out something better.
 * 
 * @return null if not found
 */
public static IMethod findJavaMethodInProjects(String klass, String selector,
        Collection<IJavaProject> projects) {

    if (klass == null) {
        throw new IllegalArgumentException("null klass");
    }
    if (projects == null) {
        throw new IllegalArgumentException("null projects");
    }
    IType type = null;
    try {
        type = findJavaClassInProjects(klass, projects);
    } catch (Throwable t) {
        return null;
    }
    if (type == null) {
        return null;
    }
    String name = parseForName(selector, type);
    String[] paramTypes = parseForParameterTypes(selector);
    IMethod m = type.getMethod(name, paramTypes);
    IMethod[] methods = type.findMethods(m);
    if (methods != null && methods.length == 1) {
        return methods[0];
    } else {
        // methods is null. probably got screwed by generics.
        // i spent 5 hours trying to figure out how to fix this correctly
        // and failed. implementing a miserable hack instead.
        // Need to consult a guru to figure out how to do this.
        try {
            List<IMethod> matches = new ArrayList<IMethod>();
            Collection<String> typeParameterNames = getTypeParameterNames(type);
            METHODS: for (IMethod x : type.getMethods()) {
                if (x.getElementName().equals(name)) {
                    if (x.getParameterTypes().length == paramTypes.length) {
                        for (int i = 0; i < x.getParameterTypes().length; i++) {
                            String s1 = Signature
                                    .getTypeErasure(Signature.getSignatureSimpleName(x.getParameterTypes()[i]));
                            String s2 = Signature
                                    .getTypeErasure(Signature.getSignatureSimpleName(paramTypes[i]));
                            if (typeParameterNames.contains(s1)) {
                                // s1 is a type parameter to the class. optimistically assume
                                // the types match.
                            } else {
                                if (!s1.equals(s2)) {
                                    // no match
                                    continue METHODS;
                                }
                            }
                        }
                        matches.add(x);
                    }
                }
            }
            if (matches.size() == 1) {
                return matches.get(0);
            } else {
                System.err.println("findJavaMethodInWorkspace FAILED TO MATCH " + m);
                return null;
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
            return null;
        }
    }
}

From source file:com.liferay.ide.service.ui.editor.ServiceMethodHyperlinkDetector.java

License:Open Source License

private IMethod getServiceImplMethod(final IMethod method) {
    IMethod retval = null;//ww  w. jav  a 2 s  . c  om

    try {
        final IJavaElement methodClass = method.getParent();
        final IType methodClassType = method.getDeclaringType();
        final String methodClassName = methodClass.getElementName();

        if (methodClassName.endsWith("Util") && JdtFlags.isPublic(method) && JdtFlags.isStatic(method)) {
            final String packageName = methodClassType.getPackageFragment().getElementName();
            final String baseServiceName = methodClassName.substring(0, methodClassName.length() - 4);
            // as per liferay standard real implementation will be in impl package and Impl suffix
            // e.g. com.example.service.FooUtil.getBar() --> com.example.service.impl.FooImpl.getBar()
            final String fullyQualifiedName = packageName + ".impl." + baseServiceName + "Impl";
            final IType implType = findType(methodClass, fullyQualifiedName);

            if (implType != null) {
                IMethod[] methods = implType.findMethods(method);

                if (CoreUtil.isNullOrEmpty(methods)) {
                    final ITypeHierarchy hierarchy = implType.newSupertypeHierarchy(new NullProgressMonitor());
                    IType currentType = implType;

                    while (retval == null && currentType != null) {
                        methods = currentType.findMethods(method);// match name and arguments

                        if (!CoreUtil.isNullOrEmpty(methods)) {
                            retval = methods[0];
                        } else {
                            currentType = hierarchy.getSuperclass(currentType);
                        }
                    }
                } else {
                    retval = methods[0];
                }
            }
        }
    } catch (Exception e) {
    }

    return retval;
}

From source file:com.liferay.ide.service.ui.editor.ServiceMethodHyperlinkDetector.java

License:Open Source License

private IMethodWrapper getServiceWrapperMethod(final IMethod method) {
    IMethodWrapper retval = null;// w w w.  j a v  a2  s .c  om

    try {
        final IJavaElement methodClass = method.getParent();
        final IType methodClassType = method.getDeclaringType();
        final String methodClassName = methodClass.getElementName();

        if (methodClassName.endsWith("Util") && JdtFlags.isPublic(method) && JdtFlags.isStatic(method)) {
            final String packageName = methodClassType.getPackageFragment().getElementName();
            final String baseServiceName = methodClassName.substring(0, methodClassName.length() - 4);
            // as per liferay standard wrapper type will be in service package with Wrapper suffix
            // e.g. com.example.service.FooUtil.getBar() --> com.example.service.FooWrapper.getBar()
            final String fullyQualifiedName = packageName + "." + baseServiceName + "Wrapper";
            final IType wrapperType = findType(methodClass, fullyQualifiedName);

            if (wrapperType != null) {
                IMethod[] wrapperBaseMethods = wrapperType.findMethods(method);

                if (!CoreUtil.isNullOrEmpty(wrapperBaseMethods)) {
                    // look for classes that implement this wrapper
                    final List<IMethod> overrides = new ArrayList<IMethod>();
                    final SearchRequestor requestor = new WrapperMethodCollector(overrides, method);

                    final IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(null, wrapperType,
                            true, false, null);

                    final SearchPattern search = SearchPattern.createPattern(method.getElementName(),
                            IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS,
                            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

                    new SearchEngine().search(search,
                            new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                            requestor, new NullProgressMonitor());

                    if (overrides.size() > 1) {
                        retval = new IMethodWrapper(wrapperBaseMethods[0], true);
                    } else if (overrides.size() == 1) {
                        retval = new IMethodWrapper(overrides.get(0), false);
                    }
                }
            }
        }
    } catch (Exception e) {
    }

    return retval;
}

From source file:org.eclipse.che.jdt.internal.core.util.JavaElementFinder.java

License:Open Source License

public void consumeMethod(char[] selector, char[] signature) {
    if (!(this.element instanceof IType))
        return;/*ww  w.jav  a2s . c  o m*/
    String[] parameterTypes = Signature.getParameterTypes(new String(signature));
    IType type = (IType) this.element;
    IMethod method = type.getMethod(new String(selector), parameterTypes);
    IMethod[] methods = type.findMethods(method);

    if (methods != null && methods.length > 0)
        this.element = methods[0];
}

From source file:org.eclipse.che.jdt.javadoc.JavaElementLinks.java

License:Open Source License

public static IJavaElement parseURI(String ssp, JavaProject project) {
    //        String ssp= uri.getSchemeSpecificPart();
    String[] segments = ssp.split(String.valueOf(LINK_SEPARATOR));

    // replace '[' manually, since URI confuses it for an IPv6 address as per RFC 2732:
    IJavaElement element = JavaCore.create(segments[1].replace(LINK_BRACKET_REPLACEMENT, '['));

    if (segments.length > 2) {
        String refTypeName = segments[2];
        if (refTypeName.indexOf('.') == -1) {
            try {
                ITypeParameter resolvedTypeVariable = resolveTypeVariable(element, refTypeName);
                if (resolvedTypeVariable != null)
                    return resolvedTypeVariable;
            } catch (JavaModelException e) {
                LOG.error(e.getMessage(), e);
            }/* ww  w  .j  a  v a2s.  c o m*/
        }
        if (element instanceof IAnnotation) {
            element = element.getParent();
        }

        if (element instanceof ILocalVariable) {
            element = ((ILocalVariable) element).getDeclaringMember();
        } else if (element instanceof ITypeParameter) {
            element = ((ITypeParameter) element).getDeclaringMember();
        }
        if (element instanceof IMember && !(element instanceof IType)) {
            element = ((IMember) element).getDeclaringType();
        }

        if (element instanceof IPackageFragment) {
            try {
                IPackageFragment root = (IPackageFragment) element;
                element = resolvePackageInfoType(root, refTypeName);
                if (element == null) {
                    // find it as package
                    IJavaProject javaProject = root.getJavaProject();
                    return JavaModelUtil.findTypeContainer(javaProject, refTypeName);
                }
            } catch (JavaModelException e) {
                LOG.error(e.getMessage(), e);
            }
        }

        if (element instanceof IType) {
            try {
                IType type = (IType) element;
                if (refTypeName.length() > 0) {
                    type = resolveType(type, refTypeName);
                    if (type == null) {
                        IPackageFragment pack = JavaModelUtil.getPackageFragmentRoot(element)
                                .getPackageFragment(refTypeName);
                        if (pack.exists())
                            return pack;
                    }
                }
                if (type != null) {
                    element = type;
                    if (segments.length > 3) {
                        String refMemberName = segments[3];
                        if (segments.length > 4) {
                            String[] paramSignatures = new String[segments[4].length() == 0 ? 0
                                    : segments.length - 4];
                            for (int i = 0; i < paramSignatures.length; i++) {
                                paramSignatures[i] = Signature.createTypeSignature(segments[i + 4], false);
                            }
                            IMethod method = type.getMethod(refMemberName, paramSignatures);
                            IMethod[] methods = type.findMethods(method);
                            if (methods != null) {
                                return methods[0];
                            } else {
                                //TODO: methods whose signature contains type parameters can not be found
                                // easily, since the Javadoc references are erasures

                                //Shortcut: only check name and parameter count:
                                methods = type.getMethods();
                                for (int i = 0; i < methods.length; i++) {
                                    method = methods[i];
                                    if (method.getElementName().equals(refMemberName)
                                            && method.getNumberOfParameters() == paramSignatures.length)
                                        return method;
                                }

                                //                                    // reference can also point to method from supertype:
                                //                                    ITypeHierarchy hierarchy= SuperTypeHierarchyCache.getTypeHierarchy(type);
                                //                                    method= JavaModelUtil.findMethodInHierarchy(hierarchy, type, refMemberName, paramSignatures, false);
                                //                                    if (method != null)
                                //                                        return method;
                            }
                        } else {
                            IField field = type.getField(refMemberName);
                            if (field.exists()) {
                                return field;
                            } else {
                                IMethod[] methods = type.getMethods();
                                for (int i = 0; i < methods.length; i++) {
                                    IMethod method = methods[i];
                                    if (method.getElementName().equals(refMemberName))
                                        return method;
                                }
                            }
                        }

                    }
                } else {
                    // FIXME: either remove or show dialog
                    //                  JavaPlugin.logErrorMessage("JavaElementLinks could not resolve " + uri); //$NON-NLS-1$
                }
                return type;
            } catch (JavaModelException e) {
                LOG.error(e.getMessage(), e);
            }
        }
    }
    return element;
}

From source file:org.eclipse.jst.j2ee.internal.common.operations.JavaModelUtil.java

License:Open Source License

private static IMethod toOriginalMethod(IMethod method) {
    ICompilationUnit cu = method.getCompilationUnit();
    if (cu == null || isPrimary(cu)) {
        return method;
    }/*w  w w  .  java 2 s.  c o  m*/
    try {
        //use the workaround only if needed   
        if (!method.getElementName().equals(method.getDeclaringType().getElementName()))
            return (IMethod) method.getPrimaryElement();

        IType originalType = (IType) toOriginal(method.getDeclaringType());
        IMethod[] methods = originalType.findMethods(method);
        boolean isConstructor = method.isConstructor();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].isConstructor() == isConstructor)
                return methods[i];
        }
        return null;
    } catch (JavaModelException e) {
        return null;
    }
}

From source file:org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil.java

License:Open Source License

private static IMethod toOriginalMethod(IMethod method) {
    ICompilationUnit cu = method.getCompilationUnit();
    if (cu == null || isPrimary(cu)) {
        return method;
    }//from w w w  .java  2s .  c  o  m
    try {
        // use the workaround only if needed
        if (!method.getElementName().equals(method.getDeclaringType().getElementName()))
            return (IMethod) method.getPrimaryElement();

        IType originalType = (IType) toOriginal(method.getDeclaringType());
        IMethod[] methods = originalType.findMethods(method);
        boolean isConstructor = method.isConstructor();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].isConstructor() == isConstructor)
                return methods[i];
        }
        return null;
    } catch (JavaModelException e) {
        return null;
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.PhantomType.java

License:Open Source License

public IMethod[] findMethods(IMethod method) {
    if (this._allRealTypes == null)
        return this._realType.findMethods(method);
    for (IType type : this._allRealTypes) {
        IMethod[] ms = type.findMethods(method);
        if (ms != null)
            return ms;
    }//from   w ww  . ja  v a 2s .  c  om
    return null;
}