Skip to content

Commit

Permalink
Make service api method not abstract
Browse files Browse the repository at this point in the history
In case when a developer wants to override original grpc method it's necessary to make dumb implementation of abstract method. I made the api method returning UNIMPLEMENTED status by default to deal with that
  • Loading branch information
Duzhinsky committed Oct 18, 2023
1 parent 3df5e31 commit cc1e6c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import javax.lang.model.element.Modifier;
import java.util.List;

public class AbstractServiceMethodGenerator {
public class ApiServiceMethodGenerator {

private final GenerationContext context;

Expand All @@ -22,7 +22,7 @@ public class AbstractServiceMethodGenerator {

private final @Nullable TypeModel responseType;

public AbstractServiceMethodGenerator(GenerationContext context, Method method) {
public ApiServiceMethodGenerator(GenerationContext context, Method method) {
this.context = context;
this.method = method;
this.requestType = context.typeManager().processType(method.getInputType());
Expand All @@ -31,8 +31,13 @@ public AbstractServiceMethodGenerator(GenerationContext context, Method method)

public MethodSpec generate() {
MethodSpec.Builder builder = MethodSpec.methodBuilder(method.generatedName())
.addModifiers(Modifier.PROTECTED, Modifier.ABSTRACT)
.addParameters(generateRequestParameters());
.addModifiers(Modifier.PROTECTED)
.addParameters(generateRequestParameters())
.addCode(CodeBlock.of(
"throw $T.UNIMPLEMENTED.withDescription(\"Method $L is not implemented\").asRuntimeException();",
ClassName.get("io.grpc", "Status"),
method.getName()
));
specifyResponseWay(builder);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ private Iterable<MethodSpec> methods(Service service) {
return service.getMethods().stream()
.filter(Method::doGenerate)
.flatMap(method -> {
MethodSpec abstractMethod = new AbstractServiceMethodGenerator(context, method).generate();
MethodSpec overriddenMethod = new OverriddenServiceMethodGenerator(context, method, abstractMethod).generate();
return Stream.of(abstractMethod, overriddenMethod);
MethodSpec apiMethod = new ApiServiceMethodGenerator(context, method).generate();
MethodSpec overriddenMethod = new OverriddenServiceMethodGenerator(context, method, apiMethod).generate();
return Stream.of(apiMethod, overriddenMethod);
})
.toList();
}
Expand Down

0 comments on commit cc1e6c7

Please sign in to comment.