Example usage for org.eclipse.jdt.core.search SearchMatch A_ACCURATE

List of usage examples for org.eclipse.jdt.core.search SearchMatch A_ACCURATE

Introduction

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

Prototype

int A_ACCURATE

To view the source code for org.eclipse.jdt.core.search SearchMatch A_ACCURATE.

Click Source Link

Document

The search result corresponds an exact match of the search pattern.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchingNodeSet.java

License:Open Source License

public int addMatch(ASTNode node, int matchLevel) {
    int maskedLevel = matchLevel & PatternLocator.MATCH_LEVEL_MASK;
    switch (maskedLevel) {
    case PatternLocator.INACCURATE_MATCH:
        if (matchLevel != maskedLevel) {
            addTrustedMatch(node, new Integer(SearchMatch.A_INACCURATE + (matchLevel
                    & org.eclipse.jdt.internal.core.search.matching.PatternLocator.FLAVORS_MASK)));
        } else {//from w  ww . jav a2 s.  c o m
            addTrustedMatch(node, POTENTIAL_MATCH);
        }
        break;
    case PatternLocator.POSSIBLE_MATCH:
        addPossibleMatch(node);
        break;
    case PatternLocator.ERASURE_MATCH:
        if (matchLevel != maskedLevel) {
            addTrustedMatch(node,
                    new Integer(SearchPattern.R_ERASURE_MATCH + (matchLevel & PatternLocator.FLAVORS_MASK)));
        } else {
            addTrustedMatch(node, ERASURE_MATCH);
        }
        break;
    case PatternLocator.ACCURATE_MATCH:
        if (matchLevel != maskedLevel) {
            addTrustedMatch(node,
                    new Integer(SearchMatch.A_ACCURATE + (matchLevel & PatternLocator.FLAVORS_MASK)));
        } else {
            addTrustedMatch(node, EXACT_MATCH);
        }
        break;
    }
    return matchLevel;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchingNodeSet.java

License:Open Source License

public String toString() {
    // TODO (jerome) should show both tables
    StringBuffer result = new StringBuffer();
    result.append("Exact matches:"); //$NON-NLS-1$
    Object[] keyTable = this.matchingNodes.keyTable;
    Object[] valueTable = this.matchingNodes.valueTable;
    for (int i = 0, l = keyTable.length; i < l; i++) {
        ASTNode node = (ASTNode) keyTable[i];
        if (node == null)
            continue;
        result.append("\n\t"); //$NON-NLS-1$
        switch (((Integer) valueTable[i]).intValue()) {
        case SearchMatch.A_ACCURATE:
            result.append("ACCURATE_MATCH: "); //$NON-NLS-1$
            break;
        case SearchMatch.A_INACCURATE:
            result.append("INACCURATE_MATCH: "); //$NON-NLS-1$
            break;
        case SearchPattern.R_ERASURE_MATCH:
            result.append("ERASURE_MATCH: "); //$NON-NLS-1$
            break;
        }/*from w ww.  j a v a2s.c o m*/
        node.print(0, result);
    }

    result.append("\nPossible matches:"); //$NON-NLS-1$
    Object[] nodes = this.possibleMatchingNodesSet.values;
    for (int i = 0, l = nodes.length; i < l; i++) {
        ASTNode node = (ASTNode) nodes[i];
        if (node == null)
            continue;
        result.append("\nPOSSIBLE_MATCH: "); //$NON-NLS-1$
        node.print(0, result);
    }
    return result.toString();
}

From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java

License:Open Source License

private IType findNonPrimaryType(String fullyQualifiedName, IProgressMonitor pm, RefactoringStatus status)
        throws JavaModelException {
    SearchPattern p = SearchPattern.createPattern(fullyQualifiedName, IJavaSearchConstants.TYPE,
            IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(p);

    engine.setFiltering(true, true);/*from   w  ww  .j av a 2s . c o m*/
    engine.setScope(RefactoringScopeFactory.create(fCtorBinding.getJavaElement().getJavaProject()));
    engine.setStatus(status);
    engine.searchPattern(new SubProgressMonitor(pm, 1));

    SearchResultGroup[] groups = (SearchResultGroup[]) engine.getResults();

    if (groups.length != 0) {
        for (int i = 0; i < groups.length; i++) {
            SearchMatch[] matches = groups[i].getSearchResults();
            for (int j = 0; j < matches.length; j++) {
                if (matches[j].getAccuracy() == SearchMatch.A_ACCURATE)
                    return (IType) matches[j].getElement();
            }
        }
    }
    return null;
}

From source file:org.eclim.plugin.jdt.command.complete.CompletionProposalCollector.java

License:Open Source License

public void completionFailure(IProblem problem) {
    ICompilationUnit src = getCompilationUnit();
    IJavaProject javaProject = src.getJavaProject();
    IProject project = javaProject.getProject();

    // undefined type or attempting to complete static members of an unimported
    // type/*from   w  w w .  java  2  s .  c  om*/
    if (problem.getID() == IProblem.UndefinedType || problem.getID() == IProblem.UnresolvedVariable) {
        try {
            SearchPattern pattern = SearchPattern.createPattern(problem.getArguments()[0],
                    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);
            if (requestor.getMatches().size() > 0) {
                imports = new ArrayList<String>();
                for (SearchMatch match : requestor.getMatches()) {
                    if (match.getAccuracy() != SearchMatch.A_ACCURATE) {
                        continue;
                    }
                    IJavaElement element = (IJavaElement) match.getElement();
                    String name = null;
                    switch (element.getElementType()) {
                    case IJavaElement.TYPE:
                        IType type = (IType) element;
                        if (Flags.isPublic(type.getFlags())) {
                            name = type.getFullyQualifiedName();
                        }
                        break;
                    case IJavaElement.METHOD:
                    case IJavaElement.FIELD:
                        name = ((IType) element.getParent()).getFullyQualifiedName() + '.'
                                + element.getElementName();
                        break;
                    }
                    if (name != null) {
                        name = name.replace('$', '.');
                        if (!ImportUtils.isImportExcluded(project, name)) {
                            imports.add(name);
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    IResource resource = src.getResource();
    String relativeName = resource.getProjectRelativePath().toString();
    if (new String(problem.getOriginatingFileName()).endsWith(relativeName)) {
        String filename = resource.getLocation().toString();

        // ignore the problem if a temp file is being used and the problem is that
        // the type needs to be defined in its own file.
        if (problem.getID() == IProblem.PublicClassMustMatchFileName
                && filename.indexOf("__eclim_temp_") != -1) {
            return;
        }

        FileOffsets offsets = FileOffsets.compile(filename);
        int[] lineColumn = offsets.offsetToLineColumn(problem.getSourceStart());

        error = new Error(problem.getMessage(), filename.replace("__eclim_temp_", ""), lineColumn[0],
                lineColumn[1], problem.isWarning());
    }
}

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

License:Open Source License

private String findMainClass(IJavaProject javaProject) throws Exception {
    ArrayList<IJavaElement> srcs = new ArrayList<IJavaElement>();
    for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            for (IPackageFragmentRoot root : javaProject.findPackageFragmentRoots(entry)) {
                srcs.add(root);/*from   w  ww. j  a va2s . c om*/
            }
        }
    }

    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(srcs.toArray(new IJavaElement[srcs.size()]));
    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;
            }

            try {
                IMethod method = (IMethod) match.getElement();
                String[] params = method.getParameterTypes();
                if (params.length != 1) {
                    return;
                }

                if (!Signature.SIG_VOID.equals(method.getReturnType())) {
                    return;
                }

                int flags = method.getFlags();
                if (!Flags.isPublic(flags) || !Flags.isStatic(flags)) {
                    return;
                }

                methods.add(method);
            } catch (JavaModelException e) {
                // ignore
            }
        }
    };

    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.command.search.ImplementorsSearchRequestor.java

License:Open Source License

@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
    if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
        Object element = match.getElement();
        if (element instanceof IMethod) {
            IMethod method = (IMethod) element;
            if (!JdtFlags.isAbstract(method)) {
                super.acceptSearchMatch(match);
            }/*w  ww .  j a  v a2s  .c  o m*/
        }
    }
}

From source file:org.eclim.plugin.jdt.command.search.SearchRequestor.java

License:Open Source License

@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
    if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
        matches.add(match);/* w  ww  .j  a  v  a2 s  .  co  m*/
    }
}

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;
            }/*www. ja  v  a 2 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.//from  w ww . ja v  a  2  s  .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.ajdt.internal.core.search.ExtraITDFinder.java

License:Open Source License

/**
 * Return all declaration matches that are ITDs of the proper type or in the type hierarchy of the expected type 
 * @throws JavaModelException /* w  ww  .  j a  v  a2s .  co  m*/
 */
