private boolean haveTypeAnnotations(
     TypeBinding baseType,
     TypeBinding someType,
     TypeBinding[] someTypes,
     AnnotationBinding[] annotations) {
   if (baseType != null && baseType.hasTypeAnnotations()) return true;
   if (someType != null && someType.hasTypeAnnotations()) return true;
   for (int i = 0, length = annotations == null ? 0 : annotations.length; i < length; i++)
     if (annotations[i] != null) return true;
   for (int i = 0, length = someTypes == null ? 0 : someTypes.length; i < length; i++)
     if (someTypes[i].hasTypeAnnotations()) return true;
   return false;
 }
  /* This method replaces the version that used to sit in LE. The parameter `annotations' is a flattened sequence of annotations,
     where each dimension's annotations end with a sentinel null. Leaf type can be an already annotated type.

     See ArrayBinding.swapUnresolved for further special case handling if incoming leafType is a URB that would resolve to a raw
     type later.
  */
  public ArrayBinding getArrayType(
      TypeBinding leafType, int dimensions, AnnotationBinding[] annotations) {

    ArrayBinding nakedType = null;
    TypeBinding[] derivedTypes = getDerivedTypes(leafType);
    for (int i = 0, length = derivedTypes.length; i < length; i++) {
      TypeBinding derivedType = derivedTypes[i];
      if (derivedType == null) break;
      if (!derivedType.isArrayType()
          || derivedType.dimensions() != dimensions
          || derivedType.leafComponentType() != leafType) // $IDENTITY-COMPARISON$
      continue;
      if (Util.effectivelyEqual(derivedType.getTypeAnnotations(), annotations))
        return (ArrayBinding) derivedType;
      if (!derivedType.hasTypeAnnotations()) nakedType = (ArrayBinding) derivedType;
    }
    if (nakedType == null) nakedType = super.getArrayType(leafType, dimensions);

    if (!haveTypeAnnotations(leafType, annotations)) return nakedType;

    ArrayBinding arrayType = new ArrayBinding(leafType, dimensions, this.environment);
    arrayType.id = nakedType.id;
    arrayType.setTypeAnnotations(annotations, this.isAnnotationBasedNullAnalysisEnabled);
    return (ArrayBinding) cacheDerivedType(leafType, nakedType, arrayType);
  }
  public RawTypeBinding getRawType(
      ReferenceBinding genericType,
      ReferenceBinding enclosingType,
      AnnotationBinding[] annotations) {

    if (genericType.hasTypeAnnotations()) throw new IllegalStateException();

    RawTypeBinding nakedType = null;
    TypeBinding[] derivedTypes = getDerivedTypes(genericType);
    for (int i = 0, length = derivedTypes.length; i < length; i++) {
      TypeBinding derivedType = derivedTypes[i];
      if (derivedType == null) break;
      if (!derivedType.isRawType()
          || derivedType.actualType() != genericType
          || derivedType.enclosingType() != enclosingType) // $IDENTITY-COMPARISON$
      continue;
      if (Util.effectivelyEqual(derivedType.getTypeAnnotations(), annotations))
        return (RawTypeBinding) derivedType;
      if (!derivedType.hasTypeAnnotations()) nakedType = (RawTypeBinding) derivedType;
    }
    if (nakedType == null) nakedType = super.getRawType(genericType, enclosingType);

    if (!haveTypeAnnotations(genericType, enclosingType, null, annotations)) return nakedType;

    RawTypeBinding rawType = new RawTypeBinding(genericType, enclosingType, this.environment);
    rawType.id = nakedType.id;
    rawType.setTypeAnnotations(annotations, this.isAnnotationBasedNullAnalysisEnabled);
    return (RawTypeBinding) cacheDerivedType(genericType, nakedType, rawType);
  }
  // Given a type, return all its annotated variants: parameter may be annotated.
  public TypeBinding[] getAnnotatedTypes(TypeBinding type) {

    TypeBinding[] derivedTypes = getDerivedTypes(type);
    final int length = derivedTypes.length;
    TypeBinding[] annotatedVersions = new TypeBinding[length];
    int versions = 0;
    for (int i = 0; i < length; i++) {
      final TypeBinding derivedType = derivedTypes[i];
      if (derivedType == null) break;
      if (!derivedType.hasTypeAnnotations()) continue;
      if (derivedType.id == type.id) annotatedVersions[versions++] = derivedType;
    }

    if (versions != length)
      System.arraycopy(
          annotatedVersions, 0, annotatedVersions = new TypeBinding[versions], 0, versions);
    return annotatedVersions;
  }
  public WildcardBinding getWildcard(
      ReferenceBinding genericType,
      int rank,
      TypeBinding bound,
      TypeBinding[] otherBounds,
      int boundKind,
      AnnotationBinding[] annotations) {

    if (genericType == null) // pseudo wildcard denoting composite bounds for lub computation
    genericType = ReferenceBinding.LUB_GENERIC;

    if (genericType.hasTypeAnnotations()) throw new IllegalStateException();

    WildcardBinding nakedType = null;
    TypeBinding[] derivedTypes = getDerivedTypes(genericType);
    for (int i = 0, length = derivedTypes.length; i < length; i++) {
      TypeBinding derivedType = derivedTypes[i];
      if (derivedType == null) break;
      if (!derivedType.isWildcard()
          || derivedType.actualType() != genericType
          || derivedType.rank() != rank) // $IDENTITY-COMPARISON$
      continue;
      if (derivedType.boundKind() != boundKind
          || derivedType.bound() != bound
          || !Util.effectivelyEqual(
              derivedType.additionalBounds(), otherBounds)) // $IDENTITY-COMPARISON$
      continue;
      if (Util.effectivelyEqual(derivedType.getTypeAnnotations(), annotations))
        return (WildcardBinding) derivedType;
      if (!derivedType.hasTypeAnnotations()) nakedType = (WildcardBinding) derivedType;
    }

    if (nakedType == null)
      nakedType = super.getWildcard(genericType, rank, bound, otherBounds, boundKind);

    if (!haveTypeAnnotations(genericType, bound, otherBounds, annotations)) return nakedType;

    WildcardBinding wildcard =
        new WildcardBinding(genericType, rank, bound, otherBounds, boundKind, this.environment);
    wildcard.id = nakedType.id;
    wildcard.setTypeAnnotations(annotations, this.isAnnotationBasedNullAnalysisEnabled);
    return (WildcardBinding) cacheDerivedType(genericType, nakedType, wildcard);
  }
  /* Private subroutine for public APIs. Create an annotated version of the type. To materialize the annotated version, we can't use new since
     this is a general purpose method designed to deal type bindings of all types. "Clone" the incoming type, specializing for any enclosing type
     that may itself be possibly be annotated. This is so the binding for @Outer Outer.Inner != Outer.@Inner Inner != @Outer Outer.@Inner Inner.
     Likewise so the bindings for @Readonly List<@NonNull String> != @Readonly List<@Nullable String> != @Readonly List<@Interned String>
  */
  private TypeBinding getAnnotatedType(
      TypeBinding type, TypeBinding enclosingType, AnnotationBinding[] annotations) {
    if (type.kind() == Binding.PARAMETERIZED_TYPE) {
      return getParameterizedType(
          type.actualType(), type.typeArguments(), (ReferenceBinding) enclosingType, annotations);
    }
    TypeBinding nakedType = null;
    TypeBinding[] derivedTypes = getDerivedTypes(type);
    for (int i = 0, length = derivedTypes.length; i < length; i++) {
      TypeBinding derivedType = derivedTypes[i];
      if (derivedType == null) break;

      if (derivedType.enclosingType() != enclosingType
          || !Util.effectivelyEqual(
              derivedType.typeArguments(), type.typeArguments())) // $IDENTITY-COMPARISON$
      continue;

      switch (type.kind()) {
        case Binding.ARRAY_TYPE:
          if (!derivedType.isArrayType()
              || derivedType.dimensions() != type.dimensions()
              || derivedType.leafComponentType()
                  != type.leafComponentType()) // $IDENTITY-COMPARISON$
          continue;
          break;
        case Binding.RAW_TYPE:
          if (!derivedType.isRawType()
              || derivedType.actualType() != type.actualType()) // $IDENTITY-COMPARISON$
          continue;
          break;
        case Binding.WILDCARD_TYPE:
          if (!derivedType.isWildcard()
              || derivedType.actualType() != type.actualType()
              || derivedType.rank() != type.rank()
              || derivedType.boundKind() != type.boundKind()) // $IDENTITY-COMPARISON$
          continue;
          if (derivedType.bound() != type.bound()
              || !Util.effectivelyEqual(
                  derivedType.additionalBounds(), type.additionalBounds())) // $IDENTITY-COMPARISON$
          continue;
          break;
        default:
          switch (derivedType.kind()) {
            case Binding.ARRAY_TYPE:
            case Binding.RAW_TYPE:
            case Binding.WILDCARD_TYPE:
            case Binding.INTERSECTION_CAST_TYPE:
            case Binding.INTERSECTION_TYPE:
              continue;
          }
          break;
      }
      if (Util.effectivelyEqual(derivedType.getTypeAnnotations(), annotations)) {
        return derivedType;
      }
      if (!derivedType.hasTypeAnnotations()) nakedType = derivedType;
    }
    if (nakedType == null) nakedType = getUnannotatedType(type);

    if (!haveTypeAnnotations(type, enclosingType, null, annotations)) return nakedType;

    TypeBinding annotatedType = type.clone(enclosingType);
    annotatedType.id = nakedType.id;
    annotatedType.setTypeAnnotations(annotations, this.isAnnotationBasedNullAnalysisEnabled);
    TypeBinding keyType;
    switch (type.kind()) {
      case Binding.ARRAY_TYPE:
        keyType = type.leafComponentType();
        break;
      case Binding.RAW_TYPE:
      case Binding.WILDCARD_TYPE:
        keyType = type.actualType();
        break;
      default:
        keyType = nakedType;
        break;
    }
    return cacheDerivedType(keyType, nakedType, annotatedType);
  }