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

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

Introduction

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

Prototype

int DECLARATIONS

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

Click Source Link

Document

The search result is a declaration.

Usage

From source file:org.eclim.plugin.jdt.command.src.JavaCommand.java

License:Open Source License

private String findMainClass(IJavaProject javaProject) throws Exception {
    final String projectPath = ProjectUtils.getPath(javaProject.getProject());
    final ArrayList<IMethod> methods = new ArrayList<IMethod>();
    int context = IJavaSearchConstants.DECLARATIONS;
    int type = IJavaSearchConstants.METHOD;
    int matchType = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });
    SearchPattern pattern = SearchPattern.createPattern("main(String[])", type, context, matchType);
    SearchRequestor requestor = new SearchRequestor() {
        public void acceptSearchMatch(SearchMatch match) {
            if (match.getAccuracy() != SearchMatch.A_ACCURATE) {
                return;
            }/*from  ww w.j a  va2 s  . co m*/

            IPath location = match.getResource().getRawLocation();
            if (location == null) {
                return;
            }

            String path = location.toOSString().replace('\\', '/');
            if (!path.toLowerCase().startsWith(projectPath.toLowerCase())) {
                return;
            }

            IJavaElement element = (IJavaElement) match.getElement();
            if (element.getElementType() != IJavaElement.METHOD) {
                return;
            }

            IMethod method = (IMethod) element;
            String[] params = method.getParameterTypes();
            if (params.length != 1) {
                return;
            }
            methods.add(method);
        }
    };

    if (pattern != null) {
        SearchEngine engine = new SearchEngine();
        SearchParticipant[] participants = new SearchParticipant[] {
                SearchEngine.getDefaultSearchParticipant() };
        engine.search(pattern, participants, scope, requestor, null);

        // if we found only 1 result, we can use it.
        if (methods.size() == 1) {
            IMethod method = methods.get(0);
            ICompilationUnit cu = method.getCompilationUnit();
            IPackageDeclaration[] packages = cu.getPackageDeclarations();
            if (packages != null && packages.length > 0) {
                return packages[0].getElementName() + "." + cu.getElementName();
            }
            return cu.getElementName();
        }
    }
    return null;
}

From source file:org.eclim.plugin.jdt.util.TypeUtils.java

License:Open Source License

/**
 * Find types by the supplied fully qualified name or unqualified class name.
 *
 * @param javaProject The java project to be searched.
 * @param name The name to search./*ww w. ja  v  a2s  .  c  o  m*/
 *
 * @return A possibly empty array of IType results found.
 */
public static IType[] findTypes(IJavaProject javaProject, String name) throws Exception {
    SearchPattern pattern = SearchPattern.createPattern(name, IJavaSearchConstants.TYPE,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });
    SearchRequestor requestor = new SearchRequestor();
    SearchEngine engine = new SearchEngine();
    SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    engine.search(pattern, participants, scope, requestor, null);

    ArrayList<IType> types = new ArrayList<IType>();
    if (requestor.getMatches().size() > 0) {
        for (SearchMatch match : requestor.getMatches()) {
            if (match.getAccuracy() != SearchMatch.A_ACCURATE) {
                continue;
            }
            IJavaElement element = (IJavaElement) match.getElement();
            if (element.getElementType() == IJavaElement.TYPE) {
                types.add((IType) element);
            }
        }
    }
    return types.toArray(new IType[types.size()]);
}

From source file:org.eclipse.acceleo.internal.ide.ui.launching.AcceleoMainMethodSearchEngine.java

License:Open Source License

/**
 * Searches for all main methods in the given scope. Valid styles are
 * IJavaElementSearchConstants.CONSIDER_BINARIES and IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
 * /*from ww w  .  jav  a 2 s. c  om*/
 * @param pm
 *            progress monitor
 * @param scope
 *            search scope
 * @param includeSubtypes
 *            whether to consider types that inherit a main method
 * @return main methods types
 */
public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, boolean includeSubtypes) {
    final int v100 = 100;
    final int v25 = 25;
    final int v75 = 75;
    pm.beginTask(AcceleoUIMessages.getString("AcceleoMainMethodSearchEngine.SearchingForMainTypes"), 100); //$NON-NLS-1$
    int searchTicks = v100;
    if (includeSubtypes) {
        searchTicks = v25;
    }

    SearchPattern pattern = SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD, //$NON-NLS-1$
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    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 e) {
        AcceleoUIActivator.getDefault().getLog().log(e.getStatus());
    }

    List<IType> result = collector.getResult();
    if (includeSubtypes) {
        IProgressMonitor subtypesMonitor = new SubProgressMonitor(pm, v75);
        subtypesMonitor.beginTask(
                AcceleoUIMessages.getString("AcceleoMainMethodSearchEngine.SearchingForMainTypes"), //$NON-NLS-1$
                result.size());
        Set<IType> set = addSubtypes(result, subtypesMonitor, scope);
        return set.toArray(new IType[set.size()]);
    }
    return result.toArray(new IType[result.size()]);
}

