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:org.eclipse.wst.xml.search.editor.internal.jdt.search.XMLReferenceJavaSearchParticipant.java

License:Open Source License

public void search(final ISearchRequestor requestor, final QuerySpecification query, IProgressMonitor monitor)
        throws CoreException {
    if (!isValid(query)) {
        return;/* w  w  w .  jav a  2 s .c  o m*/
    }
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.beginTask("", 11); //$NON-NLS-1$
    try {
        // Get Java project, class name and method name to search
        IJavaProject javaProject = null;
        String className = null;
        String methodName = null;
        if (query instanceof ElementQuerySpecification) {
            // Java Search launched with Java Editor with Ctrl+Shift+G
            ElementQuerySpecification elementQuery = (ElementQuerySpecification) query;
            IJavaElement element = elementQuery.getElement();
            if (element instanceof IMember) {
                IMember member = (IMember) element;
                javaProject = member.getJavaProject();
                if (member.getElementType() == IJavaElement.TYPE) {
                    // Java class
                    IType type = (IType) member;
                    className = type.getFullyQualifiedName('.');
                } else if (member.getElementType() == IJavaElement.METHOD) {
                    IMethod method = (IMethod) member;
                    methodName = method.getElementName();
                    className = method.getDeclaringType().getFullyQualifiedName('.');
                }
            } else {
                return;
            }
        } else if (query instanceof PatternQuerySpecification) {
            // Java Search launched with Java UI Dialog Search
            PatternQuerySpecification patternQuery = (PatternQuerySpecification) query;
            if (patternQuery.getSearchFor() == IJavaSearchConstants.METHOD) {
                methodName = patternQuery.getPattern();
            } else {
                className = patternQuery.getPattern();
            }
        }
        if (monitor.isCanceled()) {
            return;
        }
        monitor.worked(1);
        if (className == null && methodName == null) {
            return;
        }
        ToType toType = (!StringUtils.isEmpty(methodName) ? ToType.JAVA_METHOD : ToType.JAVA);
        Collection<IXMLReference> references = XMLReferencesManager.getInstance()
                .getXMLReferencesForToType(toType);
        if (references.size() < 1) {
            return;
        }
        XMLReferencesIndexManager.getDefault().flushIndexedFiles(javaProject);
        searchXMLReferences(query.getScope(), requestor, className, methodName, javaProject, references, toType,
                new SubProgressMonitor(monitor, 7));
    } finally {
        monitor.done();
    }
}

From source file:org.eclipse.wst.xml.search.editor.internal.jdt.search.XMLReferenceJavaSearchParticipant.java

License:Open Source License

/**
 * Determines if the current query should be considered or not
 * /*from   w  w w .j  a  va 2  s.c  o m*/
 * @param query
 *            the current query
 * @return true if the query should be considered, false otherwise
 */
private boolean isValid(QuerySpecification query) {
    switch (query.getLimitTo()) {
    case IJavaSearchConstants.REFERENCES:
    case IJavaSearchConstants.ALL_OCCURRENCES: {
        break;
    }
    default: {
        return false;
    }
    }
    if (query instanceof ElementQuerySpecification) {
        IJavaElement element = ((ElementQuerySpecification) query).getElement();
        return element.getElementType() == IJavaElement.TYPE || element.getElementType() == IJavaElement.METHOD;
    }
    if (query instanceof PatternQuerySpecification) {
        PatternQuerySpecification patternQuery = (PatternQuerySpecification) query;
        switch (patternQuery.getSearchFor()) {
        case IJavaSearchConstants.UNKNOWN:
        case IJavaSearchConstants.TYPE:
        case IJavaSearchConstants.CLASS:
        case IJavaSearchConstants.CLASS_AND_INTERFACE:
        case IJavaSearchConstants.METHOD: {
            return true;
        }
        }
    }
    return false;
}

From source file:org.fusesource.ide.camel.editor.globalconfiguration.beans.BeanConfigUtil.java

License:Open Source License

public boolean hasMethod(String methodName, IType type) {
    if (type == null) {
        return false;
    }/*from w w w.  ja  v  a2s . c  o m*/

    SearchPattern pattern = SearchPattern.createPattern(methodName, IJavaSearchConstants.METHOD,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);

    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { type });

    CountingSearchRequestor matchCounter = new CountingSearchRequestor();

    SearchEngine search = new SearchEngine();
    try {
        search.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                matchCounter, null);
    } catch (CoreException ce) {
        CamelEditorUIActivator.pluginLog()
                .logError(UIMessages.beanConfigUtilMethodSelectionErrorNoTypeFound + ce.getMessage(), ce);
    }

    return matchCounter.getNumMatch() > 0;
}

From source file:org.jboss.byteman.eclipse.validation.BytemanJavaValidator.java

License:Open Source License

