Example usage for org.eclipse.jdt.core.compiler CharOperation equals

List of usage examples for org.eclipse.jdt.core.compiler CharOperation equals

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler CharOperation equals.

Prototype

public static final boolean equals(char[] first, char[] second, boolean isCaseSensitive) 

Source Link

Document

If isCaseSensite is true, answers true if the two arrays are identical character by character, otherwise false.

Usage

From source file:com.codenvy.ide.ext.java.server.core.search.SearchPattern.java

License:Open Source License

/**
 * Returns whether the given name matches the given pattern.
 * <p>/*from w ww  .  j  av a  2 s .co m*/
 * This method should be re-implemented in subclasses that need to define how
 * a name matches a pattern.
 * </p>
 *
 * @param pattern the given pattern, or <code>null</code> to represent "*"
 * @param name the given name
 * @return whether the given name matches the given pattern
 */
public boolean matchesName(char[] pattern, char[] name) {
    if (pattern == null)
        return true; // null is as if it was "*"
    if (name != null) {
        boolean isCaseSensitive = (this.matchRule & R_CASE_SENSITIVE) != 0;
        int matchMode = this.matchRule & MODE_MASK;
        boolean emptyPattern = pattern.length == 0;
        if (emptyPattern && (this.matchRule & R_PREFIX_MATCH) != 0)
            return true;
        boolean sameLength = pattern.length == name.length;
        boolean canBePrefix = name.length >= pattern.length;
        boolean matchFirstChar = !isCaseSensitive || emptyPattern || (name.length > 0 && pattern[0] == name[0]);
        switch (matchMode) {
        case R_EXACT_MATCH:
            if (sameLength && matchFirstChar) {
                return CharOperation.equals(pattern, name, isCaseSensitive);
            }
            break;

        case R_PREFIX_MATCH:
            if (canBePrefix && matchFirstChar) {
                return CharOperation.prefixEquals(pattern, name, isCaseSensitive);
            }
            break;

        case R_PATTERN_MATCH:
            if (!isCaseSensitive)
                pattern = CharOperation.toLowerCase(pattern);
            return CharOperation.match(pattern, name, isCaseSensitive);

        case SearchPattern.R_CAMELCASE_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(pattern, name, false)) {
                return true;
            }
            // only test case insensitive as CamelCase already verified prefix case sensitive
            if (!isCaseSensitive && matchFirstChar && CharOperation.prefixEquals(pattern, name, false)) {
                return true;
            }
            break;

        case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
            return matchFirstChar && CharOperation.camelCaseMatch(pattern, name, true);

        case R_REGEXP_MATCH:
            // TODO implement regular expression match
            return true;
        }
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

private void selectMemberTypeFromImport(CompilationUnitDeclaration parsedUnit, char[] lastToken,
        ReferenceBinding ref, boolean staticOnly) {
    int fieldLength = lastToken.length;
    ReferenceBinding[] memberTypes = ref.memberTypes();
    next: for (int j = 0; j < memberTypes.length; j++) {
        ReferenceBinding memberType = memberTypes[j];

        if (fieldLength > memberType.sourceName.length)
            continue next;

        if (staticOnly && !memberType.isStatic())
            continue next;

        if (!CharOperation.equals(lastToken, memberType.sourceName, true))
            continue next;

        selectFrom(memberType, parsedUnit, false);
    }//  w  w  w .j  av  a2 s .  co m
}

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

private void selectStaticFieldFromStaticImport(CompilationUnitDeclaration parsedUnit, char[] lastToken,
        ReferenceBinding ref) {// w w w. j a  v  a2  s  .  c  o m
    int fieldLength = lastToken.length;
    FieldBinding[] fields = ref.availableFields();
    next: for (int j = 0; j < fields.length; j++) {
        FieldBinding field = fields[j];

        if (fieldLength > field.name.length)
            continue next;

        if (field.isSynthetic())
            continue next;

        if (!field.isStatic())
            continue next;

        if (!CharOperation.equals(lastToken, field.name, true))
            continue next;

        selectFrom(field, parsedUnit, false);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

private void selectStaticMethodFromStaticImport(CompilationUnitDeclaration parsedUnit, char[] lastToken,
        ReferenceBinding ref) {/*  w w  w  . ja  v  a  2s  .  c o m*/
    int methodLength = lastToken.length;
    MethodBinding[] methods = ref.availableMethods();
    next: for (int j = 0; j < methods.length; j++) {
        MethodBinding method = methods[j];

        if (method.isSynthetic())
            continue next;

        if (method.isDefaultAbstract())
            continue next;

        if (method.isConstructor())
            continue next;

        if (!method.isStatic())
            continue next;

        if (methodLength > method.selector.length)
            continue next;

        if (!CharOperation.equals(lastToken, method.selector, true))
            continue next;

        selectFrom(method, parsedUnit, false);
    }
}

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

License:Open Source License

boolean match(char patternTypeSuffix, char[] patternPkg, int matchRulePkg, char[] patternTypeName,
        int matchRuleType, int typeKind, char[] pkg, char[] typeName) {
    switch (patternTypeSuffix) {
    case IIndexConstants.CLASS_SUFFIX:
        if (typeKind != TypeDeclaration.CLASS_DECL)
            return false;
        break;/*from w  ww  .ja  v  a  2s.co m*/
    case IIndexConstants.CLASS_AND_INTERFACE_SUFFIX:
        if (typeKind != TypeDeclaration.CLASS_DECL && typeKind != TypeDeclaration.INTERFACE_DECL)
            return false;
        break;
    case IIndexConstants.CLASS_AND_ENUM_SUFFIX:
        if (typeKind != TypeDeclaration.CLASS_DECL && typeKind != TypeDeclaration.ENUM_DECL)
            return false;
        break;
    case IIndexConstants.INTERFACE_SUFFIX:
        if (typeKind != TypeDeclaration.INTERFACE_DECL)
            return false;
        break;
    case IIndexConstants.INTERFACE_AND_ANNOTATION_SUFFIX:
        if (typeKind != TypeDeclaration.INTERFACE_DECL && typeKind != TypeDeclaration.ANNOTATION_TYPE_DECL)
            return false;
        break;
    case IIndexConstants.ENUM_SUFFIX:
        if (typeKind != TypeDeclaration.ENUM_DECL)
            return false;
        break;
    case IIndexConstants.ANNOTATION_TYPE_SUFFIX:
        if (typeKind != TypeDeclaration.ANNOTATION_TYPE_DECL)
            return false;
        break;
    case IIndexConstants.TYPE_SUFFIX: // nothing
    }

    boolean isPkgCaseSensitive = (matchRulePkg & SearchPattern.R_CASE_SENSITIVE) != 0;
    if (patternPkg != null && !CharOperation.equals(patternPkg, pkg, isPkgCaseSensitive))
        return false;

    boolean isCaseSensitive = (matchRuleType & SearchPattern.R_CASE_SENSITIVE) != 0;
    if (patternTypeName != null) {
        boolean isCamelCase = (matchRuleType
                & (SearchPattern.R_CAMELCASE_MATCH | SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH)) != 0;
        int matchMode = matchRuleType & JavaSearchPattern.MATCH_MODE_MASK;
        if (!isCaseSensitive && !isCamelCase) {
            patternTypeName = CharOperation.toLowerCase(patternTypeName);
        }
        boolean matchFirstChar = !isCaseSensitive || patternTypeName[0] == typeName[0];
        switch (matchMode) {
        case SearchPattern.R_EXACT_MATCH:
            return matchFirstChar && CharOperation.equals(patternTypeName, typeName, isCaseSensitive);
        case SearchPattern.R_PREFIX_MATCH:
            return matchFirstChar && CharOperation.prefixEquals(patternTypeName, typeName, isCaseSensitive);
        case SearchPattern.R_PATTERN_MATCH:
            return CharOperation.match(patternTypeName, typeName, isCaseSensitive);
        case SearchPattern.R_REGEXP_MATCH:
            // TODO (frederic) implement regular expression match
            break;
        case SearchPattern.R_CAMELCASE_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(patternTypeName, typeName, false)) {
                return true;
            }
            return !isCaseSensitive && matchFirstChar
                    && CharOperation.prefixEquals(patternTypeName, typeName, false);
        case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
            return matchFirstChar && CharOperation.camelCaseMatch(patternTypeName, typeName, true);
        }
    }
    return true;

}

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

License:Open Source License

public boolean matchesDecodedKey(SearchPattern decodedPattern) {
    ConstructorDeclarationPattern pattern = (ConstructorDeclarationPattern) decodedPattern;

    // only top level types
    if ((pattern.extraFlags & ExtraFlags.IsMemberType) != 0)
        return false;

    // check package - exact match only
    if (this.declaringPackageName != null
            && !CharOperation.equals(this.declaringPackageName, pattern.declaringPackageName, true))
        return false;

    return (this.parameterCount == pattern.parameterCount || this.parameterCount == -1 || this.varargs)
            && matchesName(this.declaringSimpleName, pattern.declaringSimpleName);
}

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

License:Open Source License

/**
 * Return how the given name matches the given pattern.
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=79866"
 *
 * @param pattern/*from   w  w  w.j a  v  a 2  s .  com*/
 * @param name
 * @return Possible values are:
 * <ul>
 *    <li> {@link #ACCURATE_MATCH}</li>
 *    <li> {@link #IMPOSSIBLE_MATCH}</li>
 *    <li> {@link #POSSIBLE_MATCH} which may be flavored with following values:
 *       <ul>
 *       <li>{@link #EXACT_FLAVOR}: Given name is equals to pattern</li>
 *       <li>{@link #PREFIX_FLAVOR}: Given name prefix equals to pattern</li>
 *       <li>{@link #CAMELCASE_FLAVOR}: Given name matches pattern as Camel Case</li>
 *       <li>{@link #PATTERN_FLAVOR}: Given name matches pattern as Pattern (i.e. using '*' and '?' characters)</li>
 *       </ul>
 *    </li>
 * </ul>
 */
protected int matchNameValue(char[] pattern, char[] name) {
    if (pattern == null)
        return ACCURATE_MATCH; // null is as if it was "*"
    if (name == null)
        return IMPOSSIBLE_MATCH; // cannot match null name
    if (name.length == 0) { // empty name
        if (pattern.length == 0) { // can only matches empty pattern
            return ACCURATE_MATCH;
        }
        return IMPOSSIBLE_MATCH;
    } else if (pattern.length == 0) {
        return IMPOSSIBLE_MATCH; // need to have both name and pattern length==0 to be accurate
    }
    boolean matchFirstChar = !this.isCaseSensitive || pattern[0] == name[0];
    boolean sameLength = pattern.length == name.length;
    boolean canBePrefix = name.length >= pattern.length;
    switch (this.matchMode) {
    case SearchPattern.R_EXACT_MATCH:
        if (sameLength && matchFirstChar && CharOperation.equals(pattern, name, this.isCaseSensitive)) {
            return POSSIBLE_MATCH | EXACT_FLAVOR;
        }
        break;

    case SearchPattern.R_PREFIX_MATCH:
        if (canBePrefix && matchFirstChar && CharOperation.prefixEquals(pattern, name, this.isCaseSensitive)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_PATTERN_MATCH:
        // TODO_PERFS (frederic) Not sure this lowercase is necessary
        if (!this.isCaseSensitive) {
            pattern = CharOperation.toLowerCase(pattern);
        }
        if (CharOperation.match(pattern, name, this.isCaseSensitive)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_REGEXP_MATCH:
        // TODO (frederic) implement regular expression match
        break;

    case SearchPattern.R_CAMELCASE_MATCH:
        if (CharOperation.camelCaseMatch(pattern, name, false)) {
            return POSSIBLE_MATCH;
        }
        // only test case insensitive as CamelCase same part count already verified prefix case sensitive
        if (!this.isCaseSensitive && CharOperation.prefixEquals(pattern, name, false)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
        if (CharOperation.camelCaseMatch(pattern, name, true)) {
            return POSSIBLE_MATCH;
        }
        break;
    }
    return IMPOSSIBLE_MATCH;
}

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

License:Open Source License

protected int resolveLevelForType(char[] simpleNamePattern, char[] qualificationPattern,
        char[][][] patternTypeArguments, int depth, TypeBinding type) {
    // standard search with no generic additional information must succeed
    int level = resolveLevelForType(simpleNamePattern, qualificationPattern, type);
    if (level == IMPOSSIBLE_MATCH)
        return IMPOSSIBLE_MATCH;
    if (type == null || patternTypeArguments == null || patternTypeArguments.length == 0
            || depth >= patternTypeArguments.length) {
        return level;
    }//from  w  ww .  jav  a2 s.c om

    // if pattern is erasure match (see bug 79790), commute impossible to erasure
    int impossible = this.isErasureMatch ? ERASURE_MATCH : IMPOSSIBLE_MATCH;

    // pattern has type parameter(s) or type argument(s)
    if (type.isGenericType()) {
        // Binding is generic, get its type variable(s)
        TypeVariableBinding[] typeVariables = null;
        if (type instanceof SourceTypeBinding) {
            SourceTypeBinding sourceTypeBinding = (SourceTypeBinding) type;
            typeVariables = sourceTypeBinding.typeVariables;
        } else if (type instanceof BinaryTypeBinding) {
            BinaryTypeBinding binaryTypeBinding = (BinaryTypeBinding) type;
            if (this.mustResolve)
                typeVariables = binaryTypeBinding.typeVariables(); // TODO (frederic) verify performance
        }
        if (patternTypeArguments[depth] != null && patternTypeArguments[depth].length > 0
                && typeVariables != null && typeVariables.length > 0) {
            if (typeVariables.length != patternTypeArguments[depth].length)
                return IMPOSSIBLE_MATCH;
        }
        // TODO (frederic) do we need to verify each parameter?
        return level; // we can't do better
    }

    // raw type always match
    if (type.isRawType()) {
        return level;
    }

    // Standard types (i.e. neither generic nor parameterized nor raw types)
    // cannot match pattern with type parameters or arguments
    TypeBinding leafType = type.leafComponentType();
    if (!leafType.isParameterizedType()) {
        return (patternTypeArguments[depth] == null || patternTypeArguments[depth].length == 0) ? level
                : IMPOSSIBLE_MATCH;
    }

    // Parameterized type
    ParameterizedTypeBinding paramTypeBinding = (ParameterizedTypeBinding) leafType;

    // Compare arguments only if there ones on both sides
    if (patternTypeArguments[depth] != null && patternTypeArguments[depth].length > 0
            && paramTypeBinding.arguments != null && paramTypeBinding.arguments.length > 0) {

        // type parameters length must match at least specified type names length
        int length = patternTypeArguments[depth].length;
        if (paramTypeBinding.arguments.length != length)
            return IMPOSSIBLE_MATCH;

        // verify each pattern type parameter
        nextTypeArgument: for (int i = 0; i < length; i++) {
            char[] patternTypeArgument = patternTypeArguments[depth][i];
            TypeBinding argTypeBinding = paramTypeBinding.arguments[i];
            // get corresponding pattern wildcard
            switch (patternTypeArgument[0]) {
            case Signature.C_STAR: // unbound parameter always match
            case Signature.C_SUPER: // needs pattern type parameter binding
                // skip to next type argument as it will be resolved later
                continue nextTypeArgument;
            case Signature.C_EXTENDS:
                // remove wildcard from patter type argument
                patternTypeArgument = CharOperation.subarray(patternTypeArgument, 1,
                        patternTypeArgument.length);
                break;
            default:
                // no wildcard
                break;
            }
            // get pattern type argument from its signature
            patternTypeArgument = Signature.toCharArray(patternTypeArgument);
            if (!this.isCaseSensitive)
                patternTypeArgument = CharOperation.toLowerCase(patternTypeArgument);
            boolean patternTypeArgHasAnyChars = CharOperation.contains(new char[] { '*', '?' },
                    patternTypeArgument);

            // Verify that names match...
            // ...special case for wildcard
            if (argTypeBinding instanceof CaptureBinding) {
                WildcardBinding capturedWildcard = ((CaptureBinding) argTypeBinding).wildcard;
                if (capturedWildcard != null)
                    argTypeBinding = capturedWildcard;
            }
            if (argTypeBinding.isWildcard()) {
                WildcardBinding wildcardBinding = (WildcardBinding) argTypeBinding;
                switch (wildcardBinding.boundKind) {
                case Wildcard.EXTENDS:
                    // Invalid if type argument is not exact
                    if (patternTypeArgHasAnyChars)
                        return impossible;
                    continue nextTypeArgument;
                case Wildcard.UNBOUND:
                    // there's no bound name to match => valid
                    continue nextTypeArgument;
                }
                // Look if bound name match pattern type argument
                ReferenceBinding boundBinding = (ReferenceBinding) wildcardBinding.bound;
                if (CharOperation.match(patternTypeArgument, boundBinding.shortReadableName(),
                        this.isCaseSensitive)
                        || CharOperation.match(patternTypeArgument, boundBinding.readableName(),
                                this.isCaseSensitive)) {
                    // found name in hierarchy => match
                    continue nextTypeArgument;
                }

                // If pattern is not exact then match fails
                if (patternTypeArgHasAnyChars)
                    return impossible;

                // Look for bound name in type argument superclasses
                boundBinding = boundBinding.superclass();
                while (boundBinding != null) {
                    if (CharOperation.equals(patternTypeArgument, boundBinding.shortReadableName(),
                            this.isCaseSensitive)
                            || CharOperation.equals(patternTypeArgument, boundBinding.readableName(),
                                    this.isCaseSensitive)) {
                        // found name in hierarchy => match
                        continue nextTypeArgument;
                    } else if (boundBinding.isLocalType() || boundBinding.isMemberType()) {
                        // for local or member type, verify also source name (bug 81084)
                        if (CharOperation.match(patternTypeArgument, boundBinding.sourceName(),
                                this.isCaseSensitive))
                            continue nextTypeArgument;
                    }
                    boundBinding = boundBinding.superclass();
                }
                return impossible;
            }

            // See if names match
            if (CharOperation.match(patternTypeArgument, argTypeBinding.shortReadableName(),
                    this.isCaseSensitive)
                    || CharOperation.match(patternTypeArgument, argTypeBinding.readableName(),
                            this.isCaseSensitive)) {
                continue nextTypeArgument;
            } else if (argTypeBinding.isLocalType() || argTypeBinding.isMemberType()) {
                // for local or member type, verify also source name (bug 81084)
                if (CharOperation.match(patternTypeArgument, argTypeBinding.sourceName(), this.isCaseSensitive))
                    continue nextTypeArgument;
            }

            // If pattern is not exact then match fails
            if (patternTypeArgHasAnyChars)
                return impossible;

            // Scan hierarchy
            TypeBinding leafTypeBinding = argTypeBinding.leafComponentType();
            if (leafTypeBinding.isBaseType())
                return impossible;
            ReferenceBinding refBinding = ((ReferenceBinding) leafTypeBinding).superclass();
            while (refBinding != null) {
                if (CharOperation.equals(patternTypeArgument, refBinding.shortReadableName(),
                        this.isCaseSensitive)
                        || CharOperation.equals(patternTypeArgument, refBinding.readableName(),
                                this.isCaseSensitive)) {
                    // found name in hierarchy => match
                    continue nextTypeArgument;
                } else if (refBinding.isLocalType() || refBinding.isMemberType()) {
                    // for local or member type, verify also source name (bug 81084)
                    if (CharOperation.match(patternTypeArgument, refBinding.sourceName(), this.isCaseSensitive))
                        continue nextTypeArgument;
                }
                refBinding = refBinding.superclass();
            }
            return impossible;
        }
    }

    // Recurse on enclosing type
    TypeBinding enclosingType = paramTypeBinding.enclosingType();
    if (enclosingType != null && enclosingType.isParameterizedType() && depth < patternTypeArguments.length
            && qualificationPattern != null) {
        int lastDot = CharOperation.lastIndexOf('.', qualificationPattern);
        char[] enclosingQualificationPattern = lastDot == -1 ? null
                : CharOperation.subarray(qualificationPattern, 0, lastDot);
        char[] enclosingSimpleNamePattern = lastDot == -1 ? qualificationPattern
                : CharOperation.subarray(qualificationPattern, lastDot + 1, qualificationPattern.length);
        int enclosingLevel = resolveLevelForType(enclosingSimpleNamePattern, enclosingQualificationPattern,
                patternTypeArguments, depth + 1, enclosingType);
        if (enclosingLevel == impossible)
            return impossible;
        if (enclosingLevel == IMPOSSIBLE_MATCH)
            return IMPOSSIBLE_MATCH;
    }
    return level;
}

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

License:Open Source License

public boolean matchesDecodedKey(SearchPattern decodedPattern) {
    TypeDeclarationPattern pattern = (TypeDeclarationPattern) decodedPattern;

    // check type suffix
    if (this.typeSuffix != pattern.typeSuffix && this.typeSuffix != TYPE_SUFFIX) {
        if (!matchDifferentTypeSuffixes(this.typeSuffix, pattern.typeSuffix)) {
            return false;
        }/* w  w w.ja v a 2 s.  c o m*/
    }

    // check name
    if (!matchesName(this.simpleName, pattern.simpleName))
        return false;

    // check package - exact match only
    if (this.pkg != null && !CharOperation.equals(this.pkg, pattern.pkg, isCaseSensitive()))
        return false;

    // check enclosingTypeNames - exact match only
    if (this.enclosingTypeNames != null) {
        if (this.enclosingTypeNames.length == 0)
            return pattern.enclosingTypeNames.length == 0;
        if (this.enclosingTypeNames.length == 1 && pattern.enclosingTypeNames.length == 1)
            return CharOperation.equals(this.enclosingTypeNames[0], pattern.enclosingTypeNames[0],
                    isCaseSensitive());
        if (pattern.enclosingTypeNames == ONE_ZERO_CHAR)
            return true; // is a local or anonymous type
        return CharOperation.equals(this.enclosingTypeNames, pattern.enclosingTypeNames, isCaseSensitive());
    }
    return true;
}

From source file:org.codehaus.groovy.eclipse.codeassist.processors.GroovyProposalTypeSearchRequestor.java

License:Apache License

int computeRelevanceForCaseMatching(char[] token, char[] proposalName) {
    if (CharOperation.equals(token, proposalName, true /* do not ignore case */)) {
        return R_CASE + R_EXACT_NAME;
    } else if (CharOperation.equals(token, proposalName, false /*
                                                                * ignore
                                                                * case
                                                                */)) {
        return R_EXACT_NAME;
    }//from  w  ww . ja  va2  s  . co m
    return 0;
}