use of com.google.protobuf.MessageLite in project retrofit by square.
the class ProtoConverterFactory method responseBodyConverter.
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
if (!(type instanceof Class<?>)) {
return null;
}
Class<?> c = (Class<?>) type;
if (!MessageLite.class.isAssignableFrom(c)) {
return null;
}
Parser<MessageLite> parser;
try {
Method method = c.getDeclaredMethod("parser");
// noinspection unchecked
parser = (Parser<MessageLite>) method.invoke(null);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
} catch (NoSuchMethodException | IllegalAccessException ignored) {
// If the method is missing, fall back to original static field for pre-3.0 support.
try {
Field field = c.getDeclaredField("PARSER");
// noinspection unchecked
parser = (Parser<MessageLite>) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalArgumentException("Found a protobuf message but " + c.getName() + " had no parser() method or PARSER field.");
}
}
return new ProtoResponseBodyConverter<>(parser, registry);
}
use of com.google.protobuf.MessageLite in project ballerina by ballerina-lang.
the class Message method writeTo.
public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {
Descriptors.Descriptor messageDescriptor = getDescriptor();
for (Descriptors.FieldDescriptor fieldDescriptor : messageDescriptor.getFields()) {
if (fields.containsKey(fieldDescriptor.getName())) {
switch(fieldDescriptor.getType().toProto().getNumber()) {
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Double[] messages = (Double[]) msgObject;
for (Double message : messages) {
output.writeDouble(fieldDescriptor.getNumber(), message);
}
} else {
output.writeDouble(fieldDescriptor.getNumber(), (Double) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Float[] messages = (Float[]) msgObject;
for (Float message : messages) {
output.writeFloat(fieldDescriptor.getNumber(), message);
}
} else {
output.writeFloat(fieldDescriptor.getNumber(), (Float) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Long[] messages = (Long[]) msgObject;
for (Long message : messages) {
output.writeInt64(fieldDescriptor.getNumber(), message);
}
} else {
output.writeInt64(fieldDescriptor.getNumber(), (Long) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Long[] messages = (Long[]) msgObject;
for (Long message : messages) {
output.writeUInt64(fieldDescriptor.getNumber(), message);
}
} else {
output.writeUInt64(fieldDescriptor.getNumber(), (Long) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Integer[] messages = (Integer[]) msgObject;
for (Integer message : messages) {
output.writeInt32(fieldDescriptor.getNumber(), message);
}
} else {
output.writeInt32(fieldDescriptor.getNumber(), (Integer) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Long[] messages = (Long[]) msgObject;
for (Long message : messages) {
output.writeFixed64(fieldDescriptor.getNumber(), message);
}
} else {
output.writeFixed64(fieldDescriptor.getNumber(), (Long) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Integer[] messages = (Integer[]) msgObject;
for (Integer message : messages) {
output.writeFixed32(fieldDescriptor.getNumber(), message);
}
} else {
output.writeFixed32(fieldDescriptor.getNumber(), (Integer) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Boolean[] messages = (Boolean[]) msgObject;
for (Boolean message : messages) {
output.writeBool(fieldDescriptor.getNumber(), message);
}
} else {
output.writeBool(fieldDescriptor.getNumber(), (Boolean) msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
String[] messages = (String[]) msgObject;
for (String message : messages) {
output.writeString(fieldDescriptor.getNumber(), message);
}
} else {
com.google.protobuf.GeneratedMessageV3.writeString(output, fieldDescriptor.getNumber(), msgObject);
}
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
{
Object msgObject = fields.get(fieldDescriptor.getName());
if (MessageUtils.isArray(msgObject)) {
Object[] messages = (Object[]) msgObject;
for (Object message : messages) {
output.writeMessage(fieldDescriptor.getNumber(), (MessageLite) message);
}
} else {
output.writeMessage(fieldDescriptor.getNumber(), (MessageLite) msgObject);
}
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while writing output stream. Field " + "type is not supported : " + fieldDescriptor.getType());
}
}
}
}
unknownFields.writeTo(output);
}
use of com.google.protobuf.MessageLite in project tink by google.
the class Ed25519PrivateKeyManagerTest method testBasic.
@Test
public void testBasic() throws Exception {
Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
KeyTemplate template = SignatureKeyTemplates.ED25519;
MessageLite key = manager.newKey(template);
assertTrue(key instanceof Ed25519PrivateKey);
Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
assertEquals(32, keyProto.getKeyValue().size());
PublicKeySign signer = manager.getPrimitive(key);
assertTrue(signer instanceof Ed25519Sign);
byte[] message = Random.randBytes(20);
byte[] signature = signer.sign(message);
assertEquals(64, signature.length);
Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
assertTrue(verifier instanceof Ed25519Verify);
try {
verifier.verify(signature, message);
} catch (GeneralSecurityException e) {
fail("Do not expect GeneralSecurityException: " + e);
}
}
use of com.google.protobuf.MessageLite in project j2objc by google.
the class CompatibilityTest method testMessageLiteToBuilderAndMergeFrom.
public void testMessageLiteToBuilderAndMergeFrom() throws Exception {
TypicalData input = TypicalData.newBuilder().setMyInt(123).build();
MessageLite msg = TypicalData.getDefaultInstance();
// mergeFrom(byte[], ExtensionRegistryLite)
MessageLite.Builder builder = msg.toBuilder();
builder.mergeFrom(input.toByteString().toByteArray(), ExtensionRegistry.getEmptyRegistry());
assertEquals(123, ((TypicalData) builder.build()).getMyInt());
// mergeFrom(byte[])
builder = msg.toBuilder();
builder.mergeFrom(input.toByteString().toByteArray());
assertEquals(123, ((TypicalData) builder.build()).getMyInt());
// mergeFrom(ByteString, ExtensionRegistryLite)
builder = msg.toBuilder();
builder.mergeFrom(input.toByteString(), ExtensionRegistry.getEmptyRegistry());
assertEquals(123, ((TypicalData) builder.build()).getMyInt());
// mergeFrom(ByteString)
builder = msg.toBuilder();
builder.mergeFrom(input.toByteString());
assertEquals(123, ((TypicalData) builder.build()).getMyInt());
}
use of com.google.protobuf.MessageLite in project j2objc by google.
the class CompatibilityTest method testMessageLiteInterface.
public void testMessageLiteInterface() throws Exception {
ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance();
TypicalData data = TypicalData.newBuilder().build();
MessageLite messageLite = data;
MessageLite.Builder builderLite = messageLite.newBuilderForType();
messageLite.writeTo(new ByteArrayOutputStream());
messageLite.writeDelimitedTo(new ByteArrayOutputStream());
builderLite.mergeFrom(new ByteArrayInputStream(new byte[0]));
builderLite.mergeFrom(new ByteArrayInputStream(new byte[0]), registry);
builderLite.mergeDelimitedFrom(new ByteArrayInputStream(new byte[0]));
builderLite.mergeDelimitedFrom(new ByteArrayInputStream(new byte[0]), registry);
assertEquals(0, messageLite.getSerializedSize());
}
Aggregations