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

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

Introduction

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

Prototype

public static final int occurencesOf(char toBeFound, char[] array) 

Source Link

Document

Answers the number of occurrences of the given character in the given array, 0 if any.

Usage

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

License:Open Source License

protected void matchReportReference(ASTNode reference, IJavaElement element, IJavaElement localElement,
        IJavaElement[] otherElements, Binding elementBinding, int accuracy, MatchLocator locator)
        throws CoreException {
    long[] positions = null;
    int last = -1;
    if (reference instanceof ImportReference) {
        ImportReference importRef = (ImportReference) reference;
        positions = importRef.sourcePositions;
        last = (importRef.bits & ASTNode.OnDemand) != 0 ? positions.length : positions.length - 1;
    } else {/* w w  w . j  a v  a2  s .c  o  m*/
        TypeBinding typeBinding = null;
        if (reference instanceof QualifiedNameReference) {
            QualifiedNameReference qNameRef = (QualifiedNameReference) reference;
            positions = qNameRef.sourcePositions;
            switch (qNameRef.bits & ASTNode.RestrictiveFlagMASK) {
            case Binding.FIELD: // reading a field
                typeBinding = qNameRef.actualReceiverType;
                break;
            case Binding.TYPE: //=============only type ==============
                if (qNameRef.binding instanceof TypeBinding)
                    typeBinding = (TypeBinding) qNameRef.binding;
                break;
            case Binding.VARIABLE: //============unbound cases===========
            case Binding.TYPE | Binding.VARIABLE:
                Binding binding = qNameRef.binding;
                if (binding instanceof TypeBinding) {
                    typeBinding = (TypeBinding) binding;
                } else if (binding instanceof ProblemFieldBinding) {
                    typeBinding = qNameRef.actualReceiverType;
                    last = qNameRef.tokens.length
                            - (qNameRef.otherBindings == null ? 2 : qNameRef.otherBindings.length + 2);
                } else if (binding instanceof ProblemBinding) {
                    ProblemBinding pbBinding = (ProblemBinding) binding;
                    typeBinding = pbBinding.searchType;
                    last = CharOperation.occurencesOf('.', pbBinding.name);
                }
                break;
            }
        } else if (reference instanceof QualifiedTypeReference) {
            QualifiedTypeReference qTypeRef = (QualifiedTypeReference) reference;
            positions = qTypeRef.sourcePositions;
            typeBinding = qTypeRef.resolvedType;
        } else if (reference instanceof JavadocSingleTypeReference) {
            JavadocSingleTypeReference jsTypeRef = (JavadocSingleTypeReference) reference;
            positions = new long[1];
            positions[0] = (((long) jsTypeRef.sourceStart) << 32) + jsTypeRef.sourceEnd;
            typeBinding = jsTypeRef.resolvedType;
        }
        if (positions == null)
            return;
        if (typeBinding instanceof ArrayBinding)
            typeBinding = ((ArrayBinding) typeBinding).leafComponentType;
        if (typeBinding instanceof ProblemReferenceBinding)
            typeBinding = ((ProblemReferenceBinding) typeBinding).closestMatch();
        if (typeBinding instanceof ReferenceBinding) {
            PackageBinding pkgBinding = ((ReferenceBinding) typeBinding).fPackage;
            if (pkgBinding != null)
                last = pkgBinding.compoundName.length;
        }
        // Do not report qualified references which are only enclosing type
        // (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=91078)
        ReferenceBinding enclosingType = typeBinding == null ? null : typeBinding.enclosingType();
        if (enclosingType != null) {
            int length = positions.length;
            while (enclosingType != null && length > 0) {
                length--;
                enclosingType = enclosingType.enclosingType();
            }
            if (length <= 1)
                return;
        }
    }
    if (last == -1) {
        last = this.pattern.segments.length;
    }
    if (last == 0)
        return;
    if (last > positions.length)
        last = positions.length;
    int sourceStart = (int) (positions[0] >>> 32);
    int sourceEnd = ((int) positions[last - 1]);
    PackageReferenceMatch packageReferenceMatch = locator.newPackageReferenceMatch(element, accuracy,
            sourceStart, sourceEnd - sourceStart + 1, reference);
    packageReferenceMatch.setLocalElement(localElement);
    this.match = packageReferenceMatch;
    locator.report(this.match);
}

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

