Skip to content

Commit

Permalink
Encapsulate isPrimitive in Type model
Browse files Browse the repository at this point in the history
Method from javapoet returns false for the void type, but it is incorrect for our logic
  • Loading branch information
Duzhinsky committed Nov 9, 2023
1 parent 6fe6f1d commit c38368d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CodeBlock> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,32 @@ public MethodSpec generate() {
* Either adds a return type or a {@code StreamObserver<Response> } 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());
} else {
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<ParameterSpec> generateRequestParameters() {
Expand All @@ -90,10 +90,10 @@ private Iterable<ParameterSpec> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ protected final String nextDefinition(Set<String> 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);
}

}

0 comments on commit c38368d

Please sign in to comment.