Example usage for org.eclipse.jdt.core.search IJavaSearchScope REFERENCED_PROJECTS

List of usage examples for org.eclipse.jdt.core.search IJavaSearchScope REFERENCED_PROJECTS

Introduction

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

Prototype

int REFERENCED_PROJECTS

To view the source code for org.eclipse.jdt.core.search IJavaSearchScope REFERENCED_PROJECTS.

Click Source Link

Document

Include type constant (bit mask) indicating that referenced projects should be considered in the search scope.

Usage

From source file:com.ebmwebsourcing.petals.common.extensions.internal.wizards.JavaToWSDLWizardPage.java

License:Open Source License

/**
 * Open a dialog to select a class.//from w  ww  . ja v  a  2  s . com
 */
private void openClassSelectionDialog() {

    if (this.javaProject == null)
        return;

    Shell shell = this.classText.getShell();
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { this.javaProject },
            IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES
                    | IJavaSearchScope.REFERENCED_PROJECTS);

    String filter = this.classText.getText().trim();
    filter = filter.length() == 0 ? "?" : filter;
    try {
        SelectionDialog dlg = JavaUI.createTypeDialog(shell, new ProgressMonitorDialog(shell), scope,
                IJavaElementSearchConstants.CONSIDER_INTERFACES, true, filter);

        if (dlg.open() == Window.OK) {
            IType type = (IType) dlg.getResult()[0];
            String selection = type.getFullyQualifiedName();
            this.classText.setText(selection);
            this.classText.setSelection(selection.length());
        }

    } catch (JavaModelException e) {
        PetalsCommonWsdlExtPlugin.log(e, IStatus.ERROR);
    }
}

From source file:com.windowtester.swt.codegen.dialogs.SearchScopeHelper.java

License:Open Source License

