Class DefaultsCodegen

java.lang.Object
io.helidon.service.codegen.DefaultsCodegen

public final class DefaultsCodegen extends Object
Code generation for handling of Default annotations.
  • Method Details

    • findDefault

      public static Optional<DefaultsCodegen.DefaultCode> findDefault(Set<Annotation> annotations, TypeName targetType)
      Find a default annotation within the set of annotations on an element.
      Parameters:
      annotations - annotations on an element
      targetType - type that is expected in the code
      Returns:
      default annotation, or empty if no default is defined
    • codegenOptional

      public static void codegenOptional(ContentBuilder<?> contentBuilder, DefaultsCodegen.DefaultCode defaultCode, FieldHandler constantHandler, DefaultsParams params)
      Code generate default handling after an optional of the correct type.

      Example with fixed default value (such as Default.Int:

       headers.find(MY_HEADER)
       .map(Header::getInt) // this is expected to be part of the already generated code
       // will be generated by this method
       .orElse(42)
       

      Example with mapping (when using Default.Value and a non-string type):

       headers.find(MY_HEADER)
       .map(Header::getInt) // this is expected to be part of the already generated code
        // will be generated by this method
       .orElseGet(() -> mappers.map("Default Value", GenericType.STRING, GTYPE_1, "headers"))
       
      - mappers - common Mappers instance, must exist in the current class, name is provided as a parameter to this method - GTYPE_1 - genericTypeNameField provided as a parameter to this method (must be generated by caller)
      Parameters:
      contentBuilder - content builder that we add the code to
      defaultCode - as returned by findDefault(java.util.Set, io.helidon.common.types.TypeName)
      constantHandler - handler of constants
      params - additional parameters, needed depending on type of the default