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)[source]
serialize(directory: str) None[source]

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.

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