public IType[] inProject(IJavaProject project) throws CoreException {

    int includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES |
    // IJavaSearchScope.SYSTEM_LIBRARIES |
            IJavaSearchScope.REFERENCED_PROJECTS;

    IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project },
            includeMask);/* ww  w  .  j  av  a 2  s . co  m*/

    // // DEBUG: what does our scope encompass?
    // for (IPath path : searchScope.enclosingProjectsAndJars())
    // System.out.println("will search " + path);
    // jar containing classes we want to match is seen,
    // but results within that jar are not being returned.

    SearchEngine se = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(type.getFullyQualifiedName(),
            IJavaSearchConstants.CLASS, IJavaSearchConstants.IMPLEMENTORS,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

    SearchRequestor requestor = new SearchRequestor() {

        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            //            System.err.println("found match "
            //                  + match.getElement().getClass() + ":"
            //                  + match.getElement());

            results.add((IType) match.getElement());
        }
    };

    //      long start = System.currentTimeMillis();
    se.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, searchScope,
            requestor, monitor);

    //add "self" type
    results.add(type);

    //      System.err.println("done in " + (System.currentTimeMillis() - start)
    //            + " ms");
    //      for (Iterator iterator = results.iterator(); iterator.hasNext();) {
    //         IType type = (IType) iterator.next();
    //         System.out.println(type);
    //      }

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

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  w  w  w .j  av a2 s.com

    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 handleFindAll(String proj, String file, int start, int end, boolean defs, boolean refs, boolean impls,
        boolean equiv, boolean exact, boolean system, boolean typeof, boolean ronly, boolean wonly,
        IvyXmlWriter xw) throws BedrockException {
    IJavaProject ijp = getJavaProject(proj);
    IPath fp = new Path(file);

    int limit = 0;
    if (defs && refs)
        limit = IJavaSearchConstants.ALL_OCCURRENCES;
    else if (defs)
        limit = IJavaSearchConstants.DECLARATIONS;
    else if (refs)
        limit = IJavaSearchConstants.REFERENCES;
    int flimit = limit;
    if (ronly)/*from ww  w  .ja v a 2  s  .  co m*/
        flimit = IJavaSearchConstants.READ_ACCESSES;
    else if (wonly)
        flimit = IJavaSearchConstants.WRITE_ACCESSES;

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

    SearchPattern pat = null;
    IJavaSearchScope scp = null;

    ICompilationUnit icu = our_plugin.getProjectManager().getCompilationUnit(proj, file);
    if (icu == null)
        throw new BedrockException("Compilation unit not found for " + fp);
    icu = getCompilationElement(icu);

    ICompilationUnit[] working = null;
    FindFilter filter = null;

    IJavaElement cls = null;
    char[] packagename = null;
    char[] typename = null;

    try {
        BedrockPlugin.logD("Getting search scopes");
        IJavaElement[] pelt;
        if (ijp != null)
            pelt = new IJavaElement[] { ijp };
        else
            pelt = getAllProjects();
        working = getWorkingElements(pelt);
        int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
        if (system)
            fg |= IJavaSearchScope.SYSTEM_LIBRARIES | IJavaSearchScope.APPLICATION_LIBRARIES;
        scp = SearchEngine.createJavaSearchScope(pelt, fg);

        BedrockPlugin.logD("Locating item to search for");
        IJavaElement[] elts = icu.codeSelect(start, end - start);

        if (typeof) {
            Set<IJavaElement> nelt = new LinkedHashSet<IJavaElement>();
            for (int i = 0; i < elts.length; ++i) {
                IType typ = null;
                String tnm = null;
                switch (elts[i].getElementType()) {
                case IJavaElement.FIELD:
                    tnm = ((IField) elts[i]).getTypeSignature();
                    break;
                case IJavaElement.LOCAL_VARIABLE:
                    tnm = ((ILocalVariable) elts[i]).getTypeSignature();
                    break;
                case IJavaElement.METHOD:
                    typ = ((IMethod) elts[i]).getDeclaringType();
                    break;
                default:
                    nelt.add(elts[i]);
                    break;
                }
                if (typ != null)
                    nelt.add(typ);
                else if (tnm != null && ijp != null) {
                    IJavaElement elt = ijp.findElement(tnm, null);
                    if (elt != null) {
                        nelt.add(elt);
                        typ = null;
                    } else {
                        while (tnm.startsWith("[")) {
                            String xtnm = tnm.substring(1);
                            if (xtnm == null)
                                break;
                            tnm = xtnm;
                        }
                        int ln = tnm.length();
                        String xtnm = tnm;
                        if (tnm.startsWith("L") && tnm.endsWith(";")) {
                            xtnm = tnm.substring(1, ln - 1);
                        } else if (tnm.startsWith("Q") && tnm.endsWith(";")) {
                            xtnm = tnm.substring(1, ln - 1);
                        }
                        if (xtnm != null)
                            tnm = xtnm;
                        int idx1 = tnm.lastIndexOf(".");
                        if (idx1 > 0) {
                            String pkgnm = tnm.substring(0, idx1);
                            xtnm = tnm.substring(idx1 + 1);
                            if (xtnm != null)
                                tnm = xtnm;
                            pkgnm = pkgnm.replace('$', '.');
                            packagename = pkgnm.toCharArray();
                        }
                        tnm = tnm.replace('$', '.');
                        typename = tnm.toCharArray();
                    }

                    if (typename != null) {
                        BedrockPlugin.logD("Handling type names");
                        FindTypeHandler fth = new FindTypeHandler(ijp);
                        SearchEngine se = new SearchEngine(working);
                        se.searchAllTypeNames(packagename, SearchPattern.R_EXACT_MATCH, typename,
                                SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, scp, fth,
                                IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
                        nelt.addAll(fth.getFoundItems());
                    }
                }
            }
            IJavaElement[] nelts = new IJavaElement[nelt.size()];
            elts = nelt.toArray(nelts);
        }

        if (elts.length == 1 && !typeof) {
            xw.begin("SEARCHFOR");
            switch (elts[0].getElementType()) {
            case IJavaElement.FIELD:
                xw.field("TYPE", "Field");
                break;
            case IJavaElement.LOCAL_VARIABLE:
                xw.field("TYPE", "Local");
                break;
            case IJavaElement.METHOD:
                xw.field("TYPE", "Function");
                break;
            case IJavaElement.TYPE:
            case IJavaElement.TYPE_PARAMETER:
                xw.field("TYPE", "Class");
                cls = elts[0];
                break;
            }
            xw.text(elts[0].getElementName());
            xw.end("SEARCHFOR");
        }
        int etyp = -1;
        for (int i = 0; i < elts.length; ++i) {
            SearchPattern sp;
            int xlimit = limit;
            switch (elts[i].getElementType()) {
            case IJavaElement.FIELD:
            case IJavaElement.LOCAL_VARIABLE:
                xlimit = flimit;
                break;
            case IJavaElement.TYPE:
                if (impls)
                    xlimit = IJavaSearchConstants.IMPLEMENTORS;
                break;
            case IJavaElement.METHOD:
                if (impls)
                    xlimit |= IJavaSearchConstants.IGNORE_DECLARING_TYPE;
                break;
            }
            if (mrule < 0)
                sp = SearchPattern.createPattern(elts[i], xlimit);
            else
                sp = SearchPattern.createPattern(elts[i], xlimit, mrule);
            if (pat == null)
                pat = sp;
            else
                pat = SearchPattern.createOrPattern(pat, sp);
            if (etyp < 0)
                etyp = elts[i].getElementType();
        }

        if (etyp == IJavaElement.METHOD) {
            if (impls) {
                if (defs)
                    filter = new ImplementFilter(elts);
            } else if (defs && !refs)
                filter = new ClassFilter(elts);
        }

    } catch (JavaModelException e) {
        BedrockPlugin.logE("SEARCH PROBLEM: " + e);
        e.printStackTrace();
        throw new BedrockException("Can't find anything to search for", e);
    }

    if (pat == null) {
        BedrockPlugin.logW("Nothing to search for");
        return;
    }

    BedrockPlugin.logD("Setting up search");
    SearchEngine se = new SearchEngine(working);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, filter, false);

    BedrockPlugin.logD(
            "BEGIN SEARCH " + pat + " " + parts.length + " " + " " + scp + " :: COPIES: " + working.length);

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

    if (cls != null && defs) { // need to add the actual class definition
        BedrockUtil.outputJavaElement(cls, false, xw);
    }
}

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

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

