コード例 #1
0
  private Binding findImport(char[][] compoundName, int length) {
    recordQualifiedReference(compoundName);

    Binding binding = this.environment.getTopLevelPackage(compoundName[0]);
    int i = 1;
    foundNothingOrType:
    if (binding != null) {
      PackageBinding packageBinding = (PackageBinding) binding;
      while (i < length) {
        binding = packageBinding.getTypeOrPackage(compoundName[i++]);
        if (binding == null || !binding.isValidBinding()) {
          binding = null;
          break foundNothingOrType;
        }
        if (!(binding instanceof PackageBinding)) break foundNothingOrType;

        packageBinding = (PackageBinding) binding;
      }
      return packageBinding;
    }

    ReferenceBinding type;
    if (binding == null) {
      if (compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4)
        return new ProblemReferenceBinding(
            CharOperation.subarray(compoundName, 0, i), null, ProblemReasons.NotFound);
      type =
          findType(
              compoundName[0], this.environment.defaultPackage, this.environment.defaultPackage);
      if (type == null || !type.isValidBinding())
        return new ProblemReferenceBinding(
            CharOperation.subarray(compoundName, 0, i), null, ProblemReasons.NotFound);
      i = 1; // reset to look for member types inside the default package type
    } else {
      type = (ReferenceBinding) binding;
    }

    while (i < length) {
      type =
          (ReferenceBinding)
              this.environment.convertToRawType(
                  type, false /*do not force conversion of enclosing types*/); // type imports are
      // necessarily raw for all
      // except last
      if (!type.canBeSeenBy(this.fPackage))
        return new ProblemReferenceBinding(
            CharOperation.subarray(compoundName, 0, i), type, ProblemReasons.NotVisible);

      char[] name = compoundName[i++];
      // does not look for inherited member types on purpose, only immediate members
      type = type.getMemberType(name);
      if (type == null)
        return new ProblemReferenceBinding(
            CharOperation.subarray(compoundName, 0, i), null, ProblemReasons.NotFound);
    }
    if (!type.canBeSeenBy(this.fPackage))
      return new ProblemReferenceBinding(compoundName, type, ProblemReasons.NotVisible);
    return type;
  }
コード例 #2
0
  private Binding findSingleStaticImport(char[][] compoundName, int mask) {
    Binding binding = findImport(compoundName, compoundName.length - 1);
    if (!binding.isValidBinding()) return binding;

    char[] name = compoundName[compoundName.length - 1];
    if (binding instanceof PackageBinding) {
      Binding temp = ((PackageBinding) binding).getTypeOrPackage(name);
      if (temp != null
          && temp
              instanceof
              ReferenceBinding) // must resolve to a member type or field, not a top level type
      return new ProblemReferenceBinding(
            compoundName, (ReferenceBinding) temp, ProblemReasons.InvalidTypeForStaticImport);
      return binding; // cannot be a package, error is caught in sender
    }

    // look to see if its a static field first
    ReferenceBinding type = (ReferenceBinding) binding;
    FieldBinding field = (mask & Binding.FIELD) != 0 ? findField(type, name, null, true) : null;
    if (field != null) {
      if (field.problemId() == ProblemReasons.Ambiguous
          && ((ProblemFieldBinding) field).closestMatch.isStatic())
        return field; // keep the ambiguous field instead of a possible method match
      if (field.isValidBinding() && field.isStatic() && field.canBeSeenBy(type, null, this))
        return field;
    }

    // look to see if there is a static method with the same selector
    MethodBinding method = (mask & Binding.METHOD) != 0 ? findStaticMethod(type, name) : null;
    if (method != null) return method;

    type = findMemberType(name, type);
    if (type == null || !type.isStatic()) {
      if (field != null && !field.isValidBinding() && field.problemId() != ProblemReasons.NotFound)
        return field;
      return new ProblemReferenceBinding(compoundName, type, ProblemReasons.NotFound);
    }
    if (type.isValidBinding() && !type.canBeSeenBy(this.fPackage))
      return new ProblemReferenceBinding(compoundName, type, ProblemReasons.NotVisible);
    if (type.problemId() == ProblemReasons.NotVisible) // ensure compoundName is correct
    return new ProblemReferenceBinding(
          compoundName, ((ProblemReferenceBinding) type).closestMatch, ProblemReasons.NotVisible);
    return type;
  }
