Example usage for org.eclipse.jdt.core.search SearchEngine createWorkspaceScope

List of usage examples for org.eclipse.jdt.core.search SearchEngine createWorkspaceScope

Introduction

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

Prototype

public static IJavaSearchScope createWorkspaceScope() 

Source Link

Document

Returns a Java search scope with the workspace as the only limit.

Usage

From source file:com.technophobia.substeps.junit.action.OpenEditorAction.java

License:Open Source License

protected final IType findType(final IJavaProject project, final String testClassName) {
    final IType[] result = { null };
    final String dottedName = testClassName.replace('$', '.'); // for nested
    // classes...
    try {//from w w  w  .j  a  v a  2s.  c  o  m
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
            @Override
            public void run(final IProgressMonitor monitor)
                    throws InvocationTargetException, InterruptedException {
                try {
                    if (project != null) {
                        result[0] = internalFindType(project, dottedName, new HashSet<IJavaProject>(), monitor);
                    }
                    if (result[0] == null) {
                        final int lastDot = dottedName.lastIndexOf('.');
                        final TypeNameMatchRequestor nameMatchRequestor = new TypeNameMatchRequestor() {
                            @Override
                            public void acceptTypeNameMatch(final TypeNameMatch match) {
                                result[0] = match.getType();
                            }
                        };
                        new SearchEngine().searchAllTypeNames(
                                lastDot >= 0 ? dottedName.substring(0, lastDot).toCharArray() : null,
                                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
                                (lastDot >= 0 ? dottedName.substring(lastDot + 1) : dottedName).toCharArray(),
                                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
                                IJavaSearchConstants.TYPE, SearchEngine.createWorkspaceScope(),
                                nameMatchRequestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
                    }
                } catch (final JavaModelException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (final InvocationTargetException e) {
        FeatureRunnerPlugin.log(e);
    } catch (final InterruptedException e) {
        // user cancelled
    }
    return result[0];
}

From source file:com.vmware.vfabric.ide.eclipse.tcserver.insight.internal.ui.link.JavaElementLocationHandler.java

License:Open Source License

private static IType[] findTypes(String typeName, IProgressMonitor monitor) throws CoreException {

    final List<IType> results = new ArrayList<IType>();

    SearchRequestor collector = new SearchRequestor() {
        @Override/*w  w w.j a  v  a  2 s. c o  m*/
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IType) {
                results.add((IType) element);
            }
        }
    };

    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(typeName, IJavaSearchConstants.TYPE,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            SearchEngine.createWorkspaceScope(), collector, monitor);

    return results.toArray(new IType[results.size()]);
}

From source file:de.gebit.integrity.eclipse.views.IntegrityTestRunnerView.java

License:Open Source License

private void jumpToJavaMethod(String aJavaClassAndMethod) {
    Matcher tempMatcher = Pattern.compile("([^#]*)\\.([^#]*)#(.*)").matcher(aJavaClassAndMethod);
    if (tempMatcher.matches()) {
        final String tempPackageName = tempMatcher.group(1);
        String tempClassName = tempMatcher.group(2);
        final String tempMethodName = tempMatcher.group(3);

        SearchPattern tempPattern = SearchPattern.createPattern(tempClassName, IJavaSearchConstants.TYPE,
                IJavaSearchConstants.DECLARATIONS,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
        IJavaSearchScope tempScope = SearchEngine.createWorkspaceScope();
        SearchRequestor tempRequestor = new SearchRequestor() {

            @Override//  w  w w . j av a 2s  .com
            public void acceptSearchMatch(SearchMatch aMatch) throws CoreException {
                IType tempType = (IType) aMatch.getElement();
                if (tempPackageName.equals(tempType.getPackageFragment().getElementName())) {
                    for (IMethod tempMethod : tempType.getMethods()) {
                        if (tempMethodName.equals(tempMethod.getElementName())) {
                            JavaUI.openInEditor(tempMethod);
                            return;
                        }
                    }
                }
            }
        };

        SearchEngine tempSearchEngine = new SearchEngine();
        try {
            tempSearchEngine.search(tempPattern,
                    new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, tempScope,
                    tempRequestor, null);
        } catch (CoreException exc) {
            exc.printStackTrace();
        }
    }
}

From source file:de.jcup.egradle.eclipse.gradleeditor.GradleResourceHyperlink.java

License:Apache License

@Override
public void open() {
    String[] packageNames = fetchImportedPackages(fullText);

    Shell shell = EGradleUtil.getActiveWorkbenchShell();
    if (shell == null) {
        return;/*from   ww w .j  a v a2s .c om*/
    }
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    JDTDataAccess access = JDTDataAccess.SHARED;

    List<String> typesFound = access.scanForJavaType(resourceName, scope, packageNames);

    SelectionDialog dialog = null;
    if (!typesFound.isEmpty()) {

        if (typesFound.size() == 1) {
            /* exact macht possible */
            String found = typesFound.get(0);
            IType type = access.findType(found, new NullProgressMonitor());
            if (type instanceof IJavaElement) {
                IJavaElement javaElement = type;
                openInEditor(javaElement);
                return;
            } else {
                /* keep on going - use type dialog as fallback...*/
            }

        }

        int style = IJavaElementSearchConstants.CONSIDER_ALL_TYPES;
        try {
            String found = typesFound.get(0);

            IRunnableContext runnableContext = EGradleUtil.getActiveWorkbenchWindow();

            dialog = JavaUI.createTypeDialog(shell, runnableContext, scope, style, false, found);
            dialog.setTitle("Potential Java types found:");
        } catch (JavaModelException e) {
            EGradleUtil.log("Cannot create java type dialog", e);
        }
    } else {
        dialog = createResourceDialog(shell);
    }

    final int resultCode = dialog.open();

    if (resultCode != Window.OK) {
        return;
    }
    Object[] result = dialog.getResult();
    List<IFile> files = new ArrayList<>();
    List<IJavaElement> javaElements = new ArrayList<>();
    if (result != null) {
        for (int i = 0; i < result.length; i++) {
            Object object = result[i];
            if (object instanceof IFile) {
                files.add((IFile) object);
            } else if (object instanceof IJavaElement) {
                IJavaElement javaElement = (IJavaElement) object;
                javaElements.add(javaElement);
            }
        }
    }

    if (files.size() > 0) {

        final IWorkbenchPage page = EGradleUtil.getActivePage();
        if (page == null) {
            return;
        }
        IFile currentFile = null;
        try {
            for (Iterator<IFile> it = files.iterator(); it.hasNext();) {
                currentFile = it.next();
                IDE.openEditor(page, currentFile, true);
            }
        } catch (final PartInitException e) {
            EGradleUtil.log("Cannot open file:" + currentFile, e);
        }
    } else if (javaElements.size() > 0) {
        IJavaElement javaElement = javaElements.get(0);
        openInEditor(javaElement);
    }

}

From source file:de.jcup.egradle.eclipse.gradleeditor.jdt.JDTDataAccess.java

License:Apache License

/**
 * Find type/* w w w.  j a v a  2s.  c o m*/
 * 
 * @param className
 * @param monitor
 * @return type or <code>null</code>
 */
public IType findType(String className, IProgressMonitor monitor) {
    final IType[] result = { null };
    TypeNameMatchRequestor nameMatchRequestor = new TypeNameMatchRequestor() {
        @Override
        public void acceptTypeNameMatch(TypeNameMatch match) {
            result[0] = match.getType();
        }
    };
    int lastDot = className.lastIndexOf('.');
    char[] packageName = lastDot >= 0 ? className.substring(0, lastDot).toCharArray() : null;
    char[] typeName = (lastDot >= 0 ? className.substring(lastDot + 1) : className).toCharArray();
    SearchEngine engine = new SearchEngine();
    int packageMatchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    try {
        engine.searchAllTypeNames(packageName, packageMatchRule, typeName, packageMatchRule,
                IJavaSearchConstants.TYPE, SearchEngine.createWorkspaceScope(), nameMatchRequestor,
                IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
    } catch (JavaModelException e) {
        EGradleUtil.log(e);
    }
    return result[0];
}

From source file:de.loskutov.anyedit.jdt.JdtUtils.java

License:Open Source License

/**
 * @param typeName/*from w w w .j  av  a  2 s.  c om*/
 * @throws OperationCanceledException
 *             if user doesnt select founded types
 */
public static int searchAndOpenType(String typeName) throws OperationCanceledException {
    if (typeName == null) {
        return 0;
    }
    IJavaSearchScope fScope = SearchEngine.createWorkspaceScope();
    IType type = null;
    try {
        IType[] types = getTypeForName(typeName, fScope, null);
        if (types.length > 0) {
            if (types[0] != null) {
                type = types[0];
            } else {
                return 2;
            }
        }
    } catch (JavaModelException e) {
        AnyEditToolsPlugin.logError(null, e);
    }
    if (type == null) {
        return 0;
    }
    try {
        IEditorPart part = JavaUI.openInEditor(type);
        JavaUI.revealInEditor(part, (IJavaElement) type);
    } catch (CoreException x) {
        AnyEditToolsPlugin.errorDialog("'Open type' operation failed", x);
        return 0;
    }
    return 1;
}

From source file:de.ovgu.featureide.core.runtime.RuntimeParameters.java

License:Open Source License

/**
 * Method to get all call locations of a method.
 * //from   w w  w.  j  a  va  2  s .co m
 * @param m
 *            Method for which the call hierarchy will be evaluated.
 * @return All call locations.
 */

private ArrayList<CallLocation[]> getCallersOf(final IMethod m) {

    final CallHierarchy callHierarchy = new CallHierarchy();
    final IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    callHierarchy.setSearchScope(scope);

    final IMember[] members = { m };
    final ArrayList<MethodCall> methodCalls = new ArrayList<MethodCall>();

    final MethodWrapper[] callerWrapper = callHierarchy.getCallerRoots(members);
    final ArrayList<MethodWrapper> callsWrapper = new ArrayList<MethodWrapper>();
    final ArrayList<CallLocation[]> callList = new ArrayList<CallLocation[]>();

    for (final MethodWrapper mWrapper : callerWrapper) {
        callsWrapper.addAll(Arrays.asList(mWrapper.getCalls(new NullProgressMonitor())));
    }
    for (final MethodWrapper mWrapper : callsWrapper) {
        methodCalls.add(mWrapper.getMethodCall());
    }
    for (final MethodCall mCall : methodCalls) {
        final CallLocation[] callArray = new CallLocation[mCall.getCallLocations().size()];
        mCall.getCallLocations().toArray(callArray);
        callList.add(callArray);
    }

    return callList;

}

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();/* w ww  .  ja v a  2s.  c  om*/

    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);
    }
}

From source file:edu.washington.cs.cupid.preferences.TypeViewPreferencePage.java

License:Open Source License

private Object[] showTypeDialog() {
    SelectionDialog dialog;//from  ww w . java2  s  .c o m
    try {
        dialog = JavaUI.createTypeDialog(this.getShell(), null, SearchEngine.createWorkspaceScope(),
                IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES, false);
    } catch (JavaModelException e) {
        return null;
        // NO OP
    }
    dialog.open();

    return dialog.getResult();
}

From source file:edu.washington.cs.cupid.scripting.java.quickfix.ClasspathProcessor.java

License:Open Source License

private List<IJavaCompletionProposal> buildMissingBundleProposals(final IInvocationContext context,
        final IProblemLocation location) {
    final List<IJavaCompletionProposal> proposals = Lists.newArrayList();

    String className = location.getProblemArguments()[0];

    SearchRequestor requestor = new SearchRequestor() {
        @Override/*from   w  w  w . j a v  a  2 s.c  om*/
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            if (match instanceof TypeDeclarationMatch) {
                if (match.getElement() instanceof IType) {
                    IType type = (IType) match.getElement();
                    Bundle bundle = null;
                    try {
                        bundle = ClasspathUtil.bundleForClass(type.getFullyQualifiedName());
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException("Internal error finding bundle for class ", e);
                    }
                    proposals.add(
                            new AddBundleCompletion(context.getCompilationUnit().getJavaProject(), bundle));
                } else {
                    throw new RuntimeException("Unexpected match of type " + match.getElement().getClass());
                }
            }
        }
    };

    SearchEngine engine = new SearchEngine();
    try {
        engine.search(
                SearchPattern.createPattern(className, IJavaSearchConstants.TYPE,
                        IJavaSearchConstants.DECLARATIONS, SearchPattern.R_FULL_MATCH), // pattern
                new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                SearchEngine.createWorkspaceScope(), //scope, 
                requestor, // searchRequestor
                null // progress monitor
        );
    } catch (CoreException ex) {
        // NO OP
    }

    return proposals;
}