License:Open Source License

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

void rename(String proj, String bid, String file, int start, int end, String name, String handle,
        String newname, boolean keeporig, boolean getters, boolean setters, boolean dohier, boolean qual,
        boolean refs, boolean dosimilar, boolean textocc, boolean doedit, String filespat, IvyXmlWriter xw)
        throws BedrockException {
    ICompilationUnit icu = our_plugin.getCompilationUnit(proj, file);

    IJavaElement[] elts;/*from   w ww . ja  v a  2 s  .  com*/
    try {
        elts = icu.codeSelect(start, end - start);
    } catch (JavaModelException e) {
        throw new BedrockException("Bad location: " + e, e);
    }

    IJavaElement relt = null;
    for (IJavaElement ije : elts) {
        if (handle != null && !handle.equals(ije.getHandleIdentifier()))
            continue;
        if (name != null && !name.equals(ije.getElementName()))
            continue;
        relt = ije;
        break;
    }
    if (relt == null)
        throw new BedrockException("Item to rename not found");

    BedrockPlugin.logD("RENAME CHECK " + relt.getElementType() + " " + relt.getParent().getElementType());

    switch (relt.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        throw new BedrockException("Compilation unit renaming not supported yet");
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        throw new BedrockException("Package renaming not supported yet");
    case IJavaElement.FIELD:
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.TYPE_PARAMETER:
        break;
    case IJavaElement.METHOD:
        IMethod mthd = (IMethod) relt;
        try {
            if (mthd.isConstructor())
                throw new BedrockException("Constructor renaming not supported yet");
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.TYPE:
        IJavaElement pelt = relt.getParent();
        if (pelt.getElementType() == IJavaElement.COMPILATION_UNIT) {
            ITypeRoot xcu = (ITypeRoot) pelt;
            if (relt == xcu.findPrimaryType()) {
                throw new BedrockException("Compilation unit renaming based on type not supported yet");
            }
        }
        break;
    default:
        throw new BedrockException("Invalid element type to rename");
    }

    SearchPattern sp = SearchPattern.createPattern(relt, IJavaSearchConstants.ALL_OCCURRENCES,
            SearchPattern.R_EXACT_MATCH);

    List<ICompilationUnit> worku = new ArrayList<ICompilationUnit>();
    for (IJavaElement je : BedrockJava.getAllProjects()) {
        our_plugin.getWorkingElements(je, worku);
    }
    ICompilationUnit[] work = new ICompilationUnit[worku.size()];
    work = worku.toArray(work);

    int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
    IJavaSearchScope scp = SearchEngine.createJavaSearchScope(work, fg);

    SearchEngine se = new SearchEngine(work);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, null);

    try {
        se.search(sp, parts, scp, fh, null);
    } catch (CoreException e) {
        throw new BedrockException("Problem doing find all search: " + e, e);
    }

    BedrockPlugin.logD("RENAME RESULT = " + xw.toString());
}

From source file:net.harawata.mybatipse.mybatis.ProposalComputorHelper.java

License:Open Source License