コード例 #3
0
  /**
   * Checks for duplicates. If all ok, records the importBinding returns -1 when this import is
   * flagged as duplicate.
   *
   * @param importBinding
   * @param typesBySimpleNames
   * @param importReference
   * @param compoundName
   * @return -1 when this import is flagged as duplicate, importPtr otherwise.
   */
  private int checkAndRecordImportBinding(
      Binding importBinding,
      HashtableOfType typesBySimpleNames,
      ImportReference importReference,
      char[][] compoundName) {
    ReferenceBinding conflictingType = null;
    if (importBinding instanceof MethodBinding) {
      conflictingType = (ReferenceBinding) getType(compoundName, compoundName.length);
      if (!conflictingType.isValidBinding()
          || (importReference.isStatic() && !conflictingType.isStatic())) conflictingType = null;
    }
    // collisions between an imported static field & a type should be checked according to spec...
    // but currently not by javac
    final char[] name = compoundName[compoundName.length - 1];
    if (importBinding instanceof ReferenceBinding || conflictingType != null) {
      ReferenceBinding referenceBinding =
          conflictingType == null ? (ReferenceBinding) importBinding : conflictingType;
      ReferenceBinding typeToCheck =
          referenceBinding.problemId() == ProblemReasons.Ambiguous
              ? ((ProblemReferenceBinding) referenceBinding).closestMatch
              : referenceBinding;
      if (importReference.isTypeUseDeprecated(typeToCheck, this))
        problemReporter().deprecatedType(typeToCheck, importReference);

      ReferenceBinding existingType = typesBySimpleNames.get(name);
      if (existingType != null) {
        // duplicate test above should have caught this case, but make sure
        if (TypeBinding.equalsEquals(existingType, referenceBinding)) {
          // https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
          // Check all resolved imports to see if this import qualifies as a duplicate
          for (int j = 0; j < this.importPtr; j++) {
            ImportBinding resolved = this.tempImports[j];
            if (resolved instanceof ImportConflictBinding) {
              ImportConflictBinding importConflictBinding = (ImportConflictBinding) resolved;
              if (TypeBinding.equalsEquals(
                  importConflictBinding.conflictingTypeBinding, referenceBinding)) {
                if (!importReference.isStatic()) {
                  // resolved is implicitly static
                  problemReporter().duplicateImport(importReference);
                  recordImportBinding(
                      new ImportBinding(compoundName, false, importBinding, importReference));
                }
              }
            } else if (resolved.resolvedImport == referenceBinding) {
              if (importReference.isStatic() != resolved.isStatic()) {
                problemReporter().duplicateImport(importReference);
                recordImportBinding(
                    new ImportBinding(compoundName, false, importBinding, importReference));
              }
            }
          }
          return -1;
        }
        // either the type collides with a top level type or another imported type
        for (int j = 0, length = this.topLevelTypes.length; j < length; j++) {
          if (CharOperation.equals(this.topLevelTypes[j].sourceName, existingType.sourceName)) {
            problemReporter().conflictingImport(importReference);
            return -1;
          }
        }
        if (importReference.isStatic()
            && importBinding instanceof ReferenceBinding
            && compilerOptions().sourceLevel >= ClassFileConstants.JDK1_8) {
          // 7.5.3 says nothing about collision of single static imports and JDK8 tolerates them,
          // though use is flagged.
          for (int j = 0; j < this.importPtr; j++) {
            ImportBinding resolved = this.tempImports[j];
            if (resolved.isStatic()
                && resolved.resolvedImport instanceof ReferenceBinding
                && importBinding != resolved.resolvedImport) {
              if (CharOperation.equals(
                  name, resolved.compoundName[resolved.compoundName.length - 1])) {
                ReferenceBinding type = (ReferenceBinding) resolved.resolvedImport;
                resolved.resolvedImport =
                    new ProblemReferenceBinding(
                        new char[][] {name}, type, ProblemReasons.Ambiguous);
                return -1;
              }
            }
          }
        }
        problemReporter().duplicateImport(importReference);
        return -1;
      }
      typesBySimpleNames.put(name, referenceBinding);
    } else if (importBinding instanceof FieldBinding) {
      for (int j = 0; j < this.importPtr; j++) {
        ImportBinding resolved = this.tempImports[j];
        // find other static fields with the same name
        if (resolved.isStatic()
            && resolved.resolvedImport instanceof FieldBinding
            && importBinding != resolved.resolvedImport) {
          if (CharOperation.equals(name, resolved.compoundName[resolved.compoundName.length - 1])) {
            if (compilerOptions().sourceLevel >= ClassFileConstants.JDK1_8) {
              // 7.5.3 says nothing about collision of single static imports and JDK8 tolerates
              // them, though use is flagged.
              FieldBinding field = (FieldBinding) resolved.resolvedImport;
              resolved.resolvedImport =
                  new ProblemFieldBinding(
                      field, field.declaringClass, name, ProblemReasons.Ambiguous);
              return -1;
            } else {
              problemReporter().duplicateImport(importReference);
              return -1;
            }
          }
        }
      }
    }
    if (conflictingType == null) {
      recordImportBinding(new ImportBinding(compoundName, false, importBinding, importReference));
    } else {
      recordImportBinding(
          new ImportConflictBinding(compoundName, importBinding, conflictingType, importReference));
    }
    return this.importPtr;
  }