License:Open Source License

protected int resolveLevel(QualifiedNameReference qNameRef) {
    TypeBinding typeBinding = null;/* ww  w. ja  v  a  2  s. co  m*/
    switch (qNameRef.bits & ASTNode.RestrictiveFlagMASK) {
    case Binding.FIELD: // reading a field
        if (qNameRef.tokens.length < (qNameRef.otherBindings == null ? 3 : qNameRef.otherBindings.length + 3))
            return IMPOSSIBLE_MATCH; // must be at least p1.A.x
        typeBinding = qNameRef.actualReceiverType;
        break;
    case Binding.LOCAL: // reading a local variable
        return IMPOSSIBLE_MATCH; // no package match in it
    case Binding.TYPE: //=============only type ==============
        if (qNameRef.binding instanceof TypeBinding)
            typeBinding = (TypeBinding) qNameRef.binding;
        break;
    /*
     * Handling of unbound qualified name references. The match may reside in the resolved fragment,
     * which is recorded inside the problem binding, along with the portion of the name until it became a problem.
     */
    case Binding.VARIABLE: //============unbound cases===========
    case Binding.TYPE | Binding.VARIABLE:
        Binding binding = qNameRef.binding;
        if (binding instanceof ProblemReferenceBinding) {
            typeBinding = (TypeBinding) binding;
        } else if (binding instanceof ProblemFieldBinding) {
            if (qNameRef.tokens.length < (qNameRef.otherBindings == null ? 3
                    : qNameRef.otherBindings.length + 3))
                return IMPOSSIBLE_MATCH; // must be at least p1.A.x
            typeBinding = qNameRef.actualReceiverType;
        } else if (binding instanceof ProblemBinding) {
            ProblemBinding pbBinding = (ProblemBinding) binding;
            if (CharOperation.occurencesOf('.', pbBinding.name) <= 0) // index of last bound token is one before the pb token
                return INACCURATE_MATCH;
            typeBinding = pbBinding.searchType;
        }
        break;
    }
    return resolveLevel(typeBinding);
}

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

License:Open Source License

protected void reportDeclaration(ASTNode reference, IJavaElement element, MatchLocator locator,
        SimpleSet knownTypes) throws CoreException {
    int maxType = -1;
    TypeBinding typeBinding = null;//from w w w .java  2 s  . co m
    if (reference instanceof TypeReference) {
        typeBinding = ((TypeReference) reference).resolvedType;
        maxType = Integer.MAX_VALUE;
    } else if (reference instanceof QualifiedNameReference) {
        QualifiedNameReference qNameRef = (QualifiedNameReference) reference;
        Binding binding = qNameRef.binding;
        maxType = qNameRef.tokens.length - 1;
        switch (qNameRef.bits & ASTNode.RestrictiveFlagMASK) {
        case Binding.FIELD: // reading a field
            typeBinding = qNameRef.actualReceiverType;
            maxType -= qNameRef.otherBindings == null ? 1 : qNameRef.otherBindings.length + 1;
            break;
        case Binding.TYPE: //=============only type ==============
            if (binding instanceof TypeBinding)
                typeBinding = (TypeBinding) binding;
            break;
        case Binding.VARIABLE: //============unbound cases===========
        case Binding.TYPE | Binding.VARIABLE:
            if (binding instanceof ProblemFieldBinding) {
                typeBinding = qNameRef.actualReceiverType;
                maxType -= qNameRef.otherBindings == null ? 1 : qNameRef.otherBindings.length + 1;
            } else if (binding instanceof ProblemBinding) {
                ProblemBinding pbBinding = (ProblemBinding) binding;
                typeBinding = pbBinding.searchType; // second chance with recorded type so far
                char[] partialQualifiedName = pbBinding.name;
                maxType = CharOperation.occurencesOf('.', partialQualifiedName) - 1; // index of last bound token is one before the pb token
                if (typeBinding == null || maxType < 0)
                    return;
            }
            break;
        }
    } else if (reference instanceof SingleNameReference) {
        typeBinding = (TypeBinding) ((SingleNameReference) reference).binding;
        maxType = 1;
    }

    if (typeBinding instanceof ArrayBinding)
        typeBinding = ((ArrayBinding) typeBinding).leafComponentType;
    if (typeBinding == null || typeBinding instanceof BaseTypeBinding)
        return;
    if (typeBinding instanceof ProblemReferenceBinding) {
        TypeBinding original = typeBinding.closestMatch();
        if (original == null)
            return; // original may not be set (bug 71279)
        typeBinding = original;
    }
    typeBinding = typeBinding.erasure();
    reportDeclaration((ReferenceBinding) typeBinding, maxType, locator, knownTypes);
}

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