@Check(CheckType.FAST)
public void checkMethodClause(EventMethod eventMethod) {
    // delete any previous cache of possible trigger methods
    getContext().remove(RULE_METHOD_KEY);

    String methodName = eventMethod.getName();
    // we can only check the method if we already know the possible classes
    List<TypeSearchResult> types = (List<TypeSearchResult>) getContext().get(RULE_CLASS_KEY);
    if (types == null) {
        error("unknown trigger method " + methodName, BytemanPackage.eINSTANCE.getEventMethod_Name());
    } else if (methodName.equals("<clinit>")) {
        // cannot verify presence of <clinit> via search
        // just check it has no arguments
        ParameterTypes parameterTypes = eventMethod.getParameterTypes();
        if (parameterTypes != null) {
            EList<String> paramTypeNames = parameterTypes.getParamTypeNames();
            int paramTypeCount = paramTypeNames.size();

            if (paramTypeCount != 0) {
                error("invalid parameter types for class initializer" + methodName,
                        BytemanPackage.eINSTANCE.getEventMethod_ParameterTypes());
            }/*w ww. j a v  a2 s. c  om*/
        }
    } else {
        boolean isConstructor = methodName.equals("<init>");
        // look for methods on each of the possible types
        ParameterTypes parameterTypes = eventMethod.getParameterTypes();
        EList<String> paramTypeNames;
        int paramTypeCount;
        if (parameterTypes != null) {
            paramTypeNames = parameterTypes.getParamTypeNames();
            paramTypeCount = paramTypeNames.size();
        } else {
            paramTypeNames = null;
            // -1 indicates any method with the relevant signature will do
            // whereas 0 indicates an empty parameter list ()
            paramTypeCount = -1;
        }

        final List<MethodSearchResult> methods = new ArrayList<MethodSearchResult>();
        // accumulate matching methods

        SearchEngine searchEngine = new SearchEngine();
        IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
        SearchParticipant[] participants = new SearchParticipant[] {
                SearchEngine.getDefaultSearchParticipant() };

        for (TypeSearchResult result : types) {
            String typeName = result.name.replace('$', '.');

            // now build type qualified method name
            StringBuilder builder = new StringBuilder();
            builder.append(typeName);
            // append method name to type name except when it is a constructor or class initializer
            if (!isConstructor) {
                builder.append('.');
                builder.append(methodName);
            }
            if (paramTypeCount >= 0) {
                String separator = "";
                builder.append("(");
                for (int i = 0; i < paramTypeCount; i++) {
                    builder.append(separator);
                    String paramTypeName = paramTypeNames.get(i);
                    // ho hum eclipse doesn't like $ to separate embedded types
                    if (paramTypeName.indexOf('$') > 0) {
                        paramTypeName = paramTypeName.replace('$', '.');
                    }
                    builder.append(paramTypeName);
                    separator = ",";
                }
                builder.append(")");
            }
            final String stringPattern = builder.toString();
            int searchFor = (isConstructor ? IJavaSearchConstants.CONSTRUCTOR : IJavaSearchConstants.METHOD);
            int limitTo = IJavaSearchConstants.DECLARATIONS;
            int matchType = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
            SearchPattern pattern = SearchPattern.createPattern(stringPattern, searchFor, limitTo, matchType);
            final TypeSearchResult typeResult = result;
            SearchRequestor requestor = new SearchRequestor() {
                @Override
                public void acceptSearchMatch(SearchMatch match) throws CoreException {
                    // only accept if we have an accurate match
                    if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
                        MethodSearchResult methodResult = new MethodSearchResult(stringPattern, typeResult,
                                match);
                        methods.add(methodResult);
                    }
                }
            };
            try {
                searchEngine.search(pattern, participants, scope, requestor, null);
            } catch (CoreException e) {
                // TODO : ho hum not sure if this will happen when we have
                // no such method or because something is wrong with paths etc
                // but just ignore for now
            }
        }

        // if we have no matches then plant an error

        if (methods.isEmpty()) {
            error("unknown trigger method " + methodName, BytemanPackage.eINSTANCE.getEventMethod_Name());
        } else {
            // cache details of potential trigger methods for current rule
            getContext().put(RULE_METHOD_KEY, methods);
        }
    }
}

From source file:org.jboss.tools.common.text.ext.hyperlink.ClassMethodHyperlink.java

License:Open Source License

protected IJavaElement searchForClassMethod(IJavaProject javaProject, String className, String methodName) {
    // Get the search pattern
    SearchPattern pattern = SearchPattern.createPattern(className + "." + methodName, //$NON-NLS-1$
            IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

    // Get the search scope
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });

    final List<SearchMatch> matches = new ArrayList<SearchMatch>();
    // Get the search requestor
    SearchRequestor requestor = new SearchRequestor() {
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            matches.add(match);/*from   w  ww  .  j a va  2s.c  om*/
        }
    };

    // Search
    SearchEngine searchEngine = new SearchEngine();
    try {
        searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                scope, requestor, null);
    } catch (CoreException ex) {
        // ignore
    }
    for (Iterator i = matches.iterator(); i != null && i.hasNext();) {
        return (IJavaElement) ((SearchMatch) i.next()).getElement();
    }
    return null;
}

From source file:org.key_project.sed.key.ui.jdt.AllOperationsSearchEngine.java

License:Open Source License

/**
 * Searches all methods and constructors.
 * @param pm The {@link IProgressMonitor} to use.
 * @param scope The {@link IJavaSearchScope} to search in.
 * @return The found {@link IMethod}s.//  ww  w .j a  v a 2s  .  c  om
 */
public IMethod[] searchOperations(IProgressMonitor pm, IJavaSearchScope scope) {
    pm.beginTask("Searching for methods...", 100);
    int searchTicks = 100;
    SearchPattern constructorPattern = SearchPattern.createPattern("*", IJavaSearchConstants.CONSTRUCTOR,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    SearchPattern methodPattern = SearchPattern.createPattern("*", IJavaSearchConstants.METHOD,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    SearchPattern pattern = SearchPattern.createOrPattern(constructorPattern, methodPattern);
    SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    MethodCollector collector = new MethodCollector();
    IProgressMonitor searchMonitor = new SubProgressMonitor(pm, searchTicks);
    try {
        new SearchEngine().search(pattern, participants, scope, collector, searchMonitor);
    } catch (CoreException ce) {
        LogUtil.getLogger().logError(ce);
    }
    List<IMethod> result = collector.getResult();
    return result.toArray(new IMethod[result.size()]);
}

From source file:org.teavm.eclipse.ui.MainClassSelectionDialog.java

License:Apache License

@Override
protected SearchPattern createSearchPattern(String text) {
    return SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
}