Example usage for org.eclipse.jdt.internal.core.search.matching PossibleMatch getSimilarMatch

List of usage examples for org.eclipse.jdt.internal.core.search.matching PossibleMatch getSimilarMatch

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.search.matching PossibleMatch getSimilarMatch.

Prototype

PossibleMatch getSimilarMatch() 

Source Link

Usage

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Creates an IMethod from the given method declaration and type.
 *//*from  www  .  ja  v a  2  s. co  m*/
protected IJavaElement createHandle(AbstractMethodDeclaration method, IJavaElement parent) {
    if (!(parent instanceof IType))
        return parent;

    IType type = (IType) parent;
    Argument[] arguments = method.arguments;
    int argCount = arguments == null ? 0 : arguments.length;
    if (type.isBinary()) {
        // don't cache the methods of the binary type
        // fall thru if its a constructor with a synthetic argument... find it the slower way
        ClassFileReader reader = classFileReader(type);
        if (reader != null) {
            // build arguments names
            boolean firstIsSynthetic = false;
            if (reader.isMember() && method.isConstructor() && !Flags.isStatic(reader.getModifiers())) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=48261
                firstIsSynthetic = true;
                argCount++;
            }
            char[][] argumentTypeNames = new char[argCount][];
            for (int i = 0; i < argCount; i++) {
                char[] typeName = null;
                if (i == 0 && firstIsSynthetic) {
                    typeName = type.getDeclaringType().getFullyQualifiedName().toCharArray();
                } else if (arguments != null) {
                    TypeReference typeRef = arguments[firstIsSynthetic ? i - 1 : i].type;
                    typeName = CharOperation.concatWith(typeRef.getTypeName(), '.');
                    for (int k = 0, dim = typeRef.dimensions(); k < dim; k++)
                        typeName = CharOperation.concat(typeName, new char[] { '[', ']' });
                }
                if (typeName == null) {
                    // invalid type name
                    return null;
                }
                argumentTypeNames[i] = typeName;
            }
            // return binary method
            IMethod binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames);
            if (binaryMethod == null) {
                // when first attempt fails, try with similar matches if any...
                PossibleMatch similarMatch = this.currentPossibleMatch.getSimilarMatch();
                while (similarMatch != null) {
                    type = ((ClassFile) similarMatch.openable).getType();
                    binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames);
                    if (binaryMethod != null) {
                        return binaryMethod;
                    }
                    similarMatch = similarMatch.getSimilarMatch();
                }
            }
            return binaryMethod;
        }
        if (BasicSearchEngine.VERBOSE) {
            System.out.println("Not able to createHandle for the method " + //$NON-NLS-1$
                    CharOperation.charToString(method.selector) + " May miss some results"); //$NON-NLS-1$
        }
        return null;
    }

    String[] parameterTypeSignatures = new String[argCount];
    if (arguments != null) {
        for (int i = 0; i < argCount; i++) {
            TypeReference typeRef = arguments[i].type;
            char[] typeName = CharOperation.concatWith(typeRef.getParameterizedTypeName(), '.');
            parameterTypeSignatures[i] = Signature.createTypeSignature(typeName, false);
        }
    }

    return createMethodHandle(type, new String(method.selector), parameterTypeSignatures);
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected void locateMatches(JavaProject javaProject, PossibleMatch[] possibleMatches, int start, int length)
        throws CoreException {
    initialize(javaProject, length);/* www  . j a va2  s  . c o  m*/

    // GROOVY start
    boolean isInterestingProject = LanguageSupportFactory.isInterestingProject(javaProject.getProject());
    Set alreadyMatched = new HashSet();
    // GROOVY end

    // create and resolve binding (equivalent to beginCompilation() in Compiler)
    boolean mustResolvePattern = this.pattern.mustResolve;
    boolean mustResolve = mustResolvePattern;
    this.patternLocator.mayBeGeneric = this.options.sourceLevel >= ClassFileConstants.JDK1_5;
    boolean bindingsWereCreated = mustResolve;
    try {
        for (int i = start, maxUnits = start + length; i < maxUnits; i++) {
            PossibleMatch possibleMatch = possibleMatches[i];
            // GROOVY start
            if (isInterestingProject
                    && LanguageSupportFactory.isInterestingSourceFile(possibleMatch.document.getPath())) {
                boolean matchPerformed = LanguageSupportFactory.maybePerformDelegatedSearch(possibleMatch,
                        this.pattern, this.requestor);
                if (matchPerformed) {
                    alreadyMatched.add(possibleMatch);
                }
            }
            // GROOVY end
            try {
                if (!parseAndBuildBindings(possibleMatch, mustResolvePattern))
                    continue;
                // Currently we only need to resolve over pattern flag if there's potential parameterized types
                if (this.patternLocator.mayBeGeneric) {
                    // If pattern does not resolve then rely on possible match node set resolution
                    // which may have been modified while locator was adding possible matches to it
                    if (!mustResolvePattern && !mustResolve) {
                        mustResolve = possibleMatch.nodeSet.mustResolve;
                        bindingsWereCreated = mustResolve;
                    }
                } else {
                    // Reset matching node resolution with pattern one if there's no potential parameterized type
                    // to minimize side effect on previous search behavior
                    possibleMatch.nodeSet.mustResolve = mustResolvePattern;
                }
                // possible match node resolution has been merged with pattern one, so rely on it to know
                // whether we need to process compilation unit now or later
                if (!possibleMatch.nodeSet.mustResolve) {
                    if (this.progressMonitor != null) {
                        this.progressWorked++;
                        if ((this.progressWorked % this.progressStep) == 0)
                            this.progressMonitor.worked(this.progressStep);
                    }
                    process(possibleMatch, bindingsWereCreated);
                    if (this.numberOfMatches > 0
                            && this.matchesToProcess[this.numberOfMatches - 1] == possibleMatch) {
                        // forget last possible match as it was processed
                        this.numberOfMatches--;
                    }
                }
            } finally {
                if (possibleMatch.hasSimilarMatch()) {
                    // If there is similar match, then also process it
                    // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=211872
                    possibleMatches[i] = possibleMatch.getSimilarMatch();
                    i--;
                }
                if (!possibleMatch.nodeSet.mustResolve)
                    possibleMatch.cleanUp();
            }
        }
        if (mustResolve)
            this.lookupEnvironment.completeTypeBindings();

        // create hierarchy resolver if needed
        IType focusType = getFocusType();
        if (focusType == null) {
            this.hierarchyResolver = null;
        } else if (!createHierarchyResolver(focusType, possibleMatches)) {
            // focus type is not visible, use the super type names instead of the bindings
            if (computeSuperTypeNames(focusType) == null)
                return;
        }
    } catch (AbortCompilation e) {
        bindingsWereCreated = false;
    }

    if (!mustResolve) {
        return;
    }

    // possible match resolution
    for (int i = 0; i < this.numberOfMatches; i++) {
        if (this.progressMonitor != null && this.progressMonitor.isCanceled())
            throw new OperationCanceledException();
        PossibleMatch possibleMatch = this.matchesToProcess[i];
        this.matchesToProcess[i] = null; // release reference to processed possible match
        try {
            process(possibleMatch, bindingsWereCreated);
        } catch (AbortCompilation e) {
            // problem with class path: it could not find base classes
            // continue and try next matching openable reporting innacurate matches (since bindings will be null)
            bindingsWereCreated = false;
        } catch (JavaModelException e) {
            // problem with class path: it could not find base classes
            // continue and try next matching openable reporting innacurate matches (since bindings will be null)
            bindingsWereCreated = false;
        } finally {
            if (this.progressMonitor != null) {
                this.progressWorked++;
                if ((this.progressWorked % this.progressStep) == 0)
                    this.progressMonitor.worked(this.progressStep);
            }
            if (this.options.verbose)
                System.out.println(Messages.bind(Messages.compilation_done,
                        new String[] { String.valueOf(i + 1), String.valueOf(this.numberOfMatches),
                                new String(possibleMatch.parsedUnit.getFileName()) }));
            // cleanup compilation unit result
            // GROOVY Start
            // delay cleanup of groovy possible matches until later
            // the clean up will null-out back pointers to scopes used by other CompilationUnitDeclarations
            // old
            // possibleMatch.cleanUp();
            // new
            if (!alreadyMatched.contains(possibleMatch)) {
                possibleMatch.cleanUp();
            }
            // GROOVY End
        }
    }
    // GROOVY Start
    // now do the clean up of groovy matches
    for (Iterator iterator = alreadyMatched.iterator(); iterator.hasNext();) {
        PossibleMatch match = (PossibleMatch) iterator.next();
        match.cleanUp();
    }
    // GROOVY End      
}