typescript-generator
typescript-generator copied to clipboard
Failure with jetbrains @Nullable annotation
I have a project in which I try to generate typescript DTO from Java one with some nullable fields.
I use jetbrains Nullable annotation which is annotated @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})
.
However when I try to process classes with maven, I get the following error :
[ERROR] Failed to execute goal cz.habarta.typescript-generator:typescript-generator-maven-plugin:2.35.1025:generate (generate) on project TestTypescriptGenerator: Execution generate of goal cz.habarta.typescript-generator:typescript-generator-maven-plugin:2.35.1025:generate failed: 'org.jetbrains.annotations.Nullable' annotation cannot be used as nullable annotation because it doesn't have 'TYPE_PARAMETER' or 'TYPE_USE' target.
Moreover, I tried to create my own nullable annotation that I called MyNullable
with the same target as the jetbrains nullable one, and the maven processing classes works without any error.
I have a sample project to reproduce the issue : https://github.com/FlorianCousin/test-typescript-generator
Is there something I misunderstood in the available nullable annotations ?
After some investigation, it might be because typescript-generator loads the jetbrains nullable annotation that is imported in its own pom instead of loading the one that is imported in my project pom.
As there is a version difference between both jetbrains nullable annotations, the targets are not the same and so the validation throws an error for the typescript-generator annotation, whereas it should not throw any exception for my project annotation.
Running into this as well. I suspect it's because the JetBrains annotation Retention
is CLASS
instead of RUNTIME
. If so the error message is a little misleading.
package org.jetbrains.annotations;
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})
public @interface Nullable {
// ...
}
I created my own annotation which is working well, using RetentionPolicy.RUNTIME
:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
public @interface NullableType {
}
Thank you for your answer @shmert.
As we can see in my sample project, when I create my own nullable annotation with the same ones as the jetbrains annotation, that is the following annotations:
@Documented
@Retention(RetentionPolicy.CLASS)
@Target({
ElementType.METHOD,
ElementType.FIELD,
ElementType.PARAMETER,
ElementType.LOCAL_VARIABLE,
ElementType.TYPE_USE
})
Then the compilation works.
Therefore, I don't think the class retention is the same problem as the one I mentioned.
There are two problems with JetBrains @Nullable
annotation:
- old version without
TYPE_USE
is used because this project useskotlin-stdlib
which depends on very oldorg.jetbrains:annotations
- but more importantly: it doesn't have runtime retention
So I added warning that such annotation doesn't have any effect (and I also updated that dependency).
Released in 3.1.1185.