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

-V, --version#

Show the version and exit.

--generate-records, --no-generate-records#

Optional Java 17 record implementation

--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

Default:

'output'

-f, --format <format>#

Output format

Default:

'java'

Options:

java

--metadata, --no-metadata#

Include metadata in output

Default:

True

--useuris, --metauris#

Use class and slot URIs over model uris

Default:

True

-im, --importmap <importmap>#

Import mapping file

--log_level <log_level>#

Logging level

Default:

'WARNING'

Options:

CRITICAL | ERROR | WARNING | INFO | DEBUG

-v, --verbose#

Verbosity. Takes precedence over –log_level.

--mergeimports, --no-mergeimports#

Merge imports into source file (default=mergeimports)

--stacktrace, --no-stacktrace#

Print a stack trace when an error occurs

Default:

False

Arguments

YAMLFILE#

Required argument

Code#

class linkml.generators.javagen.JavaGenerator(schema: str | ~typing.TextIO | ~linkml_runtime.linkml_model.meta.SchemaDefinition | Generator | ~pathlib.Path, schemaview: ~linkml_runtime.utils.schemaview.SchemaView = None, format: str | None = None, metadata: bool = True, useuris: bool | None = None, log_level: int | None = 30, mergeimports: bool | None = True, source_file_date: str | None = None, source_file_size: int | None = None, logger: ~logging.Logger | None = None, verbose: bool | None = None, output: str | None = None, namespaces: ~linkml_runtime.utils.namespaces.Namespaces | None = None, directory_output: bool = False, base_dir: str = None, metamodel_name_map: ~typing.Dict[str, str] = None, importmap: str | ~typing.Mapping[str, str] | None = None, emit_prefixes: ~typing.Set[str] = <factory>, metamodel: ~linkml.utils.schemaloader.SchemaLoader = None, stacktrace: bool = False, include: str | ~pathlib.Path | ~linkml_runtime.linkml_model.meta.SchemaDefinition | None = None, template_file: str | None = None, package: str = 'example', generate_records: bool = False, gen_classvars: bool = True, gen_slots: bool = True, genmeta: bool = False, emit_metadata: bool = True, **_kwargs)[source]#

Generates java code from a LinkML schema.

Two styles are supported:

  • java classes, using lombok annotations

  • java records

serialize(directory: str, **kwargs) None[source]#

Generate output in the required format

Parameters:

kwargs – Generator 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