Example usage for org.eclipse.jdt.internal.compiler.lookup ArrayBinding ArrayLength

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ArrayBinding ArrayLength

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup ArrayBinding ArrayLength.

Prototype

FieldBinding ArrayLength

To view the source code for org.eclipse.jdt.internal.compiler.lookup ArrayBinding ArrayLength.

Click Source Link

Usage

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

License:Open Source License

protected int matchField(FieldBinding field, boolean matchName) {
    if (field == null)
        return INACCURATE_MATCH;

    if (matchName && !matchesName(this.pattern.name, field.readableName()))
        return IMPOSSIBLE_MATCH;

    FieldPattern fieldPattern = (FieldPattern) this.pattern;
    ReferenceBinding receiverBinding = field.declaringClass;
    if (receiverBinding == null) {
        if (field == ArrayBinding.ArrayLength)
            // optimized case for length field of an array
            return fieldPattern.declaringQualification == null && fieldPattern.declaringSimpleName == null
                    ? ACCURATE_MATCH/*from   w  ww  .  j ava2  s  . co  m*/
                    : IMPOSSIBLE_MATCH;
        return INACCURATE_MATCH;
    }

    // Note there is no dynamic lookup for field access
    int declaringLevel = resolveLevelForType(fieldPattern.declaringSimpleName,
            fieldPattern.declaringQualification, receiverBinding);
    if (declaringLevel == IMPOSSIBLE_MATCH)
        return IMPOSSIBLE_MATCH;

    // look at field type only if declaring type is not specified
    if (fieldPattern.declaringSimpleName == null)
        return declaringLevel;

    // get real field binding
    FieldBinding fieldBinding = field;
    if (field instanceof ParameterizedFieldBinding) {
        fieldBinding = ((ParameterizedFieldBinding) field).originalField;
    }

    int typeLevel = resolveLevelForType(fieldBinding.type);
    return declaringLevel > typeLevel ? typeLevel : declaringLevel; // return the weaker match
}

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

License:Open Source License