License:Open Source License

protected int resolveLevel(NameReference nameRef) {
    Binding binding = nameRef.binding;

    if (nameRef instanceof SingleNameReference) {
        if (binding instanceof ProblemReferenceBinding)
            binding = ((ProblemReferenceBinding) binding).closestMatch();
        if (binding instanceof ReferenceBinding)
            return resolveLevelForType((ReferenceBinding) binding);
        return binding == null || binding instanceof ProblemBinding ? INACCURATE_MATCH : IMPOSSIBLE_MATCH;
    }//  w  w  w. j a v  a  2 s  . c  om

    TypeBinding typeBinding = null;
    QualifiedNameReference qNameRef = (QualifiedNameReference) nameRef;
    switch (qNameRef.bits & ASTNode.RestrictiveFlagMASK) {
    case Binding.FIELD: // reading a field
        if (qNameRef.tokens.length < (qNameRef.otherBindings == null ? 2 : qNameRef.otherBindings.length + 2))
            return IMPOSSIBLE_MATCH; // must be at least A.x
        typeBinding = nameRef.actualReceiverType;
        break;
    case Binding.LOCAL: // reading a local variable
        return IMPOSSIBLE_MATCH; // no type match in it
    case Binding.TYPE: //=============only type ==============
        if (binding instanceof TypeBinding)
            typeBinding = (TypeBinding) binding;
        break;
    /*
     * Handling of unbound qualified name references. The match may reside in the resolved fragment,
     * which is recorded inside the problem binding, along with the portion of the name until it became a problem.
     */
    case Binding.VARIABLE: //============unbound cases===========
    case Binding.TYPE | Binding.VARIABLE:
        if (binding instanceof ProblemReferenceBinding) {
            typeBinding = (TypeBinding) binding;
        } else if (binding instanceof ProblemFieldBinding) {
            if (qNameRef.tokens.length < (qNameRef.otherBindings == null ? 2
                    : qNameRef.otherBindings.length + 2))
                return IMPOSSIBLE_MATCH; // must be at least A.x
            typeBinding = nameRef.actualReceiverType;
        } else if (binding instanceof ProblemBinding) {
            ProblemBinding pbBinding = (ProblemBinding) binding;
            if (CharOperation.occurencesOf('.', pbBinding.name) <= 0) // index of last bound token is one before the pb token
                return INACCURATE_MATCH;
            typeBinding = pbBinding.searchType;
        }
        break;
    }
    return resolveLevel(typeBinding);
}

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

License:Open Source License

public TypeReferencePattern(char[] qualification, char[] simpleName, int matchRule) {
    this(matchRule);

    this.qualification = this.isCaseSensitive ? qualification : CharOperation.toLowerCase(qualification);
    this.simpleName = (this.isCaseSensitive || this.isCamelCase) ? simpleName
            : CharOperation.toLowerCase(simpleName);

    if (simpleName == null)
        this.segments = this.qualification == null ? ONE_STAR_CHAR
                : CharOperation.splitOn('.', this.qualification);
    else/*from w  w  w.j a  v  a  2  s. c  o m*/
        this.segments = null;

    if (this.segments == null)
        if (this.qualification == null)
            this.segmentsSize = 0;
        else
            this.segmentsSize = CharOperation.occurencesOf('.', this.qualification) + 1;
    else
        this.segmentsSize = this.segments.length;

    this.mustResolve = true; // always resolve (in case of a simple name reference being a potential match)
}

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

