コード例 #1
0
  private void generateBuildMethod(JType builder, BuilderModel model) {
    if (!model.isSupportSubclassing()) {
      SourceTemplate buildMethod =
          newSourceTemplate()
              .var("b.type", model.getPojoType().getSimpleName())
              .var("buildName", model.getBuildPojoMethodName())
              .pl("public ${b.type} ${buildName}(){")
              .p("	return new ${b.type}(");

      boolean comma = false;
      for (BuilderPropertyModel property : model.getProperties()) {
        if (property.isReadOnly()) {
          continue;
        }
        if (comma) {
          buildMethod.p(",");
        }
        buildMethod.p("this." + property.getPropertyName());
        comma = true;
      }
      buildMethod.pl(");");
      buildMethod.pl("}");
      addMethod(builder, buildMethod.asMethodNodeSnippet(), model.isMarkGenerated());
    }
  }
コード例 #2
0
  private void generateAllArgCtor(JType beanType, BuilderModel model) {
    if (!beanType.isAbstract()) {
      SourceTemplate beanCtor =
          newSourceTemplate()
              .var("b.name", model.getPojoType().getSimpleNameRaw())
              .pl("private ${b.name} (");

      boolean comma = false;
      // args
      for (BuilderPropertyModel property : model.getProperties()) {
        if (property.isReadOnly()) {
          continue;
        }
        if (comma) {
          beanCtor.p(",");
        }
        if (model.isMarkCtorArgsAsProperties()) {
          beanCtor.p(
              "@" + Property.class.getName() + "(name=\"" + property.getPropertyName() + "\") ");
        }
        beanCtor.p(property.getType().getFullName() + " " + property.getPropertyName());
        comma = true;
      }

      beanCtor.pl("){");
      // field assignments
      for (BuilderPropertyModel property : model.getProperties()) {
        if (property.isReadOnly()) {
          continue;
        }
        // TODO:figure out if we can set field directly, via setter, or via ctor args
        // for now assume a setter
        if (property.isFromSuperClass()) {
          beanCtor.pl(property.getPropertySetterName() + "(" + property.getPropertyName() + ");");
        } else {
          beanCtor.pl("this." + property.getFieldName() + "=" + property.getPropertyName() + ";");
        }
        comma = true;
      }
      beanCtor.pl("}");
      addMethod(beanType, beanCtor.asConstructorNodeSnippet(), model.isMarkGenerated());
    }
  }