Example #1
0
  public AbstractMethodDeclaration updatedMethodDeclaration() {
    /* update annotations */
    if (modifiers != 0) {
      this.methodDeclaration.modifiers |= modifiers;
      if (this.modifiersStart < this.methodDeclaration.declarationSourceStart) {
        this.methodDeclaration.declarationSourceStart = modifiersStart;
      }
    }
    /* update annotations */
    if (annotationCount > 0) {
      int existingCount =
          methodDeclaration.annotations == null ? 0 : methodDeclaration.annotations.length;
      Annotation[] annotationReferences = new Annotation[existingCount + annotationCount];
      if (existingCount > 0) {
        System.arraycopy(
            methodDeclaration.annotations, 0, annotationReferences, annotationCount, existingCount);
      }
      for (int i = 0; i < annotationCount; i++) {
        annotationReferences[i] = annotations[i].updatedAnnotationReference();
      }
      methodDeclaration.annotations = annotationReferences;

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

    if (methodBody != null) {
      Block block = methodBody.updatedBlock();
      if (block != null) {
        methodDeclaration.statements = block.statements;

        if (methodDeclaration.declarationSourceEnd == 0) {
          methodDeclaration.declarationSourceEnd = block.sourceEnd;
          methodDeclaration.bodyEnd = block.sourceEnd;
        }

        /* first statement might be an explict constructor call destinated to a special slot */
        if (methodDeclaration.isConstructor()) {
          ConstructorDeclaration constructor = (ConstructorDeclaration) methodDeclaration;
          if (methodDeclaration.statements != null
              && methodDeclaration.statements[0] instanceof ExplicitConstructorCall) {
            constructor.constructorCall = (ExplicitConstructorCall) methodDeclaration.statements[0];
            int length = methodDeclaration.statements.length;
            System.arraycopy(
                methodDeclaration.statements,
                1,
                (methodDeclaration.statements = new Statement[length - 1]),
                0,
                length - 1);
          }
          if (constructor.constructorCall == null) { // add implicit constructor call
            constructor.constructorCall = SuperReference.implicitSuperConstructorCall();
          }
        }
      }
    } else {
      if (methodDeclaration.declarationSourceEnd == 0) {
        if (methodDeclaration.sourceEnd + 1 == methodDeclaration.bodyStart) {
          // right brace is missing
          methodDeclaration.declarationSourceEnd = methodDeclaration.sourceEnd;
          methodDeclaration.bodyStart = methodDeclaration.sourceEnd;
          methodDeclaration.bodyEnd = methodDeclaration.sourceEnd;
        } else {
          methodDeclaration.declarationSourceEnd = methodDeclaration.bodyStart;
          methodDeclaration.bodyEnd = methodDeclaration.bodyStart;
        }
      }
    }
    if (localTypeCount > 0) methodDeclaration.bits |= ASTNode.HasLocalType;
    return methodDeclaration;
  }
Example #2
0
  private ConstructorDeclaration createConstructor(
      boolean isPublic, EclipseNode type, Collection<EclipseNode> fields, ASTNode source) {
    long p = (long) source.sourceStart << 32 | source.sourceEnd;

    ConstructorDeclaration constructor =
        new ConstructorDeclaration(
            ((CompilationUnitDeclaration) type.top().get()).compilationResult);
    Eclipse.setGeneratedBy(constructor, source);

    constructor.modifiers = PKG.toModifier(isPublic ? AccessLevel.PUBLIC : AccessLevel.PRIVATE);
    constructor.annotations = null;
    constructor.selector = ((TypeDeclaration) type.get()).name;
    constructor.constructorCall =
        new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
    Eclipse.setGeneratedBy(constructor.constructorCall, source);
    constructor.thrownExceptions = null;
    constructor.typeParameters = null;
    constructor.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
    constructor.bodyStart =
        constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
    constructor.bodyEnd =
        constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
    constructor.arguments = null;

    List<Argument> args = new ArrayList<Argument>();
    List<Statement> assigns = new ArrayList<Statement>();
    List<Statement> nullChecks = new ArrayList<Statement>();

    for (EclipseNode fieldNode : fields) {
      FieldDeclaration field = (FieldDeclaration) fieldNode.get();
      FieldReference thisX =
          new FieldReference(("this." + new String(field.name)).toCharArray(), p);
      Eclipse.setGeneratedBy(thisX, source);
      thisX.receiver = new ThisReference((int) (p >> 32), (int) p);
      Eclipse.setGeneratedBy(thisX.receiver, source);
      thisX.token = field.name;

      SingleNameReference assignmentNameRef = new SingleNameReference(field.name, p);
      Eclipse.setGeneratedBy(assignmentNameRef, source);
      Assignment assignment = new Assignment(thisX, assignmentNameRef, (int) p);
      Eclipse.setGeneratedBy(assignment, source);
      assigns.add(assignment);
      long fieldPos = (((long) field.sourceStart) << 32) | field.sourceEnd;
      Argument argument =
          new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL);
      Eclipse.setGeneratedBy(argument, source);
      Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
      Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
      if (nonNulls.length != 0) {
        Statement nullCheck = generateNullCheck(field, source);
        if (nullCheck != null) nullChecks.add(nullCheck);
      }
      Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables, source);
      if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations;
      args.add(argument);
    }

    nullChecks.addAll(assigns);
    constructor.statements =
        nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]);
    constructor.arguments = args.isEmpty() ? null : args.toArray(new Argument[args.size()]);
    return constructor;
  }