License:Open Source License

public TypeReferencePattern(char[] qualification, char[] simpleName, String typeSignature, int limitTo,
        char typeSuffix, int matchRule) {
    this(qualification, simpleName, matchRule);
    this.typeSuffix = typeSuffix;
    if (typeSignature != null) {
        // store type signatures and arguments
        this.typeSignatures = Util.splitTypeLevelsSignature(typeSignature);
        setTypeArguments(Util.getAllTypeArguments(this.typeSignatures));
        if (hasTypeArguments()) {
            this.segmentsSize = getTypeArguments().length
                    + CharOperation.occurencesOf('/', this.typeSignatures[0]) - 1;
        }//from w ww  . j  a  va 2  s.c  o m
    }
    this.fineGrain = limitTo & 0xFFFFFFF0;
    if (this.fineGrain == IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE) {
        this.categories = CATEGORIES_ANNOT_REF;
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lookup.SyntheticBaseCallSurrogate.java

License:Open Source License

public static char[] stripBaseCallName(char[] selector) {
    if (CharOperation.occurencesOf('$', selector) == 2) {
        char[][] tokens = CharOperation.splitOn('$', selector);
        selector = tokens[1]; // pick m from _OT$m$base
    }//  ww w.  ja v a 2  s.  c om
    return selector;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.PredicateGenerator.java

License:Open Source License

private boolean isBindingPredicateName(char[] name) {
    // names have this format: _OT$[base_]when$rmeth${after,before,replace}$bmeth
    return CharOperation.occurencesOf('$', name) == 4;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.PredicateGenerator.java

License:Open Source License

/** Find the next outer predicate to be invoked from `pred'. */
private static Expression getOuterPredicateCheck(MethodDeclaration pred, AstGenerator gen) {
    char[] predName = pred.selector;
    boolean isBasePredicate = CharOperation.prefixEquals(BASE_PREDICATE_PREFIX, pred.selector);
    ReferenceBinding site = pred.binding.declaringClass;
    ReferenceBinding currentType = site;

    while (true) {
        char[] outerName = null;
        TypeBinding[] outerParams = null;
        Expression[] outerArgs = null;
        int dollarCount = CharOperation.occurencesOf('$', predName);
        ReferenceBinding declaringClass = currentType; // assume this until we know better
        if (dollarCount == 4) {
            // binding predicate -> method predicate
            int dollarPos = CharOperation.lastIndexOf('$', predName);
            dollarPos = CharOperation.lastIndexOf('$', predName, 0, dollarPos - 1);
            outerName = CharOperation.subarray(predName, 0, dollarPos);
            outerParams = getArgTypesFromBindingPredicate(pred);
            outerArgs = makeArgExpressions(pred, outerParams.length, null, gen);
        } else {// w  ww. ja v  a  2 s .  c o  m
            // next outer is a type level predicate:
            if (dollarCount >= 2) {
                // {binding,method} predicate -> role level predicate
                int dollarPos = CharOperation.lastIndexOf('$', predName);
                outerName = CharOperation.subarray(predName, 0, dollarPos);
            } else {
                // type level guard can travel outwards without changing the name:
                outerName = predName;
                declaringClass = currentType.enclosingType();
                if (declaringClass == null || !declaringClass.isTeam())
                    return null; // travelling beyond outermost team
            }
            if (isBasePredicate) {
                // base arg only, drop binding/method args.
                outerParams = new TypeBinding[] { pred.binding.parameters[0] };
                outerArgs = new Expression[] { gen.singleNameReference(BASE) };
            } else {
                // non-base type-level guard: need no arg
                outerParams = Binding.NO_PARAMETERS;
            }
        }
        if (outerName == null)
            return null;
        MethodBinding outerMethod = TypeAnalyzer.findMethod(pred.scope, declaringClass, outerName, outerParams);
        if (outerMethod.isValidBinding())
            return gen.messageSend(genOuterReceiver(outerMethod.declaringClass, site, pred.isStatic(), gen),
                    outerMethod.selector, outerArgs);
        currentType = declaringClass;
        predName = outerName;
    }
}