示例#1
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;
  }