GeneratedMessage.Builder is a static nested class inside GeneratedMessage (both of them are abstract classes). When the method build()
is called on an object of type GeneratedMessage.Builder, it returns an object of type GeneratedMessage. Is there some way to declare my raw types T
and B
in the following example, which would make the typecasting in the return statement of convert()
needless.
public class JsonStringToMessageConverter<T extends GeneratedMessage, B extends GeneratedMessage.Builder<B>>
implements IConverter<String, T> {
private final IPbBuilderProvider<B> m_builderProvider;
@Inject
public JsonStringToMessageConverter(IPbBuilderProvider<B> builderProvider) {
m_builderProvider = builderProvider;
}
@Override
public final T convert(String value) {
B builder = m_builderProvider.getPbBuilder();
try {
JsonFormat.merge(value, builder);
} catch (ParseException e) {
throw new LoggedRuntimeException(ErrorType.PARSE_FAILED, e);
}
return (T) builder.build();
}
}
The class GeneratedMessage
is defined in Google's Protobuf library.
You should use type parameter in the Builder
for build()
method return type and declare it as extends GeneratedMessage
.
As I see the build method is not generic, so you cannot do it, I'm afraid.
No there is not since the build
method is defined within the Message.Builder
class and that class is not generic. The build
method returns a Message
object not some generic extension of the Message
class. Therefore there is no generic manipulation of GeneratedMessage.Builder
that will cause the method signature of build
return anything other than Message
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.