Package org.eclipse.jdt.internal.compiler.lookup

Examples of org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding


* NOTE: Cannot invoke this method with a compilation unit scope.
*/
public final boolean canBeSeenByForCodeSnippet(MethodBinding methodBinding, TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
  if (methodBinding.isPublic()) return true;

  ReferenceBinding invocationType = (ReferenceBinding) receiverType;
  if (invocationType == methodBinding.declaringClass) return true;

  if (methodBinding.isProtected()) {
    // answer true if the invocationType is the declaringClass or they are in the same package
    // OR the invocationType is a subclass of the declaringClass
    //    AND the receiverType is the invocationType or its subclass
    //    OR the method is a static method accessed directly through a type
    if (invocationType == methodBinding.declaringClass) return true;
    if (invocationType.fPackage == methodBinding.declaringClass.fPackage) return true;
    if (methodBinding.declaringClass.isSuperclassOf(invocationType)) {
      if (invocationSite.isSuperAccess()) return true;
      // receiverType can be an array binding in one case... see if you can change it
      if (receiverType instanceof ArrayBinding)
        return false;
      if (invocationType.isSuperclassOf((ReferenceBinding) receiverType))
        return true;
      if (methodBinding.isStatic())
        return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
    }
    return false;
  }

  if (methodBinding.isPrivate()) {
    // answer true if the receiverType is the declaringClass
    // AND the invocationType and the declaringClass have a common enclosingType
    if (receiverType != methodBinding.declaringClass) return false;

    if (invocationType != methodBinding.declaringClass) {
      ReferenceBinding outerInvocationType = invocationType;
      ReferenceBinding temp = outerInvocationType.enclosingType();
      while (temp != null) {
        outerInvocationType = temp;
        temp = temp.enclosingType();
      }

      ReferenceBinding outerDeclaringClass = methodBinding.declaringClass;
      temp = outerDeclaringClass.enclosingType();
      while (temp != null) {
        outerDeclaringClass = temp;
        temp = temp.enclosingType();
      }
      if (outerInvocationType != outerDeclaringClass) return false;
    }
    return true;
  }

  // isDefault()
  if (invocationType.fPackage != methodBinding.declaringClass.fPackage) return false;

  // receiverType can be an array binding in one case... see if you can change it
  if (receiverType instanceof ArrayBinding)
    return false;
  ReferenceBinding type = (ReferenceBinding) receiverType;
  PackageBinding declaringPackage = methodBinding.declaringClass.fPackage;
  TypeBinding originalDeclaringClass = methodBinding.declaringClass .original();
  do {
    if (type.isCapture()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=285002
      if (originalDeclaringClass == type.erasure().original()) return true;
    } else {
      if (originalDeclaringClass == type.original()) return true;
    }
    if (declaringPackage != type.fPackage) return false;
  } while ((type = type.superclass()) != null);
  return false;
}
View Full Code Here


  }

  if (referenceBinding.isPrivate()) {
    // answer true if the receiver and the receiverType have a common enclosingType
    // already know they are not the identical type
    ReferenceBinding outerInvocationType = receiverType;
    ReferenceBinding temp = outerInvocationType.enclosingType();
    while (temp != null) {
      outerInvocationType = temp;
      temp = temp.enclosingType();
    }

    ReferenceBinding outerDeclaringClass = referenceBinding;
    temp = outerDeclaringClass.enclosingType();
    while (temp != null) {
      outerDeclaringClass = temp;
      temp = temp.enclosingType();
    }
    return outerInvocationType == outerDeclaringClass;
View Full Code Here

    if (CharOperation.equals(fieldName, TypeConstants.LENGTH))
      return ArrayBinding.ArrayLength;
    return null;
  }

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

  FieldBinding field = currentType.getField(fieldName, true /*resolve*/);
  if (field != null) {
    if (canBeSeenByForCodeSnippet(field, currentType, invocationSite, this))
      return field;
    else
      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 lastPosition = -1;
  FieldBinding visibleField = null;
  boolean keepLooking = true;
  boolean notVisible = false; // we could hold onto the not visible field for extra error reporting
  while (keepLooking) {
    ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
    if (itsInterfaces != Binding.NO_SUPERINTERFACES) {
      if (interfacesToVisit == null)
        interfacesToVisit = new ReferenceBinding[5][];
      if (++lastPosition == interfacesToVisit.length)
        System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[lastPosition * 2][], 0, lastPosition);
      interfacesToVisit[lastPosition] = itsInterfaces;
    }
    if ((currentType = currentType.superclass()) == null)
      break;

    if ((field = currentType.getField(fieldName, true /*resolve*/)) != null) {
      keepLooking = false;
      if (canBeSeenByForCodeSnippet(field, receiverType, invocationSite, this)) {
        if (visibleField == null)
          visibleField = field;
        else
          return new ProblemFieldBinding(visibleField, visibleField.declaringClass, fieldName, ProblemReasons.Ambiguous);
      } else {
        notVisible = true;
      }
    }
  }

  // walk all visible interfaces to find ambiguous references
  if (interfacesToVisit != null) {
    ProblemFieldBinding ambiguous = null;
    org.eclipse.jdt.internal.compiler.util.SimpleSet interfacesSeen = new org.eclipse.jdt.internal.compiler.util.SimpleSet(lastPosition * 2);
    done : for (int i = 0; i <= lastPosition; i++) {
      ReferenceBinding[] interfaces = interfacesToVisit[i];
      for (int j = 0, length = interfaces.length; j < length; j++) {
        ReferenceBinding anInterface = interfaces[j];
        if (interfacesSeen.addIfNotIncluded(anInterface) == anInterface) {
          // if interface as not already been visited
          if ((field = anInterface.getField(fieldName, true /*resolve*/)) != null) {
            if (visibleField == null) {
              visibleField = field;
            } else {
              ambiguous = new ProblemFieldBinding(visibleField, visibleField.declaringClass, fieldName, ProblemReasons.Ambiguous);
              break done;
            }
          } else {
            ReferenceBinding[] itsInterfaces = anInterface.superInterfaces();
            if (itsInterfaces != Binding.NO_SUPERINTERFACES) {
              if (++lastPosition == interfacesToVisit.length)
                System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[lastPosition * 2][], 0, lastPosition);
              interfacesToVisit[lastPosition] = itsInterfaces;
            }
View Full Code Here

  return methodBinding;
}

// Internal use only
public MethodBinding findMethodForArray(ArrayBinding receiverType, char[] selector, TypeBinding[] argumentTypes, InvocationSite invocationSite) {
  ReferenceBinding object = getJavaLangObject();
  MethodBinding methodBinding = object.getExactMethod(selector, argumentTypes, null);
  if (methodBinding != null) {
    // handle the method clone() specially... cannot be protected or throw exceptions
    if (argumentTypes == Binding.NO_PARAMETERS && CharOperation.equals(selector, TypeConstants.CLONE))
      return new MethodBinding((methodBinding.modifiers & ~ClassFileConstants.AccProtected) | ClassFileConstants.AccPublic, TypeConstants.CLONE, methodBinding.returnType, argumentTypes, null, object);
    if (canBeSeenByForCodeSnippet(methodBinding, receiverType, invocationSite, this))
View Full Code Here

    return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, currentIndex), null, ProblemReasons.NotFound);
  }

  // know binding is now a ReferenceBinding
  while (currentIndex < length) {
    ReferenceBinding typeBinding = (ReferenceBinding) binding;
    char[] nextName = compoundName[currentIndex++];
    invocationSite.setFieldIndex(currentIndex);
    if ((binding = findFieldForCodeSnippet(typeBinding, nextName, invocationSite)) != null) {
      if (!binding.isValidBinding()) {
        return new ProblemFieldBinding(
View Full Code Here

        } else {
          expectedCollectionType = upperScope.createArrayType(elementType, 1);
          this.collection.computeConversion(this.scope, expectedCollectionType, collectionType);
        }
      } else if (collectionType instanceof ReferenceBinding) {
        ReferenceBinding iterableType = ((ReferenceBinding)collectionType).findSuperTypeOriginatingFrom(T_JavaLangIterable, false /*Iterable is not a class*/);
        if (iterableType == null && isTargetJsr14) {
          iterableType = ((ReferenceBinding)collectionType).findSuperTypeOriginatingFrom(T_JavaUtilCollection, false /*Iterable is not a class*/);
        }
        checkIterable: {
          if (iterableType == null) break checkIterable;

          this.iteratorReceiverType = collectionType.erasure();
          if (isTargetJsr14) {
            if (((ReferenceBinding)this.iteratorReceiverType).findSuperTypeOriginatingFrom(T_JavaUtilCollection, false) == null) {
              this.iteratorReceiverType = iterableType; // handle indirect inheritance thru variable secondary bound
              this.collection.computeConversion(this.scope, iterableType, collectionType);
            } else {
              this.collection.computeConversion(this.scope, collectionType, collectionType);
            }
          } else if (((ReferenceBinding)this.iteratorReceiverType).findSuperTypeOriginatingFrom(T_JavaLangIterable, false) == null) {
            this.iteratorReceiverType = iterableType; // handle indirect inheritance thru variable secondary bound
            this.collection.computeConversion(this.scope, iterableType, collectionType);
          } else {
            this.collection.computeConversion(this.scope, collectionType, collectionType);
          }

          TypeBinding[] arguments = null;
          switch (iterableType.kind()) {
            case Binding.RAW_TYPE : // for(Object o : Iterable)
              this.kind = RAW_ITERABLE;
              this.collectionElementType = this.scope.getJavaLangObject();
              if (!this.collectionElementType.isCompatibleWith(elementType)
                  && !this.scope.isBoxingCompatibleWith(this.collectionElementType, elementType)) {
                this.scope.problemReporter().notCompatibleTypesErrorInForeach(this.collection, this.collectionElementType, elementType);
              }
              // no conversion needed as only for reference types
              break checkIterable;

            case Binding.GENERIC_TYPE : // for (T t : Iterable<T>) - in case used inside Iterable itself
              arguments = iterableType.typeVariables();
              break;

            case Binding.PARAMETERIZED_TYPE : // for(E e : Iterable<E>)
              arguments = ((ParameterizedTypeBinding)iterableType).arguments;
              break;
View Full Code Here

            return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression);

          default :
            if (castType.isInterface()) {
              // ( INTERFACE ) INTERFACE
              ReferenceBinding interfaceType = (ReferenceBinding) expressionType;
              match = interfaceType.findSuperTypeOriginatingFrom(castType);
              if (match != null) {
                return checkUnsafeCast(scope, castType, interfaceType, match, false);
              }
              tagAsNeedCheckCast();
              match = castType.findSuperTypeOriginatingFrom(interfaceType);
              if (match != null) {
                return checkUnsafeCast(scope, castType, interfaceType, match, true);
              }
              if (use15specifics) {
                checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true);
                // ensure there is no collision between both interfaces: i.e. I1 extends List<String>, I2 extends List<Object>
                if (scope.compilerOptions().complianceLevel < ClassFileConstants.JDK1_7) {
                  if (interfaceType.hasIncompatibleSuperType((ReferenceBinding) castType)) {
                    return false;
                  }
                } else if (!castType.isRawType() && interfaceType.hasIncompatibleSuperType((ReferenceBinding) castType)) {
                  return false;
                }
              } else {
                // pre1.5 semantics - no covariance allowed (even if 1.5 compliant, but 1.4 source)
                // look at original methods rather than the parameterized variants at 1.4 to detect
                // covariance. Otherwise when confronted with one raw type and one parameterized type,
                // we could mistakenly detect covariance and scream foul. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=332744
                MethodBinding[] castTypeMethods = getAllOriginalInheritedMethods((ReferenceBinding) castType);
                MethodBinding[] expressionTypeMethods = getAllOriginalInheritedMethods((ReferenceBinding) expressionType);
                int exprMethodsLength = expressionTypeMethods.length;
                for (int i = 0, castMethodsLength = castTypeMethods.length; i < castMethodsLength; i++) {
                  for (int j = 0; j < exprMethodsLength; j++) {
                    if ((castTypeMethods[i].returnType != expressionTypeMethods[j].returnType)
                        && (CharOperation.equals(castTypeMethods[i].selector, expressionTypeMethods[j].selector))
                        && castTypeMethods[i].areParametersEqual(expressionTypeMethods[j])) {
                      return false;

                    }
                  }
                }
              }
              return true;
            } else {
              // ( CLASS ) INTERFACE
              if (castType.id == TypeIds.T_JavaLangObject) { // no runtime error
                tagAsUnnecessaryCast(scope, castType);
                return true;
              }
              // can only be a downcast
              tagAsNeedCheckCast();
              match = castType.findSuperTypeOriginatingFrom(expressionType);
              if (match != null) {
                return checkUnsafeCast(scope, castType, expressionType, match, true);
              }
              if (((ReferenceBinding) castType).isFinal()) {
                // no subclass for castType, thus compile-time check is invalid
                return false;
              }
              if (use15specifics) {
                checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true);
                // ensure there is no collision between both interfaces: i.e. I1 extends List<String>, I2 extends List<Object>
                if (scope.compilerOptions().complianceLevel < ClassFileConstants.JDK1_7) {
                  if (((ReferenceBinding)castType).hasIncompatibleSuperType((ReferenceBinding) expressionType)) {
                    return false;
                  }
                } else if (!castType.isRawType() && ((ReferenceBinding)castType).hasIncompatibleSuperType((ReferenceBinding) expressionType)) {
                  return false;
                }
              }
              return true;
            }
        }
      } else {
        switch (castType.kind()) {
          case Binding.ARRAY_TYPE :
            // ( ARRAY ) CLASS
            if (expressionType.id == TypeIds.T_JavaLangObject) { // potential runtime error
              if (use15specifics) checkUnsafeCast(scope, castType, expressionType, expressionType, true);
              tagAsNeedCheckCast();
              return true;
            }
            return false;

          case Binding.TYPE_PARAMETER :
            // ( TYPE_PARAMETER ) CLASS
            match = expressionType.findSuperTypeOriginatingFrom(castType);
            if (match == null) {
              checkUnsafeCast(scope, castType, expressionType, null, true);
            }
            // recurse on the type variable upper bound
            return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression);

          default :
            if (castType.isInterface()) {
              // ( INTERFACE ) CLASS
              ReferenceBinding refExprType = (ReferenceBinding) expressionType;
              match = refExprType.findSuperTypeOriginatingFrom(castType);
              if (match != null) {
                return checkUnsafeCast(scope, castType, expressionType, match, false);
              }
              // unless final a subclass may implement the interface ==> no check at compile time
              if (refExprType.isFinal()) {
                return false;
              }
              tagAsNeedCheckCast();
              match = castType.findSuperTypeOriginatingFrom(expressionType);
              if (match != null) {
                return checkUnsafeCast(scope, castType, expressionType, match, true);
              }
              if (use15specifics) {
                checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true);
                // ensure there is no collision between both interfaces: i.e. I1 extends List<String>, I2 extends List<Object>
                if (scope.compilerOptions().complianceLevel < ClassFileConstants.JDK1_7) {
                  if (refExprType.hasIncompatibleSuperType((ReferenceBinding) castType)) {
                    return false;
                  }
                } else if (!castType.isRawType() && refExprType.hasIncompatibleSuperType((ReferenceBinding) castType)) {
                  return false;
                }
              }
              return true;
            } else {
View Full Code Here

            ProblemFieldBinding problemFieldBinding = (ProblemFieldBinding) variableBinding;
            switch(problemFieldBinding.problemId()) {
              case ProblemReasons.NotVisible :
              case ProblemReasons.NonStaticReferenceInStaticContext :
              case ProblemReasons.NonStaticReferenceInConstructorInvocation :
                ReferenceBinding declaringClass = problemFieldBinding.declaringClass;
                FieldBinding exactBinding = declaringClass.getField(problemFieldBinding.name, true /*resolve*/);
                if (exactBinding != null) {
                  IVariableBinding variableBinding2 = (IVariableBinding) this.bindingTables.compilerBindingsToASTBindings.get(exactBinding);
                  if (variableBinding2 != null) {
                    return variableBinding2;
                  }
View Full Code Here

          ProblemFieldBinding problemFieldBinding = (ProblemFieldBinding) variableBinding;
          switch(problemFieldBinding.problemId()) {
            case ProblemReasons.NotVisible :
            case ProblemReasons.NonStaticReferenceInStaticContext :
            case ProblemReasons.NonStaticReferenceInConstructorInvocation :
              ReferenceBinding declaringClass = problemFieldBinding.declaringClass;
              FieldBinding exactBinding = declaringClass.getField(problemFieldBinding.name, true /*resolve*/);
              if (exactBinding != null) {
                IVariableBinding variableBinding2 = (IVariableBinding) this.bindingTables.compilerBindingsToASTBindings.get(exactBinding);
                if (variableBinding2 != null) {
                  return variableBinding2;
                }
View Full Code Here

      boolean onlyStaticFields,
      boolean notInJavadoc,
      ObjectVector localsFound,
      ObjectVector fieldsFound) {

    ReferenceBinding currentType = receiverType;
    ReferenceBinding[] interfacesToVisit = null;
    int nextPosition = 0;
    do {
      ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
      if (notInJavadoc && 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;
          }
        }
      }

      FieldBinding[] fields = currentType.availableFields();
      if(fields != null && fields.length > 0) {

        searchVisibleFields(
            fields,
            receiverType,
            scope,
            invocationSite,
            invocationScope,
            onlyStaticFields,
            localsFound,
            fieldsFound);
      }
      currentType = currentType.superclass();
    } while (notInJavadoc && currentType != null);

    if (notInJavadoc && interfacesToVisit != null) {
      for (int i = 0; i < nextPosition; i++) {
        ReferenceBinding anInterface = interfacesToVisit[i];
        FieldBinding[] fields = anInterface.availableFields();
        if(fields !=  null) {
          searchVisibleFields(
              fields,
              receiverType,
              scope,
              invocationSite,
              invocationScope,
              onlyStaticFields,
              localsFound,
              fieldsFound);
        }

        ReferenceBinding[] itsInterfaces = anInterface.superInterfaces();
        if (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;
          }
        }
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.