Class FieldTemplate

java.lang.Object
de.japkit.test.members.field.FieldTemplate

@Clazz(commentExpr="The generated class for the example.")
@RuntimeMetadata
public class FieldTemplate
extends java.lang.Object
This template shows how to generate fields and optionally getters and setters for them.
  • For more details on how to generate fields conditionally, see ConditionTemplate.
  • For more details on how to set the src and how to generate multiple fields from one field template, see SourceTemplate.
  • For more details on how to set the modifiers of the field, see ModifiersTemplate.
  • For more details on how to set the annotations of the field, see AnnotationsTemplate.
  • For more details on how to set the type of the field, see TypeTemplate.
  • For more details on how to set the name of the field, see MemberNameTemplate.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    (package private) class  FieldTemplate.fields
    A function to get the fields of a TypeElement.
    (package private) class  FieldTemplate.hasNotNull
    A function that returns true if the given element has a NotNull annotation.
    static interface  FieldTemplate.isList
    A function that returns true if the given element's type is List.
    (package private) class  FieldTemplate.notNullCheck
    This will generate a statement that checks for null, if the given source element has the NotNull annotation.
    (package private) class  FieldTemplate.setterDefensiveCopy
    Generates a code to create a defensive copy if a mutable type is used.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    private de.japkit.functions.SrcType $name$
    A field for every field in the annotated class FieldExample.
    static java.lang.String[] FIELD_NAMES
    To show some more complex initializer code, a constant field is generated here that has the array of all field names as value.
    private java.lang.String fixedField
    A field with fix name and type and with public getters and setters.
  • Constructor Summary

    Constructors 
    Constructor Description
    FieldTemplate()  
  • Method Summary

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • fixedField

      @Field(getter=@Getter(commentExpr="the value of fixedField"), setter=@Setter(commentExpr="the new value for fixedField"), initCode="\"foobar\"") private java.lang.String fixedField
      A field with fix name and type and with public getters and setters.

      While getters and setters could be generated as methods (see MethodTemplate), they are so common that japkit provides some more convenience for it.

      The field is initialized with the value "foobar".

    • $name$

      @Field(srcFun=fields.class, getter=@Getter(commentExpr="the value of #{name}"), setter=@Setter(commentExpr="the new value for #{name}",chain=true,fluent=true,beforeFragments="notNullCheck",surroundAssignmentExprFragments="setterDefensiveCopy"), genElementIsSrcForDependentRules=false) private de.japkit.functions.SrcType $name$
      A field for every field in the annotated class FieldExample.

      The setters are fluent style and return this for chaining.

      For fields with a NotNull annotation, a null-check is added to the setter by calling the CodeFragment FieldTemplate.notNullCheck. For FieldExample.someString this will result in

       if (someString == null) {
              throw new IllegalArgumentException("someString must not be null.");
       }
       this.someString = someString;
       

      For fields of mutable types, the FieldTemplate.setterDefensiveCopy is called to make a defensive copy in the setter. The code generated by that fragment "surrounds" the right hand side of the assignment in the setter. For example, for the field FieldExample.someStringList this will result in

       this.someStringList = new ArrayList<>(someStringList);
       
       
    • FIELD_NAMES

      @Field(initIterator="#{fields()}") public static final java.lang.String[] FIELD_NAMES
      To show some more complex initializer code, a constant field is generated here that has the array of all field names as value.
      • The initIterator tells to iterate over the fields of the annotated class.
      • The initBeforeIteratorCode is the code to be generated before the iteration starts.
      • The initAfterIteratorCode is the code to be generated after the iteration ends.
      • The initCode is the code to be generated for each iteration. Here, it is the name of the field from the annotated class in double quotes.
        @japkit.initBeforeIteratorCode
        new String[] {
        @japkit.initCode
        "#{name}"
        @japkit.initSeparator
        ,
        @japkit.initAfterIteratorCode
        }
    • Constructor Details