Example usage for org.eclipse.jdt.core.search IJavaSearchConstants METHOD

List of usage examples for org.eclipse.jdt.core.search IJavaSearchConstants METHOD

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.search IJavaSearchConstants METHOD.

Prototype

int METHOD

To view the source code for org.eclipse.jdt.core.search IJavaSearchConstants METHOD.

Click Source Link

Document

The searched element is a method.

Usage

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

License:Open Source License

/**
 * Find the IMethod in the workspace corresponding to a method signature.
 *
 * This doesn't work for elements declared in inner classes. It's possible
 * this is a 3.2 bug fixed in 3.3//  w  w  w. ja  va2s .co  m
 *
 * @return null if not found
 */
@Deprecated
public static IMethod findJavaMethodInWorkspaceBrokenForInnerClasses(String methodSig) {
    // dammit ... this doesn't work for inner classes.

    System.err.println("Search for " + methodSig);
    SearchPattern p = SearchPattern.createPattern(methodSig, IJavaSearchConstants.METHOD,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    SearchEngine engine = new SearchEngine();
    final Collection<IJavaElement> kludge = new ArrayList<IJavaElement>();
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            kludge.add((IJavaElement) match.getElement());
        }

    };
    try {
        engine.search(p, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                requestor, null);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    if (kludge.size() == 1) {
        return (IMethod) kludge.iterator().next();
    } else {
        System.err.println("RETURNED " + kludge.size() + " " + kludge);
        return null;
    }
}

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

License:Open Source License

/**
 * Use the search engine to find all methods in a java element
 *//*from  w  w w.  j ava2  s. c  o m*/
