typescript-generator icon indicating copy to clipboard operation
typescript-generator copied to clipboard

Failure with jetbrains @Nullable annotation

Open FlorianCousin opened this issue 2 years ago • 3 comments

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 ?

FlorianCousin avatar Feb 11 '22 22:02 FlorianCousin

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.

FlorianCousin avatar Feb 12 '22 09:02 FlorianCousin

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

shmert avatar Mar 20 '22 20:03 shmert

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.

FlorianCousin avatar Mar 21 '22 09:03 FlorianCousin

There are two problems with JetBrains @Nullable annotation:

  • old version without TYPE_USE is used because this project uses kotlin-stdlib which depends on very old org.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).

vojtechhabarta avatar Nov 18 '22 14:11 vojtechhabarta

Released in 3.1.1185.

vojtechhabarta avatar Dec 12 '22 22:12 vojtechhabarta