public static List<ICompletionProposal> proposeJavaType(IJavaProject project, final int start, final int length,
        boolean includeAlias, String matchString) {
    final List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
    if (includeAlias) {
        Map<String, String> aliasMap = TypeAliasCache.getInstance().searchTypeAliases(project, matchString);
        for (Entry<String, String> entry : aliasMap.entrySet()) {
            String qualifiedName = entry.getKey();
            String alias = entry.getValue();
            proposals.add(new JavaCompletionProposal(alias, start, length, alias.length(),
                    Activator.getIcon("/icons/mybatis-alias.png"), alias + " - " + qualifiedName, null, null,
                    200));//from ww  w  .  j  av a  2 s  .c  om
        }
    }

    int includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
    // Include application libraries only when package is specified (for better performance).
    boolean pkgSpecified = matchString != null && matchString.indexOf('.') > 0;
    if (pkgSpecified)
        includeMask |= IJavaSearchScope.APPLICATION_LIBRARIES;
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaProject[] { project }, includeMask);
    TypeNameRequestor requestor = new JavaTypeNameRequestor() {
        @Override
        public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName,
                char[][] enclosingTypeNames, String path) {
            if (Flags.isAbstract(modifiers) || Flags.isInterface(modifiers))
                return;

            addJavaTypeProposal(proposals, start, length, packageName, simpleTypeName, enclosingTypeNames);
        }
    };
    try {
        searchJavaType(matchString, scope, requestor);
    } catch (JavaModelException e) {
        Activator.log(Status.ERROR, e.getMessage(), e);
    }
    return proposals;
}

From source file:net.harawata.mybatipse.mybatis.TypeAliasCache.java

License:Open Source License

/**
 * <p>/*from  w ww.  ja  v a2 s.co m*/
 * Trying to find calls registering type aliases.<br>
 * But only simple calls can be supported.
 * </p>
 */
private void scanJavaConfig(IJavaProject project, final TypeAliasMap aliasMap, final Set<String> packages,
        IReporter reporter) {
    try {
        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaProject[] { project },
                IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS
                        | IJavaSearchScope.APPLICATION_LIBRARIES);
        SearchParticipant[] participants = new SearchParticipant[] {
                SearchEngine.getDefaultSearchParticipant() };
        SearchEngine searchEngine = new SearchEngine();

        final Set<ITypeRoot> typeRoots = new HashSet<ITypeRoot>();

        // Collect classes that contain mybatis-guice calls.
        IType mybatisModuleType = project.findType(GUICE_MODULE_FQN);
        if (mybatisModuleType == null || !mybatisModuleType.exists())
            return;

        IMethod addSimpleAliasMethod = mybatisModuleType.getMethod("addSimpleAlias",
                new String[] { "Ljava.lang.Class;" });
        SearchPattern pattern = SearchPattern.createPattern(addSimpleAliasMethod,
                IJavaSearchConstants.REFERENCES | IJavaSearchConstants.IGNORE_DECLARING_TYPE
                        | IJavaSearchConstants.IMPLEMENTORS);
        searchEngine.search(pattern, participants, scope, new MethodSearchRequestor(typeRoots), null);

        IMethod addSimpleAliasesMethodWithPackage = mybatisModuleType.getMethod("addSimpleAliases",
                new String[] { "Ljava.lang.String;" });
        pattern = SearchPattern.createPattern(addSimpleAliasesMethodWithPackage,
                IJavaSearchConstants.REFERENCES | IJavaSearchConstants.IGNORE_DECLARING_TYPE);
        searchEngine.search(pattern, participants, scope, new MethodSearchRequestor(typeRoots), null);

        // IMethod addSimpleAliasesMethodWithPackageAndTest = mybatisModuleType.getMethod(
        // "addSimpleAliases", new String[]{
        // "Ljava.util.Collection<Ljava.lang.Class<*>;>;"
        // });
        // pattern = SearchPattern.createPattern(addSimpleAliasesMethodWithPackageAndTest,
        // IJavaSearchConstants.REFERENCES | IJavaSearchConstants.IGNORE_DECLARING_TYPE);
        // searchEngine.search(pattern, participants, scope, new MethodSearchRequestor(typeRoots),
        // null);

        // Searches Spring java config if necessary.
        // if (typeRoots.isEmpty())
        // {
        // IType sqlSessionFactoryBeanType = project.findType(SPRING_BEAN_FQN);
        // if (sqlSessionFactoryBeanType == null || !sqlSessionFactoryBeanType.exists())
        // return;
        //
        // IMethod setTypeAliasesPackageMethod = sqlSessionFactoryBeanType.getMethod(
        // "setTypeAliasesPackage", new String[]{
        // "Ljava.lang.String;"
        // });
        // pattern = SearchPattern.createPattern(setTypeAliasesPackageMethod,
        // IJavaSearchConstants.REFERENCES | IJavaSearchConstants.IGNORE_DECLARING_TYPE
        // | IJavaSearchConstants.IMPLEMENTORS);
        // searchEngine.search(pattern, participants, scope, new MethodSearchRequestor(typeRoots),
        // null);
        // }

        for (ITypeRoot typeRoot : typeRoots) {
            ASTParser parser = ASTParser.newParser(AST.JLS4);
            parser.setSource(typeRoot);
            parser.setResolveBindings(true);
            CompilationUnit astUnit = (CompilationUnit) parser.createAST(null);
            astUnit.accept(new JavaConfigVisitor(aliasMap, packages));
        }
    } catch (JavaModelException e) {
        Activator.log(Status.ERROR, e.getMessage(), e);
    } catch (CoreException e) {
        Activator.log(Status.ERROR, e.getMessage(), e);
    }
}