public static Collection<IMethod> findMethods(IJavaElement elt) {

    if (elt instanceof ICompilationUnit) {
        Collection<IMethod> result = new ArrayList<IMethod>();
        for (IType type : getClasses((ICompilationUnit) elt)) {
            try {
                for (IMethod m : type.getMethods()) {
                    result.add(m);
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
        return result;
    } else {
        final Collection<IMethod> result = new ArrayList<IMethod>();
        SearchPattern p = SearchPattern.createPattern("*", IJavaSearchConstants.METHOD,
                IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
        SearchPattern p2 = SearchPattern.createPattern("*", IJavaSearchConstants.CONSTRUCTOR,
                IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { elt },
                IJavaSearchScope.SOURCES);
        SearchEngine engine = new SearchEngine();
        SearchRequestor requestor = new SearchRequestor() {
            @Override
            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                if (match.getElement() instanceof IMethod) {
                    result.add((IMethod) match.getElement());
                }
            }
        };
        try {
            engine.search(p, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                    requestor, null);
            engine.search(p2, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                    requestor, null);
        } catch (CoreException e) {
            e.printStackTrace();
        }

        return result;
    }
}

From source file:com.google.gwt.eclipse.core.search.JavaQueryParticipant.java

License:Open Source License

private Set<IIndexedJavaRef> findMatches(PatternQuerySpecification query) {
    // Translate the IJavaSearchConstant element type constants to IJavaElement
    // type constants.
    int elementType;
    switch (query.getSearchFor()) {
    case IJavaSearchConstants.TYPE:
        elementType = IJavaElement.TYPE;
        break;//w  w w. j ava  2s  .  c o  m
    case IJavaSearchConstants.FIELD:
        elementType = IJavaElement.FIELD;
        break;
    case IJavaSearchConstants.METHOD:
    case IJavaSearchConstants.CONSTRUCTOR:
        // IJavaElement.METHOD represents both methods & ctors
        elementType = IJavaElement.METHOD;
        break;
    default:
        // We only support searching for types, fields, methods, and ctors
        return Collections.emptySet();
    }

    return JavaRefIndex.getInstance().findElementReferences(query.getPattern(), elementType,
            query.isCaseSensitive());
}

From source file:com.google.gwt.eclipse.core.search.JavaQueryParticipantTest.java

License:Open Source License

public void testPatternSearch() throws CoreException {
    Match[] expected;//from  w  w w .j av a 2s  .c o  m

    // Search for type references by simple name
    expected = new Match[] { createWindowsTestMatch(840, 50), createWindowsTestMatch(1207, 50),
            createWindowsTestMatch(1419, 50) };
    assertSearchMatches(expected, createQuery("InnerSub", IJavaSearchConstants.TYPE));

    // Search for type with different casing
    expected = new Match[] { createWindowsTestMatch(840, 50), createWindowsTestMatch(1207, 50),
            createWindowsTestMatch(1419, 50) };
    assertSearchMatches(expected, createQuery("innersub", IJavaSearchConstants.TYPE));

    // Search for type with different casing with case-sensitive enabled
    QuerySpecification query = new PatternQuerySpecification("innersub", IJavaSearchConstants.TYPE, true,
            IJavaSearchConstants.REFERENCES, WORKSPACE_SCOPE, "");
    assertSearchMatches(NO_MATCHES, query);

    // Search for field references
    assertSearchMatch(createWindowsTestMatch(990, 5), createQuery("keith", IJavaSearchConstants.FIELD));

    // Search for method references using * wildcard
    expected = new Match[] { createWindowsTestMatch(1174, 5), createWindowsTestMatch(1259, 5),
            createWindowsTestMatch(1340, 8) };
    assertSearchMatches(expected, createQuery("sayH*", IJavaSearchConstants.METHOD));

    // Search for method references using ? wildcard
    expected = new Match[] { createWindowsTestMatch(1174, 5), createWindowsTestMatch(1259, 5) };
    assertSearchMatches(expected, createQuery("sayH?", IJavaSearchConstants.METHOD));

    // Search for constructor references with qualified type name and parameters
    assertSearchMatch(createWindowsTestMatch(892, 3),
            createQuery("com.hello.client.JavaQueryParticipantTest.InnerSub.InnerSub(String)",
                    IJavaSearchConstants.CONSTRUCTOR));
}

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

License:Open Source License

/**
 * Find the IMethod in the workspace corresponding to a method signature.
 * //from w ww . j av  a 2s.com
 * This doesn't work for elements declared in inner classes. It's possible this is a 3.2 bug fixed in 3.3
 * 
 * @return null if not found
 */
@Deprecated
public static IMethod findJavaMethodInWorkspaceBrokenForInnerClasses(String methodSig) {
    // dammit ... this doesn't work for inner classes.

    System.err.println("Search for " + methodSig);
    SearchPattern p = SearchPattern.createPattern(methodSig, IJavaSearchConstants.METHOD,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    SearchEngine engine = new SearchEngine();
    final Collection<IJavaElement> kludge = HashSetFactory.make();
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            kludge.add((IJavaElement) match.getElement());
        }

    };
    try {
        engine.search(p, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                requestor, null);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    if (kludge.size() == 1) {
        return (IMethod) kludge.iterator().next();
    } else {
        System.err.println("RETURNED " + kludge.size() + " " + kludge);
        return null;
    }
}

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

License:Open Source License

/**
 * Use the search engine to find all methods in a java element
 *///from   w w  w  . j  a va  2 s . c  o m
public static Collection<IMethod> findMethods(IJavaElement elt) {

    if (elt instanceof ICompilationUnit) {
        Collection<IMethod> result = HashSetFactory.make();
        for (IType type : getClasses((ICompilationUnit) elt)) {
            try {
                for (IMethod m : type.getMethods()) {
                    result.add(m);
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
        return result;
    } else {
        final Collection<IMethod> result = HashSetFactory.make();
        SearchPattern p = SearchPattern.createPattern("*", IJavaSearchConstants.METHOD,
                IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
        SearchPattern p2 = SearchPattern.createPattern("*", IJavaSearchConstants.CONSTRUCTOR,
                IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { elt },
                IJavaSearchScope.SOURCES);
        SearchEngine engine = new SearchEngine();
        SearchRequestor requestor = new SearchRequestor() {
            @Override
            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                if (match.getElement() instanceof IMethod) {
                    result.add((IMethod) match.getElement());
                }
            }
        };
        try {
            engine.search(p, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                    requestor, null);
            engine.search(p2, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                    requestor, null);
        } catch (CoreException e) {
            e.printStackTrace();
        }

        return result;
    }
}

From source file:com.liferay.ide.portlet.ui.editor.PortletURLHyperlinkDetector.java

License:Open Source License

private IMethod[] findPortletMethods(IDocument document, String nameValue) {
    IMethod[] retval = null;/*from w w  w  .j av  a  2s.co  m*/

    final IFile file = DOMUtils.getFile(document);

    if (file != null && file.exists()) {
        final IJavaProject project = JavaCore.create(file.getProject());

        if (project != null && project.exists()) {
            try {
                final IType portlet = project.findType("javax.portlet.Portlet");

                if (portlet != null) {
                    final List<IMethod> methods = new ArrayList<IMethod>();
                    final SearchRequestor requestor = new ActionMethodCollector(methods);

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

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

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

                    retval = methods.toArray(new IMethod[0]);
                }
            } catch (JavaModelException e) {
            } catch (CoreException 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 .ja  v a 2s .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:edu.brown.cs.bubbles.bedrock.BedrockCall.java

License:Open Source License

/********************************************************************************/

void getCallPath(String proj, String src, String tgt, boolean shortest, int lvls, IvyXmlWriter xw)
        throws BedrockException {
    IProject ip = our_plugin.getProjectManager().findProject(proj);
    IJavaProject ijp = JavaCore.create(ip);
    if (lvls < 0)
        lvls = MAX_LEVELS;/*from   ww  w .java2 s  .c  o  m*/

    IJavaElement[] pelt = new IJavaElement[] { ijp };
    int incl = IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES
            | IJavaSearchScope.REFERENCED_PROJECTS;
    IJavaSearchScope scp = SearchEngine.createJavaSearchScope(pelt, incl);

    SearchEngine se = new SearchEngine();
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };

    SearchPattern p1 = SearchPattern.createPattern(src, IJavaSearchConstants.METHOD,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
    SearchPattern p1a = SearchPattern.createPattern(fixConstructor(src), IJavaSearchConstants.CONSTRUCTOR,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
    if (p1 == null || p1a == null)
        throw new BedrockException("Illegal source pattern " + src);

    SearchPattern p2 = SearchPattern.createPattern(tgt, IJavaSearchConstants.METHOD,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
    SearchPattern p2a = SearchPattern.createPattern(fixConstructor(tgt), IJavaSearchConstants.CONSTRUCTOR,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
    if (p2 == null || p2a == null)
        throw new BedrockException("Illegal target pattern " + tgt);

    SetHandler sh = new SetHandler();
    try {
        se.search(p1, parts, scp, sh, null);
        BedrockPlugin.logD("CALL: Source A: " + sh.getSize() + " " + p1);
        if (sh.isEmpty())
            se.search(p1a, parts, scp, sh, null);
        BedrockPlugin.logD("CALL: Source B: " + sh.getSize() + " " + p1a);
    } catch (CoreException e) {
        throw new BedrockException("Problem doing call search 1: " + e, e);
    }

    SetHandler th = new SetHandler();
    try {
        se.search(p2, parts, scp, th, null);
        BedrockPlugin.logD("CALL: Target A: " + th.getSize() + " " + p2);
        if (th.isEmpty())
            se.search(p2a, parts, scp, th, null);
        BedrockPlugin.logD("CALL: Target B: " + th.getSize() + " " + p2a);
    } catch (CoreException e) {
        throw new BedrockException("Problem doing call search 2: " + e, e);
    }

    Map<IMethod, CallNode> nodes = new HashMap<IMethod, CallNode>();
    Queue<IMethod> workqueue = new LinkedList<IMethod>();
    for (IMethod je : th.getElements()) {
        CallNode cn = new CallNode(je, 0);
        cn.setTarget();
        nodes.put(je, cn);
        workqueue.add(je);
    }

    while (!workqueue.isEmpty()) {
        IMethod je = workqueue.remove();
        CallNode cn = nodes.get(je);
        if (cn.isDone())
            continue;
        cn.markDone();

        BedrockPlugin.logD("CALL: WORK ON " + je.getKey() + " " + cn.getLevel() + " " + sh.contains(je));

        if (shortest && sh.contains(je))
            break;
        int lvl = cn.getLevel() + 1;
        if (lvl > lvls)
            continue;

        String nm = je.getElementName();
        if (nm == null)
            continue;
        String cnm = je.getDeclaringType().getFullyQualifiedName();
        if (cnm != null)
            nm = cnm.replace("$", ".") + "." + nm;
        nm += "(";
        String[] ptyps = je.getParameterTypes();
        for (int i = 0; i < ptyps.length; ++i) {
            if (i > 0)
                nm += ",";
            nm += IvyFormat.formatTypeName(ptyps[i]);
        }
        nm += ")";

        SearchPattern p3;
        try {
            BedrockPlugin.logD("CALL: Search for: " + nm + " " + je.isConstructor());
            if (je.isConstructor()) {
                String nm1 = fixConstructor(nm);
                p3 = SearchPattern.createPattern(nm1, IJavaSearchConstants.CONSTRUCTOR,
                        IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH);
            } else {
                p3 = SearchPattern.createPattern(nm, IJavaSearchConstants.METHOD,
                        IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH);
            }

            CallHandler ch = new CallHandler(je, workqueue, nodes, lvl);

            se.search(p3, parts, scp, ch, null);
        } catch (CoreException e) {
            throw new BedrockException("Problem doing call search e: " + e, e);
        }
    }

    // TODO: restrict to single path if shortest is set

    xw.begin("PATH");
    for (IMethod je : sh.getElements()) {
        CallNode cn = nodes.get(je);
        if (cn == null)
            continue;
        Set<IMethod> done = new HashSet<IMethod>();
        cn.output(xw, done, nodes);
    }
    xw.end("PATH");
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockJava.java

License:Open Source License

/********************************************************************************/

void handleJavaSearch(String proj, String bid, String patstr, String foritems, boolean defs, boolean refs,
        boolean impls, boolean equiv, boolean exact, boolean system, IvyXmlWriter xw) throws BedrockException {
    IJavaProject ijp = getJavaProject(proj);

    our_plugin.waitForEdits();//  ww  w  .j av a 2 s  . co m

    int forflags = 0;
    if (foritems == null)
        forflags = IJavaSearchConstants.TYPE;
    else if (foritems.equalsIgnoreCase("CLASS"))
        forflags = IJavaSearchConstants.CLASS;
    else if (foritems.equalsIgnoreCase("INTERFACE"))
        forflags = IJavaSearchConstants.INTERFACE;
    else if (foritems.equalsIgnoreCase("ENUM"))
        forflags = IJavaSearchConstants.ENUM;
    else if (foritems.equalsIgnoreCase("ANNOTATION"))
        forflags = IJavaSearchConstants.ANNOTATION_TYPE;
    else if (foritems.equalsIgnoreCase("CLASS&ENUM"))
        forflags = IJavaSearchConstants.CLASS_AND_ENUM;
    else if (foritems.equalsIgnoreCase("CLASS&INTERFACE"))
        forflags = IJavaSearchConstants.CLASS_AND_INTERFACE;
    else if (foritems.equalsIgnoreCase("TYPE"))
        forflags = IJavaSearchConstants.TYPE;
    else if (foritems.equalsIgnoreCase("FIELD"))
        forflags = IJavaSearchConstants.FIELD;
    else if (foritems.equalsIgnoreCase("METHOD"))
        forflags = IJavaSearchConstants.METHOD;
    else if (foritems.equalsIgnoreCase("CONSTRUCTOR"))
        forflags = IJavaSearchConstants.CONSTRUCTOR;
    else if (foritems.equalsIgnoreCase("PACKAGE"))
        forflags = IJavaSearchConstants.PACKAGE;
    else if (foritems.equalsIgnoreCase("FIELDWRITE"))
        forflags = IJavaSearchConstants.FIELD | IJavaSearchConstants.WRITE_ACCESSES;
    else if (foritems.equalsIgnoreCase("FIELDREAD"))
        forflags = IJavaSearchConstants.FIELD | IJavaSearchConstants.READ_ACCESSES;
    else
        forflags = IJavaSearchConstants.TYPE;

    int limit = 0;
    if (defs && refs)
        limit = IJavaSearchConstants.ALL_OCCURRENCES;
    else if (defs)
        limit = IJavaSearchConstants.DECLARATIONS;
    else if (refs)
        limit = IJavaSearchConstants.REFERENCES;
    else if (impls)
        limit = IJavaSearchConstants.IMPLEMENTORS;

    int mrule = SearchPattern.R_PATTERN_MATCH;
    if (equiv)
        mrule = SearchPattern.R_EQUIVALENT_MATCH;
    else if (exact)
        mrule = SearchPattern.R_EXACT_MATCH;

    SearchPattern pat = SearchPattern.createPattern(patstr, forflags, limit, mrule);
    if (pat == null) {
        throw new BedrockException(
                "Invalid java search pattern `" + patstr + "' " + forflags + " " + limit + " " + mrule);
    }

    FindFilter filter = null;
    if (forflags == IJavaSearchConstants.METHOD) {
        String p = patstr;
        int idx = p.indexOf("(");
        if (idx > 0)
            p = p.substring(0, idx);
        idx = p.lastIndexOf(".");
        if (idx > 0) {
            if (defs)
                filter = new ClassFilter(p.substring(0, idx));
        }
    }

    // TODO: create scope for only user's items
    IJavaElement[] pelt;
    if (ijp != null)
        pelt = new IJavaElement[] { ijp };
    else
        pelt = getAllProjects();

    ICompilationUnit[] working = getWorkingElements(pelt);
    for (ICompilationUnit xcu : working) {
        try {
            BedrockPlugin.logD("WORK WITH " + xcu.isWorkingCopy() + " " + xcu.getPath() + xcu.getSourceRange());
        } catch (JavaModelException e) {
            BedrockPlugin.logD("WORK WITH ERROR: " + e);
        }
    }

    IJavaSearchScope scp = null;
    int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
    if (system) {
        fg |= IJavaSearchScope.SYSTEM_LIBRARIES | IJavaSearchScope.APPLICATION_LIBRARIES;
        scp = SearchEngine.createWorkspaceScope();
    } else {
        scp = SearchEngine.createJavaSearchScope(pelt, fg);
    }

    SearchEngine se = new SearchEngine(working);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, filter, system);

    BedrockPlugin.logD("BEGIN SEARCH " + pat);
    BedrockPlugin.logD("SEARCH SCOPE " + system + " " + fg + " " + scp);

    try {
        se.search(pat, parts, scp, fh, null);
    } catch (Throwable e) {
        throw new BedrockException("Problem doing Java search: " + e, e);
    }
}