diff --git a/generator/src/main/java/org/sudu/protogen/generator/client/ApiMethodGeneratorBase.java b/generator/src/main/java/org/sudu/protogen/generator/client/ApiMethodGeneratorBase.java index de4ba87..503dbde 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/client/ApiMethodGeneratorBase.java +++ b/generator/src/main/java/org/sudu/protogen/generator/client/ApiMethodGeneratorBase.java @@ -39,12 +39,14 @@ public MethodSpec generate() { .addModifiers(method.getAccessModifier()) .returns(returnType.getTypeName()) .addParameters(params) - .addCode(body(params)) - .addAnnotation( - method.ifNotFoundBehavior() == Options.IfNotFound.NULLIFY - ? context.configuration().nullableAnnotationClass() - : context.configuration().nonnullAnnotationClass() - ); + .addCode(body(params)); + if (!returnType.isPrimitiveOrVoid()) { + builder.addAnnotation( + method.ifNotFoundBehavior() == Options.IfNotFound.NULLIFY + ? context.configuration().nullableAnnotationClass() + : context.configuration().nonnullAnnotationClass() + ); + } switch (method.ifNotFoundBehavior()) { case NULLIFY -> builder.addAnnotation(NullifyIfNotFound.class); case EMPTY -> builder.addAnnotation(EmptyIfNotFound.class); diff --git a/generator/src/main/java/org/sudu/protogen/generator/client/StubCallMethodGenerator.java b/generator/src/main/java/org/sudu/protogen/generator/client/StubCallMethodGenerator.java index f90bd37..21bbb95 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/client/StubCallMethodGenerator.java +++ b/generator/src/main/java/org/sudu/protogen/generator/client/StubCallMethodGenerator.java @@ -9,6 +9,7 @@ import org.sudu.protogen.generator.type.RepeatedType; import org.sudu.protogen.generator.type.TypeModel; import org.sudu.protogen.generator.type.UnfoldedType; +import protogen.Options; import javax.lang.model.element.Modifier; import java.util.List; @@ -35,17 +36,20 @@ public StubCallMethodGenerator(GenerationContext context, Method method, TypeMod } public MethodSpec generate() { - return MethodSpec.methodBuilder(method.generatedName() + "StubCall") + MethodSpec.Builder builder = MethodSpec.methodBuilder(method.generatedName() + "StubCall") .addModifiers(Modifier.PRIVATE) .addParameters(parameters()) .addCode(new BodyGenerator().get()) - .returns(returnType.getTypeName()) - .addAnnotation( - method.ifNotFoundBehavior() == NULLIFY - ? context.configuration().nullableAnnotationClass() - : context.configuration().nonnullAnnotationClass() - ) - .build(); + .returns(returnType.getTypeName()); + + if (!returnType.isPrimitiveOrVoid()) { + builder.addAnnotation( + method.ifNotFoundBehavior() == Options.IfNotFound.NULLIFY + ? context.configuration().nullableAnnotationClass() + : context.configuration().nonnullAnnotationClass() + ); + } + return builder.build(); } private class BodyGenerator implements Supplier { diff --git a/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java b/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java index 7f04e4c..d8b8100 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java +++ b/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java @@ -28,7 +28,7 @@ public FieldProcessingResult generate(Field field) { boolean isNullable = field.isNullable(); - if (!type.getTypeName().isPrimitive()) { + if (!type.isPrimitiveOrVoid()) { if (field.getContainingMessage().getContainingFile().doUseNullabilityAnnotation(isNullable)) { Poem.attachNullabilityAnnotations(fieldSpecBuilder, context, isNullable); } diff --git a/generator/src/main/java/org/sudu/protogen/generator/server/ApiServiceMethodGenerator.java b/generator/src/main/java/org/sudu/protogen/generator/server/ApiServiceMethodGenerator.java index 0a7037e..081a824 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/server/ApiServiceMethodGenerator.java +++ b/generator/src/main/java/org/sudu/protogen/generator/server/ApiServiceMethodGenerator.java @@ -47,12 +47,12 @@ public MethodSpec generate() { * Either adds a return type or a {@code StreamObserver } parameter */ private void specifyResponseWay(MethodSpec.Builder methodBuilder) { - TypeName returnType = responseType(); + TypeModel returnType = responseType(); if (method.isOutputStreaming()) { methodBuilder.returns(TypeName.VOID); methodBuilder.addParameter(generateObserverParameter(returnType)); } else { - if (!returnType.isPrimitive() && method.getContainingFile().doUseNullabilityAnnotation(false)) { + if (!returnType.isPrimitiveOrVoid() && method.getContainingFile().doUseNullabilityAnnotation(false)) { if (method.ifNotFoundBehavior() == Options.IfNotFound.NULLIFY) { methodBuilder.addJavadoc("Null result is converted to Status.NOT_FOUND"); methodBuilder.addAnnotation(context.configuration().nullableAnnotationClass()); @@ -60,19 +60,19 @@ private void specifyResponseWay(MethodSpec.Builder methodBuilder) { methodBuilder.addAnnotation(context.configuration().nonnullAnnotationClass()); } } - methodBuilder.returns(returnType); + methodBuilder.returns(returnType.getTypeName()); } } - private TypeName responseType() { + private TypeModel responseType() { if (responseType != null) { - return responseType.getTypeName(); + return responseType; } if (method.doUnfoldResponse(responseType)) { var field = method.unfoldedResponseField(); - return context.typeManager().processType(field).getTypeName(); + return context.typeManager().processType(field); } - return method.getOutputType().getProtobufTypeName(); + return new TypeModel(method.getOutputType().getProtobufTypeName()); // todo make getProtobufTypeName returning TypeModel } private Iterable generateRequestParameters() { @@ -90,10 +90,10 @@ private Iterable generateRequestParameters() { return List.of(ParameterSpec.builder(requestType.getTypeName(), "request").addAnnotation(NotNull.class).build()); } - private ParameterSpec generateObserverParameter(TypeName responseType) { + private ParameterSpec generateObserverParameter(TypeModel responseType) { TypeName observerType = ParameterizedTypeName.get( ClassName.get("java.util.function", "Consumer"), - responseType.box() + responseType.getTypeName().box() ); return ParameterSpec.builder(observerType, "responseConsumer").addAnnotation(NotNull.class).build(); } diff --git a/generator/src/main/java/org/sudu/protogen/generator/type/TypeModel.java b/generator/src/main/java/org/sudu/protogen/generator/type/TypeModel.java index 49377bf..4e73504 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/type/TypeModel.java +++ b/generator/src/main/java/org/sudu/protogen/generator/type/TypeModel.java @@ -51,4 +51,13 @@ protected final String nextDefinition(Set usedDefinitions) { } throw new IllegalStateException("Failed to define a new variable!"); } + + public boolean isPrimitiveOrVoid() { + return isPrimitiveOrVoid(this); + } + + public static boolean isPrimitiveOrVoid(TypeModel typeModel) { + return typeModel.typeName.isPrimitive() || typeModel.typeName.equals(TypeName.VOID); + } + }