Class ConstructorTemplate

java.lang.Object
de.japkit.test.members.constructor.ConstructorTemplate

@Clazz(commentExpr="The generated class for the example.")
@RuntimeMetadata
public class ConstructorTemplate
extends java.lang.Object
This template shows how to generate constructors.
  • For more details on how to generate constructors conditionally, see ConditionTemplate.
  • For more details on how to set the src and how to generate multiple constructors from one constructor template, see SourceTemplate.
  • For more details on how to set the modifiers of the constructor, see ModifiersTemplate.
  • For more details on how to set the annotations of the constructor, see AnnotationsTemplate.
  • For more examples on how to generate the parameters and the body code of the constructor, see also MethodTemplate.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    (package private) class  ConstructorTemplate.fieldAssignment
    The CodeFragment to create a field assignment within the constructor.
    (package private) class  ConstructorTemplate.fieldAssignmentRhs
    The right hand side of the field assignment.
    (package private) class  ConstructorTemplate.fields
    A function to get the fields of a TypeElement.
    (package private) class  ConstructorTemplate.hasNotNull
    A boolean function that checks if the src element has a NotNull annotation.
    (package private) class  ConstructorTemplate.isString
    A boolean function that checks if the src element has String as type.
    (package private) class  ConstructorTemplate.nullCheck
    A code fragment that adds a null check to the field assignment if the field has a NotNull annotation.
    (package private) class  ConstructorTemplate.trimToNull
    A code fragment that trims Strings by StringUtils.trimToNull(String).
  • Field Summary

    Fields 
    Modifier and Type Field Description
    (package private) de.japkit.functions.SrcType $name$
    Copies the fields from ConstructorExample.
  • Constructor Summary

    Constructors 
    Constructor Description
    ConstructorTemplate()
    A parameterless constructor.
    ConstructorTemplate​(int dummy, de.japkit.functions.SrcType $name$)
    ConstructorTemplate​(de.japkit.functions.SrcType $name$)
    A constructor that has the fields of the annotated class (MembersExample) as parameters and assigns them to the according generated fields.
    ConstructorTemplate​(java.lang.String field2)
    For constructors with at least one parameter, the Constructor annotation is not necessary.
  • Method Summary

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • ConstructorTemplate

      public ConstructorTemplate()
      A parameterless constructor. When it is generated in the target class, the name will we be adjusted accordingly.

      The Constructor annotation is required for disambiguation when generating a default constructor.

    • ConstructorTemplate

      public ConstructorTemplate​(java.lang.String field2)
      For constructors with at least one parameter, the Constructor annotation is not necessary.
      @japkit.bodyCode
      this.field2 = field2;
    • ConstructorTemplate

      public ConstructorTemplate​(de.japkit.functions.SrcType $name$)
      A constructor that has the fields of the annotated class (MembersExample) as parameters and assigns them to the according generated fields.

      The parameter order depends on the result of TypeElement.getEnclosedElements(), which is used in ConstructorTemplate.fields function. Usually, this will be the same order as in the source code of ConstructorExample. However, during incremental build in Eclipse, you may sometimes experience a different order. If this happens, please do a clean build.

      See Also:
      Eclipse Bug 300408, Eclipse Bug 500589
    • ConstructorTemplate

      public ConstructorTemplate​(int dummy, de.japkit.functions.SrcType $name$)
      The same as ConstructorTemplate(SrcType). But this time, a CodeFragment is called for generating the field assignment code. A CodeFragment is basically a function that creates code.

      By using CodeFragments, arbitrary complex constructors and methods can be generated. But as in manual programming, it might be sometimes better to split them into multiple (private) methods then.

      Note that there a important differences between a CodeFragment and a function that returns a string. For example, CodeFragments provide support for adding import statements to the generated class. Furthermore, they are evaluated lazily, when the generated class is finally written. So, do not use simple string functions when generating method or constructor bodies, but always CodeFragments.