Class ConstructorTemplate
@Clazz(commentExpr="The generated class for the example.")
@RuntimeMetadata
public class ConstructorTemplate
extends java.lang.Object
- 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) classConstructorTemplate.fieldAssignmentThe CodeFragment to create a field assignment within the constructor.(package private) classConstructorTemplate.fieldAssignmentRhsThe right hand side of the field assignment.(package private) classConstructorTemplate.fieldsA function to get the fields of aTypeElement.(package private) classConstructorTemplate.hasNotNullA boolean function that checks if the src element has aNotNullannotation.(package private) classConstructorTemplate.isStringA boolean function that checks if the src element has String as type.(package private) classConstructorTemplate.nullCheckA code fragment that adds a null check to the field assignment if the field has aNotNullannotation.(package private) classConstructorTemplate.trimToNullA code fragment that trims Strings byStringUtils.trimToNull(String). -
Field Summary
Fields Modifier and Type Field Description (package private) de.japkit.functions.SrcType$name$Copies the fields fromConstructorExample. -
Constructor Summary
Constructors Constructor Description ConstructorTemplate()A parameterless constructor.ConstructorTemplate(int dummy, de.japkit.functions.SrcType $name$)The same asConstructorTemplate(SrcType).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, theConstructorannotation is not necessary. -
Method Summary
-
Field Details
-
$name$
Copies the fields fromConstructorExample.
-
-
Constructor Details
-
ConstructorTemplate
public ConstructorTemplate()A parameterless constructor. When it is generated in the target class, the name will we be adjusted accordingly.The
Constructorannotation is required for disambiguation when generating a default constructor. -
ConstructorTemplate
public ConstructorTemplate(java.lang.String field2)For constructors with at least one parameter, theConstructorannotation 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 inConstructorTemplate.fieldsfunction. Usually, this will be the same order as in the source code ofConstructorExample. 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 asConstructorTemplate(SrcType). But this time, aCodeFragmentis 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.
-