protected void reportDeclaration(FieldBinding fieldBinding, MatchLocator locator, SimpleSet knownFields)
        throws CoreException {
    // ignore length field
    if (fieldBinding == ArrayBinding.ArrayLength)
        return;/*from w w  w .  java  2  s .  com*/

    ReferenceBinding declaringClass = fieldBinding.declaringClass;
    IType type = locator.lookupType(declaringClass);
    if (type == null)
        return; // case of a secondary type

    char[] bindingName = fieldBinding.name;
    IField field = type.getField(new String(bindingName));
    if (knownFields.addIfNotIncluded(field) == null)
        return;

    IResource resource = type.getResource();
    boolean isBinary = type.isBinary();
    IBinaryType info = null;
    if (isBinary) {
        if (resource == null)
            resource = type.getJavaProject().getProject();
        info = locator.getBinaryInfo((org.eclipse.jdt.internal.core.ClassFile) type.getClassFile(), resource);
        locator.reportBinaryMemberDeclaration(resource, field, fieldBinding, info, SearchMatch.A_ACCURATE);
    } else {
        if (declaringClass instanceof ParameterizedTypeBinding)
            declaringClass = ((ParameterizedTypeBinding) declaringClass).genericType();
        ClassScope scope = ((SourceTypeBinding) declaringClass).scope;
        if (scope != null) {
            TypeDeclaration typeDecl = scope.referenceContext;
            FieldDeclaration fieldDecl = null;
            FieldDeclaration[] fieldDecls = typeDecl.fields;
            int length = fieldDecls == null ? 0 : fieldDecls.length;
            for (int i = 0; i < length; i++) {
                if (CharOperation.equals(bindingName, fieldDecls[i].name)) {
                    fieldDecl = fieldDecls[i];
                    break;
                }
            }
            if (fieldDecl != null) {
                int offset = fieldDecl.sourceStart;
                this.match = new FieldDeclarationMatch(((JavaElement) field).resolved(fieldBinding),
                        SearchMatch.A_ACCURATE, offset, fieldDecl.sourceEnd - offset + 1,
                        locator.getParticipant(), resource);
                locator.report(this.match);
            }
        }
    }
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

public FieldBinding findField(TypeBinding receiverType, char[] fieldName, InvocationSite invocationSite,
        boolean needResolve, boolean invisibleFieldsOk) {

    CompilationUnitScope unitScope = compilationUnitScope();
    unitScope.recordTypeReference(receiverType);

    checkArrayField: {// w  ww.j  a  va  2 s.  co m
        TypeBinding leafType;
        switch (receiverType.kind()) {
        case Binding.BASE_TYPE:
            return null;
        case Binding.WILDCARD_TYPE:
        case Binding.INTERSECTION_TYPE:
        case Binding.TYPE_PARAMETER: // capture
            TypeBinding receiverErasure = receiverType.erasure();
            if (!receiverErasure.isArrayType())
                break checkArrayField;
            leafType = receiverErasure.leafComponentType();
            break;
        case Binding.ARRAY_TYPE:
            leafType = receiverType.leafComponentType();
            break;
        default:
            break checkArrayField;
        }
        if (leafType instanceof ReferenceBinding)
            if (!((ReferenceBinding) leafType).canBeSeenBy(this))
                return new ProblemFieldBinding((ReferenceBinding) leafType, fieldName,
                        ProblemReasons.ReceiverTypeNotVisible);
        if (CharOperation.equals(fieldName, TypeConstants.LENGTH)) {
            if ((leafType.tagBits & TagBits.HasMissingType) != 0) {
                return new ProblemFieldBinding(ArrayBinding.ArrayLength, null, fieldName,
                        ProblemReasons.NotFound);
            }
            return ArrayBinding.ArrayLength;
        }
        return null;
    }

    ReferenceBinding currentType = (ReferenceBinding) receiverType;
    if (!currentType.canBeSeenBy(this))
        return new ProblemFieldBinding(currentType, fieldName, ProblemReasons.ReceiverTypeNotVisible);

    currentType.initializeForStaticImports();
    FieldBinding field = currentType.getField(fieldName, needResolve);
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=316456
    boolean insideTypeAnnotations = this instanceof MethodScope && ((MethodScope) this).insideTypeAnnotation;
    if (field != null) {
        if (invisibleFieldsOk) {
            return field;
        }
        if (invocationSite == null || insideTypeAnnotations ? field.canBeSeenBy(getCurrentPackage())
                : field.canBeSeenBy(currentType, invocationSite, this))
            return field;
        return new ProblemFieldBinding(field /* closest match*/, field.declaringClass, fieldName,
                ProblemReasons.NotVisible);
    }
    // collect all superinterfaces of receiverType until the field is found in a supertype
    ReferenceBinding[] interfacesToVisit = null;
    int nextPosition = 0;
    FieldBinding visibleField = null;
    boolean keepLooking = true;
    FieldBinding notVisibleField = null;
    // we could hold onto the not visible field for extra error reporting
    while (keepLooking) {
        ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
        if (itsInterfaces != null && itsInterfaces != Binding.NO_SUPERINTERFACES) {
            if (interfacesToVisit == null) {
                interfacesToVisit = itsInterfaces;
                nextPosition = interfacesToVisit.length;
            } else {
                int itsLength = itsInterfaces.length;
                if (nextPosition + itsLength >= interfacesToVisit.length)
                    System.arraycopy(interfacesToVisit, 0,
                            interfacesToVisit = new ReferenceBinding[nextPosition + itsLength + 5], 0,
                            nextPosition);
                nextInterface: for (int a = 0; a < itsLength; a++) {
                    ReferenceBinding next = itsInterfaces[a];
                    for (int b = 0; b < nextPosition; b++)
                        if (next == interfacesToVisit[b])
                            continue nextInterface;
                    interfacesToVisit[nextPosition++] = next;
                }
            }
        }
        if ((currentType = currentType.superclass()) == null)
            break;

        unitScope.recordTypeReference(currentType);
        currentType.initializeForStaticImports();
        currentType = (ReferenceBinding) currentType.capture(this,
                invocationSite == null ? 0 : invocationSite.sourceEnd());
        if ((field = currentType.getField(fieldName, needResolve)) != null) {
            if (invisibleFieldsOk) {
                return field;
            }
            keepLooking = false;
            if (field.canBeSeenBy(receiverType, invocationSite, this)) {
                if (visibleField == null)
                    visibleField = field;
                else
                    return new ProblemFieldBinding(visibleField /* closest match*/, visibleField.declaringClass,
                            fieldName, ProblemReasons.Ambiguous);
            } else {
                if (notVisibleField == null)
                    notVisibleField = field;
            }
        }
    }

    // walk all visible interfaces to find ambiguous references
    if (interfacesToVisit != null) {
        ProblemFieldBinding ambiguous = null;
        done: for (int i = 0; i < nextPosition; i++) {
            ReferenceBinding anInterface = interfacesToVisit[i];
            unitScope.recordTypeReference(anInterface);
            // no need to capture rcv interface, since member field is going to be static anyway
            if ((field = anInterface.getField(fieldName, true /*resolve*/)) != null) {
                if (invisibleFieldsOk) {
                    return field;
                }
                if (visibleField == null) {
                    visibleField = field;
                } else {
                    ambiguous = new ProblemFieldBinding(visibleField /* closest match*/,
                            visibleField.declaringClass, fieldName, ProblemReasons.Ambiguous);
                    break done;
                }
            } else {
                ReferenceBinding[] itsInterfaces = anInterface.superInterfaces();
                if (itsInterfaces != null && itsInterfaces != Binding.NO_SUPERINTERFACES) {
                    int itsLength = itsInterfaces.length;
                    if (nextPosition + itsLength >= interfacesToVisit.length)
                        System.arraycopy(interfacesToVisit, 0,
                                interfacesToVisit = new ReferenceBinding[nextPosition + itsLength + 5], 0,
                                nextPosition);
                    nextInterface: for (int a = 0; a < itsLength; a++) {
                        ReferenceBinding next = itsInterfaces[a];
                        for (int b = 0; b < nextPosition; b++)
                            if (next == interfacesToVisit[b])
                                continue nextInterface;
                        interfacesToVisit[nextPosition++] = next;
                    }
                }
            }
        }
        if (ambiguous != null)
            return ambiguous;
    }

    if (visibleField != null)
        return visibleField;
    if (notVisibleField != null) {
        return new ProblemFieldBinding(notVisibleField, currentType, fieldName, ProblemReasons.NotVisible);
    }
    return null;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.bytecode.ConstantPoolObjectReader.java

License:Open Source License

private FieldBinding findFieldBinding(TypeBinding class_tb, char[] name, char[] descriptor) {
    if (class_tb instanceof ReferenceBinding) {
        ReferenceBinding class_rb = (ReferenceBinding) class_tb;
        return findFieldByName(class_rb, name);
    } else if (class_tb instanceof ArrayBinding) {
        if (!CharOperation.equals(name, TypeConstants.LENGTH)) {
            assert (false);
        }//from   w w  w. j av  a2  s.  com
        return ArrayBinding.ArrayLength;
    } else {
        //no fields on basetypes
        assert (false);
        return null;
    }
}