From source file:org.eclipse.ajdt.core.tests.search.ITDAwareDeclarationSearchTests.java

License:Open Source License

public void testITDSearchFieldDeclaration1() throws Exception {
    ICompilationUnit unit = createCU("Aspect.aj", "aspect Aspect {\n int Java.xxx = 0; }");
    createCU("Java.java", "class Java { }");

    IntertypeElement itd = findFirstITD(unit);

    List<SearchMatch> matches = findSearchMatches(itd, this.getName(), IJavaSearchConstants.DECLARATIONS);
    assertExpectedNumberOfMatches(1, matches);
    assertDeclarationMatches(itd, matches);
}

From source file:org.eclipse.ajdt.core.tests.search.ITDAwareDeclarationSearchTests.java

License:Open Source License

public void testITDSearchFieldDeclaration2() throws Exception {
    ICompilationUnit unit = createCU("Aspect.aj", "aspect Aspect {\n int xxx = 0;\n int Java.xxx = 0; }");
    createCU("Java.java", "class Java { }");
    createCU("Other.java", "class Other extends Java { int xxx = 0; }");

    IntertypeElement itd = findFirstITD(unit);

    List<SearchMatch> matches = findSearchMatches(itd, this.getName(), IJavaSearchConstants.DECLARATIONS);
    assertExpectedNumberOfMatches(1, matches);
    assertDeclarationMatches(itd, matches);
}

From source file:org.eclipse.ajdt.core.tests.search.ITDAwareDeclarationSearchTests.java

License:Open Source License

public void testITDSearchFieldDeclaration3() throws Exception {
    createCU("Aspect.aj", "aspect Aspect {\n int xxx = 0;\n int Java.xxx = 0; }");
    createCU("Java.java", "class Java { }");
    ICompilationUnit unit = createCU("Other.java", "class Other extends Java { int xxx = 0; }");

    IMember field = findFirstChild(unit);

    List<SearchMatch> matches = findSearchMatches(field, this.getName(), IJavaSearchConstants.DECLARATIONS);
    assertExpectedNumberOfMatches(1, matches);
    assertDeclarationMatches(field, matches);
}

From source file:org.eclipse.ajdt.core.tests.search.ITDAwareDeclarationSearchTests.java

License:Open Source License

public void testITDSearchFieldDeclaration4() throws Exception {
    ICompilationUnit unit = createCU("Aspect.aj", "aspect Aspect {\n int xxx = 0;\n int Java.xxx = 0; }");
    createCU("Java.java", "class Java { }");
    createCU("Other.java", "class Other extends Java { int xxx = 0; }");

    IMember field = findFirstChild(unit);

    List<SearchMatch> matches = findSearchMatches(field, this.getName(), IJavaSearchConstants.DECLARATIONS);
    assertExpectedNumberOfMatches(1, matches);
    assertDeclarationMatches(field, matches);
}

From source file:org.eclipse.ajdt.core.tests.search.ITDAwareDeclarationSearchTests.java

License:Open Source License

public void testITDSearchMethodDeclaration1() throws Exception {
    ICompilationUnit unit = createCU("Aspect.aj", "aspect Aspect {\n void Java.xxx() { } }");
    createCU("Java.java", "class Java { }");

    IntertypeElement itd = findFirstITD(unit);

    List<SearchMatch> matches = findSearchMatches(itd, this.getName(), IJavaSearchConstants.DECLARATIONS);
    assertExpectedNumberOfMatches(1, matches);
    assertDeclarationMatches(itd, matches);
}

From source file:org.eclipse.ajdt.core.tests.search.ITDAwareDeclarationSearchTests.java

License:Open Source License

public void testITDSearchMethodDeclaration2() throws Exception {
    ICompilationUnit unit = createCU("Aspect.aj", "aspect Aspect {\n void Java.xxx() { } }");
    createCU("Java.java", "class Java { }");
    ICompilationUnit other = createCU("Other.java", "class Other extends Java { void xxx() { } }");

    IntertypeElement itd = findFirstITD(unit);
    IMember method = findFirstChild(other);

    List<SearchMatch> matches = findSearchMatches(itd, this.getName(), IJavaSearchConstants.DECLARATIONS);
    assertExpectedNumberOfMatches(2, matches);
    assertDeclarationMatches(itd, matches);
    assertDeclarationMatches(method, matches);
}

From source file:org.eclipse.ajdt.core.tests.search.ITDAwareDeclarationSearchTests.java

License:Open Source License

public void testITDSearchMethodDeclaration3() throws Exception {
    createCU("Other.java", "abstract class Other { abstract void xxx(); }");
    ICompilationUnit unit = createCU("Aspect.aj", "aspect Aspect {\n void Java.xxx() { } }");
    createCU("Java.java", "class Java extends Other { }");

    IntertypeElement itd = findFirstITD(unit);

    List<SearchMatch> matches = findSearchMatches(itd, this.getName(), IJavaSearchConstants.DECLARATIONS);
    assertExpectedNumberOfMatches(1, matches);
    assertDeclarationMatches(itd, matches);
}