Java — linkml documentation

# Java

## Overview

The Java Generator produces java class files from a linkml model, with optional support for user-supplied jinja2 templates to generate classes with alternate annotations or additional documentation.

## Docs

### Command Line

#### gen-java

Generate java classes to represent a LinkML model

    gen-java [OPTIONS] YAMLFILE

Options

--template_file <template_file>

  Optional jinja2 template to use for class generation

--package <package>

  Package name where relevant for generated class files

--output_directory <output_directory>

  Output directory for individually generated class files

-f, --format <format>

  Output format (default=java)

  Options

    java

--metadata, --no-metadata

  Include metadata in output (default=–metadata)

--useuris, --metauris

  Include metadata in output (default=–useuris)

-im, --importmap <importmap>

  Import mapping file

--log_level <log_level>

  Logging level (default=WARNING)

  Options

    CRITICAL | ERROR | WARNING | INFO | DEBUG

--mergeimports, --no-mergeimports

  Merge imports into source file (default=mergeimports)

Arguments

YAMLFILE

  Required argument

### Code

class linkml.generators.javagen.JavaGenerator(schema: Union[str, TextIO, linkml_runtime.linkml_model.meta.SchemaDefinition], package: Optional[str] = None, template_file: Optional[str] = None, format: str = 'java', genmeta: bool = False, gen_classvars: bool = True, gen_slots: bool = True, **kwargs)
  serialize(directory: str) → None

    Generate output in the required format

    Parameters

      kwargs – Generater specific parameters

    Returns

      Generated output

## Additional Notes

The Java generator’s default template uses Project Lombok’s @Data annotation, which provides getters, setters, equals and hashcode functionality.

## Biolink Example

Begin by downloading the Biolink Model YAML and adding a virtual environment and installing linkml.

    curl -OJ https://raw.githubusercontent.com/biolink/biolink-model/master/biolink-model.yaml
    python3 -m venv venv
    source venv/bin/activate
    pip install linkml

Now generate the classes using the gen-java command

    gen-java --package org.biolink.model --output_directory org/biolink/model biolink-model.yaml

Finally, fetch the Lombok jar, build the java classes and package into a jar file

    curl -OJ https://repo1.maven.org/maven2/org/projectlombok/lombok/1.18.20/lombok-1.18.20.jar
    javac org/biolink/model/*.java -cp lombok-1.18.20.jar
    jar -cf biolink-model.jar org

## Alternate Template Example

Here is an alternate template using Hibernate JPA annotations, named example_template.java.jinja2

    package {{ doc.package }};

    import java.util.List;
    import lombok.*;
    import javax.persistence.*;
    import org.hibernate.search.engine.backend.types.*;
    import org.hibernate.envers.Audited;
    import org.hibernate.search.mapper.pojo.mapping.definition.annotation.*;

    @Audited
    @Indexed
    @Entity
    @Data @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
    public class {{ cls.name }} {% if cls.is_a -%} extends {{ cls.is_a }} {%- endif %} {
    {% for f in cls.fields %}
      private {{f.range}} {{ f.name }};
    {%- endfor %}

    }

The alternate template for the generator can be specified with the –template_file option

    gen-java --package org.biolink.model --output_directory org/biolink/model \
             --template_file example_template.java.jinja2 biolink-model.yaml