Package org.tribuo.protos
Class ProtoUtil
java.lang.Object
org.tribuo.protos.ProtoUtil
Utilities for working with Tribuo protobufs.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <SERIALIZED extends com.google.protobuf.Message,
PROTO_SERIALIZABLE extends ProtoSerializable<SERIALIZED>>
PROTO_SERIALIZABLEdeserialize
(SERIALIZED serialized) Instantiates the class from the supplied protobuf fields.static String
generateMethodName
(String prefix, String name) Generate the method name for the type from the protobuf.static int
getCurrentVersion
(Class<? extends ProtoSerializable<?>> clazz) If this class is annotated withProtoSerializableClass
returns the version number, otherwise returns -1.static <SERIALIZED_CLASS extends com.google.protobuf.Message,
SERIALIZED_DATA extends com.google.protobuf.Message, PROTO_SERIALIZABLE extends ProtoSerializable<SERIALIZED_CLASS>>
SERIALIZED_CLASSserialize
(PROTO_SERIALIZABLE protoSerializable) Serializes aProtoSerializable
class which has the appropriateProtoSerializableClass
annotations.
-
Method Details
-
deserialize
public static <SERIALIZED extends com.google.protobuf.Message,PROTO_SERIALIZABLE extends ProtoSerializable<SERIALIZED>> PROTO_SERIALIZABLE deserialize(SERIALIZED serialized) Instantiates the class from the supplied protobuf fields.Deserialization proceeds as follows:
- Check to see if there is a valid redirect for this version and class name tuple. If there is then the new class name is used for the following steps.
- Lookup the class name and instantiate the
Class
object. - Find the 3 arg static method
deserializeFromProto(int version, String className, com.google.protobuf.Any message)
. - Call the method passing along the original three arguments (note this uses the original class name even if a redirect has been applied).
- Return the freshly constructed object, or rethrow any runtime exceptions.
Throws
IllegalStateException
if:- the requested class could not be found on the classpath/modulepath
- the requested class does not have the necessary 3 arg constructor
- the deserialization method could not be invoked due to its accessibility, or is in some other way invalid
- the deserialization method threw an exception
- Type Parameters:
SERIALIZED
- The protobuf type.PROTO_SERIALIZABLE
- The deserialized type.- Parameters:
serialized
- The protobuf to deserialize.- Returns:
- The deserialized object.
-
serialize
public static <SERIALIZED_CLASS extends com.google.protobuf.Message,SERIALIZED_DATA extends com.google.protobuf.Message, SERIALIZED_CLASS serializePROTO_SERIALIZABLE extends ProtoSerializable<SERIALIZED_CLASS>> (PROTO_SERIALIZABLE protoSerializable) Serializes aProtoSerializable
class which has the appropriateProtoSerializableClass
annotations.- Type Parameters:
SERIALIZED_CLASS
- The protobuf class.SERIALIZED_DATA
- The serialized data class inside the protobuf.PROTO_SERIALIZABLE
- The class of the proto serializable object.- Parameters:
protoSerializable
- The object to serialize.- Returns:
- The protobuf representation of the input object.
-
getCurrentVersion
If this class is annotated withProtoSerializableClass
returns the version number, otherwise returns -1.- Parameters:
clazz
- The class to check.- Returns:
- The version number, or -1 if it does not use the annotation.
-
generateMethodName
Generate the method name for the type from the protobuf.- Parameters:
prefix
- The prefix, usually "addAll", "set", "put" etc.name
- The field name.- Returns:
- The method name.
-