コード例 #4
0
  void buildTypeBindings(AccessRestriction accessRestriction) {
    this.topLevelTypes =
        new SourceTypeBinding[0]; // want it initialized if the package cannot be resolved
    boolean firstIsSynthetic = false;
    if (this.referenceContext.compilationResult.compilationUnit != null) {
      char[][] expectedPackageName =
          this.referenceContext.compilationResult.compilationUnit.getPackageName();
      if (expectedPackageName != null
          && !CharOperation.equals(this.currentPackageName, expectedPackageName)) {

        // only report if the unit isn't structurally empty
        if (this.referenceContext.currentPackage != null
            || this.referenceContext.types != null
            || this.referenceContext.imports != null) {
          problemReporter().packageIsNotExpectedPackage(this.referenceContext);
        }
        this.currentPackageName =
            expectedPackageName.length == 0 ? CharOperation.NO_CHAR_CHAR : expectedPackageName;
      }
    }
    if (this.currentPackageName == CharOperation.NO_CHAR_CHAR) {
      // environment default package is never null
      this.fPackage = this.environment.defaultPackage;
    } else {
      if ((this.fPackage = this.environment.createPackage(this.currentPackageName)) == null) {
        if (this.referenceContext.currentPackage != null) {
          problemReporter()
              .packageCollidesWithType(
                  this.referenceContext); // only report when the unit has a package statement
        }
        // ensure fPackage is not null
        this.fPackage = this.environment.defaultPackage;
        return;
      } else if (this.referenceContext.isPackageInfo()) {
        // resolve package annotations now if this is "package-info.java".
        if (this.referenceContext.types == null || this.referenceContext.types.length == 0) {
          this.referenceContext.types = new TypeDeclaration[1];
          this.referenceContext.createPackageInfoType();
          firstIsSynthetic = true;
        }
        // ensure the package annotations are copied over before resolution
        if (this.referenceContext.currentPackage != null
            && this.referenceContext.currentPackage.annotations != null) {
          this.referenceContext.types[0].annotations =
              this.referenceContext.currentPackage.annotations;
        }
      }
      recordQualifiedReference(this.currentPackageName); // always dependent on your own package
    }

    // Skip typeDeclarations which know of previously reported errors
    TypeDeclaration[] types = this.referenceContext.types;
    int typeLength = (types == null) ? 0 : types.length;
    this.topLevelTypes = new SourceTypeBinding[typeLength];
    int count = 0;
    nextType:
    for (int i = 0; i < typeLength; i++) {
      TypeDeclaration typeDecl = types[i];
      if (this.environment.isProcessingAnnotations && this.environment.isMissingType(typeDecl.name))
        throw new SourceTypeCollisionException(); // resolved a type ref before APT generated the
      // type
      ReferenceBinding typeBinding = this.fPackage.getType0(typeDecl.name);
      recordSimpleReference(typeDecl.name); // needed to detect collision cases
      if (typeBinding != null
          && typeBinding.isValidBinding()
          && !(typeBinding instanceof UnresolvedReferenceBinding)) {
        // if its an unresolved binding - its fixed up whenever its needed, see
        // UnresolvedReferenceBinding.resolve()
        if (this.environment.isProcessingAnnotations)
          throw new SourceTypeCollisionException(); // resolved a type ref before APT generated the
        // type
        // if a type exists, check that its a valid type
        // it can be a NotFound problem type if its a secondary type referenced before its primary
        // type found in additional units
        // and it can be an unresolved type which is now being defined
        problemReporter().duplicateTypes(this.referenceContext, typeDecl);
        continue nextType;
      }
      if (this.fPackage != this.environment.defaultPackage
          && this.fPackage.getPackage(typeDecl.name) != null) {
        // if a package exists, it must be a valid package - cannot be a NotFound problem package
        // this is now a warning since a package does not really 'exist' until it contains a type,
        // see JLS v2, 7.4.3
        problemReporter().typeCollidesWithPackage(this.referenceContext, typeDecl);
      }

      if ((typeDecl.modifiers & ClassFileConstants.AccPublic) != 0) {
        char[] mainTypeName;
        if ((mainTypeName = this.referenceContext.getMainTypeName())
                != null // mainTypeName == null means that implementor of ICompilationUnit decided
            // to return null
            && !CharOperation.equals(mainTypeName, typeDecl.name)) {
          problemReporter().publicClassMustMatchFileName(this.referenceContext, typeDecl);
          // tolerate faulty main type name (91091), allow to proceed into type construction
        }
      }

      ClassScope child = new ClassScope(this, typeDecl);
      SourceTypeBinding type = child.buildType(null, this.fPackage, accessRestriction);
      if (firstIsSynthetic && i == 0) type.modifiers |= ClassFileConstants.AccSynthetic;
      if (type != null) this.topLevelTypes[count++] = type;
    }

    // shrink topLevelTypes... only happens if an error was reported
    if (count != this.topLevelTypes.length)
      System.arraycopy(
          this.topLevelTypes, 0, this.topLevelTypes = new SourceTypeBinding[count], 0, count);
  }