Package net.karneim.pojobuilder.model

Examples of net.karneim.pojobuilder.model.PropertyListM


  private void generateSource(TypeM builderType, boolean isAbstract, TypeM selfType,
      TypeM baseType, TypeM interfaceType, boolean hasBuilderProperties, TypeM pojoType,
      PropertyListM properties, BuildMethodM buildMethod, FactoryMethodM factoryMethod,
      CopyMethodM copyMethodM, ValidatorM validator, TypeM optionalType) throws IOException {
    properties = new PropertyListM(properties);
    properties.filterOutNonWritableProperties(builderType);

    ImportTypesM importTypes = pojoType.addToImportTypes(new ImportTypesM());
    if (factoryMethod != null) {
      factoryMethod.getDeclaringClass().addToImportTypes(importTypes);
View Full Code Here


        initialization);
  }

  private void emitCopyMethod(TypeM selfType, TypeM pojoType, PropertyListM properties,
      CopyMethodM copyMethodM) throws IOException {
    properties = new PropertyListM(properties);
    String selfTypeDeclaration = writer.compressType(selfType.getGenericTypeDeclaration());
    String pojoTypeDeclaration = writer.compressType(pojoType.getGenericTypeDeclaration());
    // @formatter:off
    writer
      .emitEmptyLine()   
      .emitJavadoc(
         "Copies the values from the given pojo into this builder.\n\n"
        +"@param pojo\n"
        +"@return this builder")
      .beginMethod(selfTypeDeclaration, copyMethodM.getName(), EnumSet.of(PUBLIC), pojoTypeDeclaration, "pojo");
   
    PropertyListM getterProperties = properties.filterOutPropertiesReadableViaGetterCall(selfType);   
    for( PropertyM prop : getterProperties) {
      String withMethodName = prop.getWithMethodName();
      writer
      .emitStatement("%s(pojo.%s())", withMethodName, prop.getGetterMethod().getName());
    }
   
    PropertyListM readableFieldProperties = properties.filterOutPropertiesReadableViaFieldAccess(selfType);   
    for( PropertyM prop : readableFieldProperties) {
      String withMethodName = prop.getWithMethodName();
      writer
      .emitStatement("%s(pojo.%s)", withMethodName, prop.getPropertyName());
    }
View Full Code Here

  }

  private void emitBuildMethod(TypeM builderType, TypeM pojoType, TypeM interfaceType,
      boolean hasBuilderProperties, PropertyListM properties, FactoryMethodM factoryMethod,
      BuildMethodM buildMethod, ValidatorM validator) throws IOException {
    properties = new PropertyListM(properties);
    String pojoTypeDeclaration = writer.compressType(pojoType.getGenericTypeDeclaration());
    String pojoClassname = writer.compressType(pojoType.getName());

    // @formatter:off
    writer
      .emitEmptyLine()   
      .emitJavadoc(
         "Creates a new {@link %s} based on this builder's settings.\n\n"
        +"@return the created %s"
        , pojoClassname, pojoClassname);
    if ( buildMethod.isOverrides()) {
      writer
        .emitAnnotation(Override.class);
    }
    writer
      .beginMethod(pojoTypeDeclaration, "build", EnumSet.of(PUBLIC))
        .beginControlFlow("try");
   
    if ( !hasBuilderProperties) {
      if ( factoryMethod == null) {
        String arguments = properties.filterOutPropertiesWritableViaConstructorParameter(builderType).toArgumentString();
        writer
            .emitStatement("%s result = new %s(%s)", pojoTypeDeclaration, pojoTypeDeclaration, arguments);
      } else {
        String arguments = properties.filterOutPropertiesWritableViaFactoryMethodParameter(builderType).toArgumentString();
        String factoryClass = writer.compressType(factoryMethod.getDeclaringClass().getName());
        writer
            .emitStatement("%s result = %s.%s(%s)", pojoTypeDeclaration, factoryClass, factoryMethod.getName(), arguments);
      }
    } else {
      if ( factoryMethod == null) {
        ArgumentListM constructorArguments = properties.filterOutPropertiesWritableViaConstructorParameter(builderType);
        StringBuilder arguments = new StringBuilder();
        for (PropertyM prop : constructorArguments.sortByPosition().getPropertyList()) {
          writer
            .emitStatement("%s _%s = !%s && %s!=null?%s.build():%s",
                writer.compressType(prop.getPropertyType().getGenericTypeDeclaration()),
                prop.getConstructorParameter().getName(),
                prop.getIsSetFieldName(),
                prop.getBuilderFieldName(),
                prop.getBuilderFieldName(),
                prop.getValueFieldName()
              );
          if ( arguments.length()>0) {
            arguments.append(", ");
          }
          arguments.append(String.format("_%s", prop.getConstructorParameter().getName()));
        }
        writer
            .emitStatement("%s result = new %s(%s)", pojoTypeDeclaration, pojoTypeDeclaration, arguments.toString());
      } else {
        ArgumentListM constructorArguments = properties.filterOutPropertiesWritableViaFactoryMethodParameter(builderType);
        StringBuilder arguments = new StringBuilder();
        for (PropertyM prop : constructorArguments.sortByPosition().getPropertyList()) {
          writer
            .emitStatement("%s _%s = !%s && %s!=null?%s.build():%s",
                writer.compressType(prop.getPropertyType().getGenericType()),
                prop.getFactoryMethodParameter().getName(),
                prop.getIsSetFieldName(),
                prop.getBuilderFieldName(),
                prop.getBuilderFieldName(),
                prop.getValueFieldName()
              );
          if ( arguments.length()>0) {
            arguments.append(", ");
          }
          arguments.append(String.format("_%s", prop.getFactoryMethodParameter().getName()));
        }
        String factoryClass = writer.compressType(factoryMethod.getDeclaringClass().getName());
        writer
            .emitStatement("%s result = %s.%s(%s)", pojoTypeDeclaration, factoryClass, factoryMethod.getName(), arguments.toString());
      }
    }

    PropertyListM setterProperties = properties.filterOutPropertiesWritableBy(Type.SETTER, builderType);
    for( PropertyM prop : setterProperties) {
      writer
          .beginControlFlow("if (%s)", prop.getIsSetFieldName())
            .emitStatement("result.%s(%s)", prop.getSetterMethod().getName(), prop.getValueFieldName());
      if ( hasBuilderProperties) {
        writer
          .nextControlFlow("else if (%s!=null)", prop.getBuilderFieldName())
            .emitStatement("result.%s(%s.build())", prop.getSetterMethod().getName(), prop.getBuilderFieldName());
      }
      writer
          .endControlFlow();
    }

    PropertyListM writableProperties = properties.filterOutPropertiesWritableBy(Type.FIELD, builderType);
    for( PropertyM prop : writableProperties) {
      writer
          .beginControlFlow("if (%s)", prop.getIsSetFieldName())
            .emitStatement("result.%s = %s", prop.getPropertyName(), prop.getValueFieldName());
      if ( hasBuilderProperties) {
View Full Code Here

    // Given:  @formatter:off
    TypeM pojoType = new TypeM("com.example.output", "Sample");
   
    BuilderM builder = new BuilderM();
    builder.setPojoType(pojoType);
    builder.setProperties( new PropertyListM(
      new PropertyM("someBoolean", PrimitiveTypeM.BOOLEAN)
        .accessibleVia(new FieldAccessM(EnumSet.of(PUBLIC)).declaredIn(pojoType))
        .withMethodNamePattern("with*"),
      new PropertyM("someChar", PrimitiveTypeM.CHAR)
        .accessibleVia(new FieldAccessM(EnumSet.of(PRIVATE)).declaredIn(pojoType))
View Full Code Here

    // Given:  @formatter:off
    TypeM pojoType = new TypeM("com.example.output", "Sample");
   
    BuilderM builder = new BuilderM();
    builder.setPojoType(pojoType);
    builder.setProperties( new PropertyListM(
      new PropertyM("someBoolean", PrimitiveTypeM.BOOLEAN)
        .accessibleVia(new FieldAccessM(EnumSet.of(PUBLIC)))
        .withMethodNamePattern("with*"),
      new PropertyM("someChar", PrimitiveTypeM.CHAR)
        .accessibleVia(new FieldAccessM(EnumSet.of(PUBLIC)))
View Full Code Here

    // Given:  @formatter:off
    TypeM pojoType = new TypeM("com.example.output", "Sample");
   
    BuilderM builder = new BuilderM();
    builder.setPojoType(pojoType);
    builder.setProperties( new PropertyListM(
      new PropertyM("someString", new TypeM("java.lang","String"))
        .accessibleVia(new FieldAccessM(EnumSet.of(PUBLIC)))
        .withMethodNamePattern("with*"),
      new PropertyM("someFile", new TypeM("java.io","File"))
        .accessibleVia(new FieldAccessM(EnumSet.of(PUBLIC)))
View Full Code Here

    // Given:  @formatter:off
    TypeM pojoType = new TypeM("com.example.output", "Sample");
   
    BuilderM builder = new BuilderM();
    builder.setPojoType(pojoType);
    builder.setProperties( new PropertyListM(
      new PropertyM("someStringList", new TypeM("java.util","List")
        .withTypeParameter(new TypeM("java.lang","String")))
        .accessibleVia(new FieldAccessM(EnumSet.of(PUBLIC)))
        .withMethodNamePattern("with*"),
      new PropertyM("someMap", new TypeM("java.util","Map")
View Full Code Here

    TypeVariableM V = new TypeVariableM("V").whichExtends(new TypeM("java.lang","Number"));
    TypeM pojoType = new TypeM("com.example.output", "Sample").withTypeParameter( K, V);
   
    BuilderM builder = new BuilderM();
    builder.setPojoType(pojoType);
    builder.setProperties( new PropertyListM(
      new PropertyM("someMap", new TypeM("java.util","Map")
        .withTypeParameter(K, V))
        .accessibleVia(new FieldAccessM(EnumSet.of(PUBLIC)))
        .withMethodNamePattern("with*")
    ));   
View Full Code Here

TOP

Related Classes of net.karneim.pojobuilder.model.PropertyListM

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.