Class MethodTemplate

java.lang.Object
de.japkit.test.members.method.MethodTemplate

@Clazz(commentExpr="The generated class for the example.")
@RuntimeMetadata
public class MethodTemplate
extends java.lang.Object
This template shows how to generate methods.

The examples shown here for generating the parameters and the code body also apply to constructors. See ConstructorTemplate.

In case of the code body, there is no syntax or structure enforced for then generated code by japkit. Basically, it is just free text templating. However, there is support for adding import statements to the generated class to avoid use of fully qualified class names in the code templates.

  • For more details on how to generate methods conditionally, see ConditionTemplate.
  • For more details on how to set the src and how to generate multiple methods from one method template, see SourceTemplate.
  • For more details on how to set the modifiers of the method, see ModifiersTemplate.
  • For more details on how to set the annotations of the method or the parameters, see AnnotationsTemplate.
  • For more details on how to set the return type of the method, see TypeTemplate.
  • For more details on how to set the name of the method, see MemberNameTemplate.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    (package private) class  MethodTemplate.fields
    A function to get the fields of a TypeElement.
    (package private) class  MethodTemplate.rethrowAsRuntimeException
    A code fragment that that catches every Exception and rethrows it is RuntimeException.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    private de.japkit.functions.SrcType $name$
    As shown in FieldTemplate, the fields are copied from the annotated class.
  • Constructor Summary

    Constructors 
    Constructor Description
    MethodTemplate()  
  • Method Summary

    Modifier and Type Method Description
    int add​(int number1, int number2)
    Generates a method with fixed parameter list, name and return type.
    boolean equals​(java.lang.Object obj)
    Generates an equals method based on Objects.equals(Object, Object).
    int hashCode()
    Generates a hashCode method based on Objects.hash(Object...).

    Methods inherited from class java.lang.Object

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

    • $name$

      @Field(srcFun=fields.class, getter=@Getter, setter=@Setter) private de.japkit.functions.SrcType $name$
      As shown in FieldTemplate, the fields are copied from the annotated class. They are used later to generate some meaningful methods.
  • Constructor Details

  • Method Details

    • add

      @Method(bodyCode="return number1 + number2;", bodySurroundingFragments="rethrowAsRuntimeException") public int add​(int number1, int number2)
      Generates a method with fixed parameter list, name and return type.

      Since the body code is simple, it can be easily written as bodyCode annotation value.

      Since the method has a non-void return type, the method template returns 0 as dummy value. Alternatively, the method template can be made abstract.

      To demonstrate the usage of surrounding code fragments, the code body is wrapped into a try-catch that catches every Exception and rethrows it is RuntimeException. See MethodTemplate.rethrowAsRuntimeException.

    • hashCode

      @Method(imports=java.util.Objects.class, bodyIteratorFun=fields.class, bodyBeforeIteratorCode="return Objects.hash(", bodyCode="#{name}", bodySeparator=", ", bodyLinebreak=false, bodyAfterIteratorCode=");") public int hashCode()
      Generates a hashCode method based on Objects.hash(Object...).
      • The method template is annotated with Override. This annotation will be copied onto the generated method.
      • To not have to use the qualified name java.util.Objects, it is "imported". Japkit will take care of adding the import statement to the generated class.
      • When generating the code body, it is necessary to iterate over the fields, since the result shall look like return Objects.hash(field1, field2, field3, ...); For this, bodyIterator is used, which calls the MethodTemplate.fields function here to determine the collection to iterate on. bodyCode is just the name of the field. The list of fields is separated by "," (bodySeparator) and surrounded by "return Objects.hash(" (bodyBeforeIteratorCode) and ")" (bodyAfterIteratorCode).
      Overrides:
      hashCode in class java.lang.Object
    • equals

      @Method(imports=java.util.Objects.class, bodyIteratorFun=fields.class, bodySeparator=" &&", bodyIndentAfterLinebreak=true) public boolean equals​(java.lang.Object obj)
      Generates an equals method based on Objects.equals(Object, Object).

      This is quite similar to generating hashCode(): It iterates over the fields and adds some code for each field. But the code to be generated has mutltiple lines, which is impossible or at least unreadable when put into annotation values. Thus, as an alternative syntax, bodyBeforeIteratorCode, bodyCode, bodyAfterIteratorCode and others can be put into the javadoc comment. Please note the special syntax:

      • The prefix japkit. is necessary to make the "tags" recognizable to japkit at all.
      • Since the bodyBeforeIteratorCode has multiple lines, the <pre> tag is required to keep the line breaks and indentations.
      • The <ul> / <li> notation is for keeping this javadoc comment formatted as it is in the IDE.
      • <li>japkit.code is an alternative to @japkit.code. It has proven to be more resistant to re-formatting applied by the IDE (especially indentation).
      Note that this syntax is not only preferable for multiline code, but also if your are struggling with escaping of quotation marks in the code.

      Also note that the complete <ul> Block will be removed by japkit when the method comment is generated. So, it is only in the template, where it belongs.

      • japkit.bodyBeforeIteratorCode
         if (!(obj instanceof #{genClass.asType().code})) {
          return false;
         }
         #{genClass.asType().code} other = (#{genClass.asType().code}) obj;
         return
         
      • japkit.bodyCode Objects.equals(#{name}, other.#{name})
      • japkit.bodyAfterIteratorCode ;
      Overrides:
      equals in class java.lang.Object