@@ -852,19 +852,24 @@ private void sanitizeConfig() {
852852
853853 protected void applyJavaxPackage () {
854854 writePropertyBack (JAVAX_PACKAGE , "javax" );
855- writePropertyBack ("nullableAnnotation" , "@javax.annotation.Nullable" );
856- writePropertyBack ("nonNullAnnotation" , "@javax.annotation.Nonnull" );
857855 }
858856
859857 protected void applyJakartaPackage () {
860858 writePropertyBack (JAVAX_PACKAGE , "jakarta" );
861- writePropertyBack ("nullableAnnotation" , "@jakarta.annotation.Nullable" );
862- writePropertyBack ("nonNullAnnotation" , "@jakarta.annotation.Nonnull" );
863859 }
864860
865861 protected void applyJspecify () {
866- writePropertyBack ("nullableAnnotation" , "@org.jspecify.annotations.Nullable" );
867- writePropertyBack ("nonNullAnnotation" , "@NonNull" );
862+ importMapping .put ("Nullable" , "org.jspecify.annotations.Nullable" );
863+ if (Boolean .TRUE .equals (additionalProperties .get (CodegenConstants .GENERATE_MODELS ))) {
864+ supportingFiles .add (new SupportingFile ("modelPackageInfo.mustache" ,
865+ (sourceFolder + File .separator + modelPackage ).replace ("." , java .io .File .separator ),
866+ "package-info.java" ));
867+ }
868+ if (Boolean .TRUE .equals (additionalProperties .get (CodegenConstants .GENERATE_APIS ))) {
869+ supportingFiles .add (new SupportingFile ("apiPackageInfo.mustache" ,
870+ (sourceFolder + File .separator + apiPackage ).replace ("." , java .io .File .separator ),
871+ "package-info.java" ));
872+ }
868873 }
869874
870875 @ Override
@@ -2677,31 +2682,33 @@ public void setEnumPropertyNaming(final String enumPropertyNamingType) {
26772682 @ Override
26782683 protected ImmutableMap .Builder <String , Mustache .Lambda > addMustacheLambdas () {
26792684 this .jSpecifyNullableLambda = new JSpecifyNullableLambda ();
2685+ // Add jSpecify nullable annotation in the correct location before or inside a declartion
2686+ Mustache .Lambda jSpecifyDatatypeLambda = (fragment , writer ) -> {
2687+ String dataType = fragment .execute ();
2688+ if (jSpecifyNullableLambda .keptNullable ) {
2689+ jSpecifyNullableLambda .keptNullable = false ;
2690+ int idx = dataType .lastIndexOf ('.' );
2691+ if (idx > 0 ) {
2692+ // generate declaration like java.time.@Nullable Timestamp
2693+ writer .write (dataType .substring (0 , idx + 1 ));
2694+ writer .write ("@Nullable " );
2695+ writer .write (dataType .substring (idx + 1 ));
2696+ } else {
2697+ writer .write ("@Nullable " );
2698+ writer .write (dataType );
2699+ }
2700+ } else {
2701+ writer .write (dataType );
2702+ }
2703+ };
26802704 return super .addMustacheLambdas ()
2681- .put ("jSpecifyDatatype" ,(fragment , writer ) -> {
2682- String dataType = fragment .execute ();
2683- if (jSpecifyNullableLambda .keptNullable ) {
2684- jSpecifyNullableLambda .keptNullable = false ;
2685- int idx = dataType .lastIndexOf ('.' );
2686- if (idx > 0 ) {
2687- // generate declareation like java.time.@Nullable Timestamp
2688- writer .write (dataType .substring (0 , idx + 1 ));
2689- writer .write ("@Nullable " );
2690- writer .write (dataType .substring (idx + 1 ));
2691- } else {
2692- writer .write ("@Nullable " );
2693- writer .write (dataType );
2694- }
2695- } else {
2696- writer .write (dataType );
2697- }
2698- })
2705+ .put ("jSpecifyDatatype" , jSpecifyDatatypeLambda )
26992706 .put ("jSpecifyNullable" , jSpecifyNullableLambda );
27002707
27012708 }
27022709
27032710 /**
2704- * for Jspecify, remove @Nullable before the datatype.
2711+ * for Jspecify, remove @Nullable before the datatype and set keptNullable to true if done .
27052712 */
27062713 class JSpecifyNullableLambda implements Mustache .Lambda {
27072714 private String nullableAnnotation = "@Nullable" ;
@@ -2731,17 +2738,6 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
27312738 }
27322739 }
27332740
2734- protected void addPackagInfoSupportingFiles () {
2735- if (useJspecify ) {
2736- supportingFiles .add (new SupportingFile ("modelPackageInfo.mustache" ,
2737- (sourceFolder + File .separator + modelPackage ).replace ("." , java .io .File .separator ),
2738- "package-info.java" ));
2739- supportingFiles .add (new SupportingFile ("apiPackageInfo.mustache" ,
2740- (sourceFolder + File .separator + apiPackage ).replace ("." , java .io .File .separator ),
2741- "package-info.java" ));
2742- }
2743- }
2744-
27452741 /**
27462742 * Adds Nullable import if any parameter is nullable or optional.
27472743 */
0 commit comments