private List<SearchMatch> findExtraDeclarationMatches(AJCompilationUnit unit,
        List<IntertypeElement> allRelevantItds, SearchPattern pattern, PossibleMatch match)
        throws JavaModelException {

    List<SearchMatch> extraDeclarationMatches = new ArrayList<SearchMatch>();
    // At this point, we know that the itds passed in have the same declaring type or are a subtype of the target type
    // So, just need to check selector and parameters
    // it is too time consuming to get the qualified parameters of the types, so
    // just match on simple names.
    if (pattern instanceof MethodPattern) {
        MethodPattern methPatt = (MethodPattern) pattern;
        char[] selector = methPatt.selector;
        char[][] simpleParamTypes = methPatt.parameterSimpleNames;

        for (IntertypeElement itd : allRelevantItds) {
            if (itd.getAJKind() == Kind.INTER_TYPE_METHOD
                    && CharOperation.equals(selector, itd.getTargetName().toCharArray())) {
                char[][] itdSimpleParamNames = extractSimpleParamNames(itd);
                if (CharOperation.equals(simpleParamTypes, itdSimpleParamNames)) {
                    ISourceRange sourceRange = itd.getNameRange();
                    extraDeclarationMatches.add(new MethodDeclarationMatch(itd, SearchMatch.A_ACCURATE,
                            sourceRange.getOffset(), sourceRange.getLength(), match.document.getParticipant(),
                            itd.getCompilationUnit().getResource()));
                }
            }
        }
    } else if (pattern instanceof ConstructorPattern) {
        ConstructorPattern consPatt = (ConstructorPattern) pattern;
        // must match the exact type
        char[] targetTypeName = TargetTypeUtils.getName(consPatt.declaringQualification,
                consPatt.declaringSimpleName);
        char[][] simpleParamTypes = consPatt.parameterSimpleNames;
        for (IntertypeElement itd : allRelevantItds) {
            if (itd.getAJKind() == Kind.INTER_TYPE_CONSTRUCTOR && targetTypeName != null
                    && CharOperation.equals(targetTypeName, fullyQualifiedTargetTypeName(itd))) {
                char[][] itdSimpleParamNames = extractSimpleParamNames(itd);
                if (CharOperation.equals(simpleParamTypes, itdSimpleParamNames)) {
                    ISourceRange sourceRange = itd.getNameRange();
                    extraDeclarationMatches.add(new MethodDeclarationMatch(itd, SearchMatch.A_ACCURATE,
                            sourceRange.getOffset(), sourceRange.getLength(), match.document.getParticipant(),
                            itd.getCompilationUnit().getResource()));
                }
            }
        }
    } else if (pattern instanceof FieldPattern) {
        FieldPattern fieldPatt = (FieldPattern) pattern;
        char[] targetTypeName = TargetTypeUtils.getName(TargetTypeUtils.getQualName(fieldPatt),
                TargetTypeUtils.getSimpleName(fieldPatt));
        char[] fieldName = fieldPatt.getIndexKey();
        for (IntertypeElement itd : allRelevantItds) {
            if (itd.getAJKind() == Kind.INTER_TYPE_FIELD
                    && CharOperation.equals(fieldName, itd.getTargetName().toCharArray()) &&
                    // must match the exact type, but only if a type exists
                    (targetTypeName == null
                            || CharOperation.equals(targetTypeName, fullyQualifiedTargetTypeName(itd)))) {
                ISourceRange sourceRange = itd.getNameRange();
                extraDeclarationMatches.add(new FieldDeclarationMatch(itd, SearchMatch.A_ACCURATE,
                        sourceRange.getOffset(), sourceRange.getLength(), match.document.getParticipant(),
                        itd.getCompilationUnit().getResource()));
            }
        }
    }
    return extraDeclarationMatches;
}