An open API service indexing awesome lists of open source software.

https://github.com/labai/labai-jsr305

Jsr305 (nullables) extension for jaxb maven plugin
https://github.com/labai/labai-jsr305

Last synced: 5 months ago
JSON representation

Jsr305 (nullables) extension for jaxb maven plugin

Awesome Lists containing this project

README

          

# labai-jaxb-jsr305

## About
Extension for jaxb maven plugin for generating @NotNull annotation on mandatory fields.

### Purpose

When kotlin access Java classes, it assumes that every field is not null by default.
In most cases it is not true for classes, generated from xsd.
If to reverse to nullable by default, it also will not always be true, as some fields
are not nullable by xsd scheme.

This plugin adds @NotNull annotation on those fields, which are mandatory by xsd.
It adds nullable-by-default annotation on package level also.

For kotlin it will make fields, which can't be null by xsd, as non-nullable - no need for '?' or '!!' operators.
Other fields are nullable and you need to handle null case.

### Usage

Add configuration to maven jaxb plugin in pom.xml:

com.github.labai:labai-jsr305-jaxb-plugin
com.github.labai:labai-jsr305x-annotations

And add argument _-XJsr305Annotations_.

#### Example
(see examples/example3-jaxb-maven-plugin)

```xml

org.jvnet.jaxb
jaxb-maven-plugin
4.0.8



-extension
-Xnamespace-prefix




xsd.pain.001.001.11
generate-sources

generate


src/main/resources/xsd/pain.001.001.11
example3.generated.pain_001_001_11
true

-XJsr305Annotations
-XJsr305Annotations:generateListItemNonnull=true






jakarta.xml.bind
jakarta.xml.bind-api
4.0.2


org.glassfish.jaxb
jaxb-runtime
4.0.5


com.github.labai
labai-jsr305-jaxb-plugin
0.1.0


com.github.labai
labai-jsr305x-annotations
0.0.2

```

Also you will need to add dependency:
```xml

com.github.labai
labai-jsr305x-annotations
0.0.2

```

### Parameters

You may want to use another NotNull annotation
(e.g. kotlin supports a couple of them: https://kotlinlang.org/docs/java-interop.html#nullability-annotations).
Then you may provide additional parameters:

| Parameter | Description | Default |
|:--------------------------------------------|:------------------------------------------------------------------|:------------------------------------------------|
| -XJsr305Annotations:nonnullClass | @Notnull annotation on field, method or parameter | @com.github.labai.jsr305x.api.NotNull |
| -XJsr305Annotations:defaultNullableClass | @NullableByDefault on package-info.java (default for all package) | @com.github.labai.jsr305x.api.NullableByDefault |
| -XJsr305Annotations:generateDefaultNullable | generate @NullableByDefault | true |
| -XJsr305Annotations:generateListItemNonnull | generate @NotNull for list items, e.g. List<@NotNull Item> | false |
| -XJsr305Annotations:verbose | More logs | true |

An example:

```xml
-XJsr305Annotations:defaultNullableClass=org.your.DefaultNullable
-XJsr305Annotations:nonnullClass=org.your.NonNull
-XJsr305Annotations:generateListItemNonnull=true
```

### Versions

- 0.1.x - Jakarta Jaxb4, Java17+
- 0.0.x - Jaxb2, Java8