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.
- --true-enums, --no-true-enums¶
Treat enums as distinct types rather than strings
- --visitor <visitor>¶
Generate a visitor interface for the specified class
- --extra-template <extra_template>¶
Name of an additional, arbitrary template to use
- --generate-records, --no-generate-records¶
Optional Java 17 record implementation (deprecated, use –template-variant=records instead)
- --template-file <template_file>¶
Optional jinja2 template to use for class generation (takes precedence over –template-dir)
- --template-variant <template_variant>¶
Use the specified template variant
- --template-dir <template_dir>¶
Directory containing the Jinja2 templates to use
- --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: dict[str, str] = None, importmap: str | ~collections.abc.Mapping[str, str] | None = None, emit_prefixes: 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, true_enums: bool = False, package: str = 'example', template_dir: ~pathlib.Path | None = None, template_cache: ~linkml.generators.javagen.TemplateCache = <factory>, gen_classvars: bool = True, gen_slots: bool = True, genmeta: bool = False)[source]¶
Generates java code from a LinkML schema.
This generators supports an arbitrary number of different styles through the use of “template variants“.
Currently, two variants are available:
the default variant represents LinkML classes as Java classes carrying Lombok annotations (https://projectlombok.org);
the records variant represents LinkML classes as Java 16 records.
- serialize(directory: str, template_variant: str | None = None, extra_templates: list[str] | None = None, visitors: list[str] | None = None, **kwargs) None[source]¶
Generate and write the Java code to files.
- Parameters:
directory – The directory where to write the code files.
template_variant – The name of the template variant to use, if any.
extra_templates – A list of additional templates from which to generate additional code files. For example, if set to [Foo,Bar], this will generate two additional files Foo.java and Bar.java (assuming the template directory contains the required templates Foo.jinja2 and Bar.jinja2). Users can exploit such additional files to generate any code they might need in addition to the code generated for each class and each enum in the model.
visitors – A list of class names for which to generate a visitor interface. For example, if set to [Foo], this will generate a IFooVisitor interface, and the generated code for both the Foo class and all its descendants will include a accept(IFooVisitor) method.
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