Example #1
0
  public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalanceValue) {

    /* do not consider a type starting passed the type end (if set)
    it must be belonging to an enclosing type */
    if (methodDeclaration.declarationSourceEnd != 0
        && typeDeclaration.declarationSourceStart > methodDeclaration.declarationSourceEnd) {

      if (this.parent == null) {
        return this; // ignore
      }
      return this.parent.add(typeDeclaration, bracketBalanceValue);
    }
    if ((typeDeclaration.bits & ASTNode.IsLocalType) != 0
        || this.parser().methodRecoveryActivated
        || this.parser().statementRecoveryActivated) {
      if (methodBody == null) {
        Block block = new Block(0);
        block.sourceStart = methodDeclaration.bodyStart;
        this.add(block, 1);
      }
      methodBody.attachPendingModifiers(
          this.pendingAnnotations,
          this.pendingAnnotationCount,
          this.pendingModifiers,
          this.pendingModifersSourceStart);
      this.resetPendingModifiers();
      return methodBody.add(typeDeclaration, bracketBalanceValue, true);
    }
    switch (TypeDeclaration.kind(typeDeclaration.modifiers)) {
      case TypeDeclaration.INTERFACE_DECL:
      case TypeDeclaration.ANNOTATION_TYPE_DECL:
        resetPendingModifiers();
        this.updateSourceEndIfNecessary(
            this.previousAvailableLineEnd(typeDeclaration.declarationSourceStart - 1));
        if (this.parent == null) {
          return this; // ignore
        }
        // close the constructor
        return this.parent.add(typeDeclaration, bracketBalanceValue);
    }
    if (localTypes == null) {
      localTypes = new RecoveredType[5];
      localTypeCount = 0;
    } else {
      if (localTypeCount == localTypes.length) {
        System.arraycopy(
            localTypes, 0, (localTypes = new RecoveredType[2 * localTypeCount]), 0, localTypeCount);
      }
    }
    RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalanceValue);
    localTypes[localTypeCount++] = element;

    if (this.pendingAnnotationCount > 0) {
      element.attach(
          pendingAnnotations, pendingAnnotationCount, pendingModifiers, pendingModifersSourceStart);
    }
    this.resetPendingModifiers();

    /* consider that if the opening brace was not found, it is there */
    if (!foundOpeningBrace) {
      foundOpeningBrace = true;
      this.bracketBalance++;
    }
    return element;
  }
  public FieldDeclaration updatedFieldDeclaration(int depth, Set knownTypes) {
    /* update annotations */
    if (this.modifiers != 0) {
      this.fieldDeclaration.modifiers |= this.modifiers;
      if (this.modifiersStart < this.fieldDeclaration.declarationSourceStart) {
        this.fieldDeclaration.declarationSourceStart = this.modifiersStart;
      }
    }
    /* update annotations */
    if (this.annotationCount > 0) {
      int existingCount =
          this.fieldDeclaration.annotations == null ? 0 : this.fieldDeclaration.annotations.length;
      Annotation[] annotationReferences = new Annotation[existingCount + this.annotationCount];
      if (existingCount > 0) {
        System.arraycopy(
            this.fieldDeclaration.annotations,
            0,
            annotationReferences,
            this.annotationCount,
            existingCount);
      }
      for (int i = 0; i < this.annotationCount; i++) {
        annotationReferences[i] = this.annotations[i].updatedAnnotationReference();
      }
      this.fieldDeclaration.annotations = annotationReferences;

      int start = this.annotations[0].annotation.sourceStart;
      if (start < this.fieldDeclaration.declarationSourceStart) {
        this.fieldDeclaration.declarationSourceStart = start;
      }
    }

    if (this.anonymousTypes != null) {
      if (this.fieldDeclaration.initialization == null) {
        ArrayInitializer recoveredInitializers = null;
        int recoveredInitializersCount = 0;
        if (this.anonymousTypeCount > 1) {
          recoveredInitializers = new ArrayInitializer();
          recoveredInitializers.expressions = new Expression[this.anonymousTypeCount];
        }
        for (int i = 0; i < this.anonymousTypeCount; i++) {
          RecoveredType recoveredType = this.anonymousTypes[i];
          TypeDeclaration typeDeclaration = recoveredType.typeDeclaration;
          if (typeDeclaration.declarationSourceEnd == 0) {
            typeDeclaration.declarationSourceEnd = this.fieldDeclaration.declarationSourceEnd;
            typeDeclaration.bodyEnd = this.fieldDeclaration.declarationSourceEnd;
          }
          if (recoveredType.preserveContent) {
            TypeDeclaration anonymousType =
                recoveredType.updatedTypeDeclaration(depth + 1, knownTypes);
            if (anonymousType != null) {
              if (this.anonymousTypeCount > 1) {
                if (recoveredInitializersCount == 0) {
                  this.fieldDeclaration.initialization = recoveredInitializers;
                }
                recoveredInitializers.expressions[recoveredInitializersCount++] =
                    anonymousType.allocation;
              } else {
                this.fieldDeclaration.initialization = anonymousType.allocation;
              }
              int end = anonymousType.declarationSourceEnd;
              if (end
                  > this.fieldDeclaration
                      .declarationSourceEnd) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=307337
                this.fieldDeclaration.declarationSourceEnd = end;
                this.fieldDeclaration.declarationEnd = end;
              }
            }
          }
        }
        if (this.anonymousTypeCount > 0) this.fieldDeclaration.bits |= ASTNode.HasLocalType;
      } else if (this.fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
        // fieldDeclaration is an enum constant
        for (int i = 0; i < this.anonymousTypeCount; i++) {
          RecoveredType recoveredType = this.anonymousTypes[i];
          TypeDeclaration typeDeclaration = recoveredType.typeDeclaration;
          if (typeDeclaration.declarationSourceEnd == 0) {
            typeDeclaration.declarationSourceEnd = this.fieldDeclaration.declarationSourceEnd;
            typeDeclaration.bodyEnd = this.fieldDeclaration.declarationSourceEnd;
          }
          // if the enum is recovered then enum constants must be recovered too.
          // depth is considered as the same as the depth of the enum
          recoveredType.updatedTypeDeclaration(depth, knownTypes);
        }
      }
    }
    return this.fieldDeclaration;
  }