From source file:net.harawata.mybatipse.mybatis.TypeAliasCache.java

License:Open Source License

private void collectTypesInPackages(final IJavaProject project, Set<String> packages,
        final TypeAliasMap aliasMap, Set<String> superTypes, IReporter reporter) {
    final Set<IType> superTypeSet = new HashSet<IType>();
    try {/*from  ww w .java  2 s .c  om*/
        for (String supType : superTypes) {
            superTypeSet.add(project.findType(supType));
        }
    } catch (JavaModelException e) {
        Activator.log(Status.ERROR, e.getMessage(), e);
    }

    int includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS
            | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES;
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaProject[] { project }, includeMask);
    TypeNameRequestor requestor = new TypeNameRequestor() {
        @Override
        public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName,
                char[][] enclosingTypeNames, String path) {
            // Ignore abstract classes.
            if (Flags.isAbstract(modifiers))
                return;

            String qualifiedName = NameUtil.buildQualifiedName(packageName, simpleTypeName, enclosingTypeNames,
                    false);
            try {
                IType foundType = project.findType(qualifiedName);
                if (superTypeSet.isEmpty() || isSuperTypeMatched(foundType, superTypeSet)) {
                    String alias = getAliasAnnotationValue(foundType);
                    if (alias == null) {
                        alias = new String(simpleTypeName);
                    }
                    aliasMap.put(alias, qualifiedName);
                }
            } catch (JavaModelException e) {
                Activator.log(Status.WARNING, "Error occurred while searching type alias.", e);
            }
        }

        private boolean isSuperTypeMatched(IType foundType, Set<IType> superTypeSet) throws JavaModelException {
            for (IType superType : superTypeSet) {
                if (isAssignable(foundType, superType)) {
                    return true;
                }
            }
            return false;
        }
    };

    SearchEngine searchEngine = new SearchEngine();
    for (String pkg : packages) {
        if (reporter != null && reporter.isCancelled()) {
            throw new OperationCanceledException();
        }
        try {
            searchEngine.searchAllTypeNames(pkg.toCharArray(), SearchPattern.R_EXACT_MATCH, null,
                    SearchPattern.R_CAMELCASE_MATCH, IJavaSearchConstants.CLASS, scope, requestor,
                    IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
        } catch (JavaModelException e) {
            Activator.log(Status.ERROR, e.getMessage(), e);
        }
    }
}

From source file:net.harawata.mybatipse.mybatis.XmlCompletionProposalComputer.java

License:Open Source License

private void proposePackage(final ContentAssistRequest contentAssistRequest, IJavaProject project,
        String matchString, final int start, final int length) throws CoreException {
    final List<ICompletionProposal> results = new ArrayList<ICompletionProposal>();
    final Set<String> foundPkgs = new HashSet<String>();
    int includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
    // Include application libraries only when package is specified (for better performance).
    boolean pkgSpecified = matchString != null && matchString.indexOf('.') > 0;
    if (pkgSpecified)
        includeMask |= IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES;
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaProject[] { project }, includeMask);
    SearchRequestor requestor = new SearchRequestor() {
        @Override//from ww w .  j a  v a 2  s. com
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            PackageFragment element = (PackageFragment) match.getElement();
            String pkg = element.getElementName();
            if (pkg != null && pkg.length() > 0 && !foundPkgs.contains(pkg)) {
                foundPkgs.add(pkg);
                results.add(new CompletionProposal(pkg, start, length, pkg.length(), Activator.getIcon(), pkg,
                        null, null));
            }
        }
    };
    searchPackage(matchString, scope, requestor);
    addProposals(contentAssistRequest, results);
}