return (TYPE) getBuilder(messageClass).getDefaultInstanceForType(); } catch (Exception e) { logger.warn("Error building protobuf object of type {} from json: {}",
/** * Utility function for creating a default PB Messgae from a Class object that * works with both protoc 2.3.0 and 2.4.x. * @param clazz The class of the protocol buffer to create * @return An instance of a protocol buffer */ public static <M extends Message> M getDefaultInstance(Class<M> clazz) { if (clazz.getConstructors().length > 0) { // Protobuf 2.3.0 return ReflectionUtils.newInstance(clazz, null); } else { // Protobuf 2.4.x try { Message.Builder mb = (Message.Builder) clazz.getDeclaredMethod("newBuilder").invoke(null); return (M) mb.getDefaultInstanceForType(); } catch (Exception e) { throw new CrunchRuntimeException(e); } } }
/** * Utility function for creating a default PB Messgae from a Class object that * works with both protoc 2.3.0 and 2.4.x. * @param clazz The class of the protocol buffer to create * @return An instance of a protocol buffer */ public static <M extends Message> M getDefaultInstance(Class<M> clazz) { if (clazz.getConstructors().length > 0) { // Protobuf 2.3.0 return ReflectionUtils.newInstance(clazz, null); } else { // Protobuf 2.4.x try { Message.Builder mb = (Message.Builder) clazz.getDeclaredMethod("newBuilder").invoke(null); return (M) mb.getDefaultInstanceForType(); } catch (Exception e) { throw new CrunchRuntimeException(e); } } }
subMessageType = defaultInstance.getClass(); } else { subMessageType = builder.newBuilderForField(field).getDefaultInstanceForType().getClass();
private JsonDeserializer<Object> getMessageDeserializer( Message.Builder builder, FieldDescriptor field, Message defaultInstance, DeserializationContext context ) throws IOException { JsonDeserializer<Object> deserializer = deserializerCache.get(field); if (deserializer == null) { final Class<?> subType; if (defaultInstance == null) { Message.Builder subBuilder = builder.newBuilderForField(field); subType = subBuilder.getDefaultInstanceForType().getClass(); } else { subType = defaultInstance.getClass(); } JavaType type = context.constructType(subType); deserializer = context.findContextualValueDeserializer(type, null); deserializerCache.put(field, deserializer); } return deserializer; }
/** * Decode the a protobuf from the given input stream * @param builder - Builder of the protobuf to decode * @param dis - DataInputStream to read the protobuf * @return Message - decoded protobuf * @throws WrappedRpcServerException - deserialization failed */ @SuppressWarnings("unchecked") private <T extends Message> T decodeProtobufFromStream(Builder builder, DataInputStream dis) throws WrappedRpcServerException { try { builder.mergeDelimitedFrom(dis); return (T)builder.build(); } catch (Exception ioe) { Class<?> protoClass = builder.getDefaultInstanceForType().getClass(); throw new WrappedRpcServerException( RpcErrorCodeProto.FATAL_DESERIALIZING_REQUEST, "Error decoding " + protoClass.getSimpleName() + ": "+ ioe); } }
/** * Decode the a protobuf from the given input stream * @param builder - Builder of the protobuf to decode * @param dis - DataInputStream to read the protobuf * @return Message - decoded protobuf * @throws WrappedRpcServerException - deserialization failed */ @SuppressWarnings("unchecked") private <T extends Message> T decodeProtobufFromStream(Builder builder, DataInputStream dis) throws WrappedRpcServerException { try { builder.mergeDelimitedFrom(dis); return (T)builder.build(); } catch (Exception ioe) { Class<?> protoClass = builder.getDefaultInstanceForType().getClass(); throw new WrappedRpcServerException( RpcErrorCodeProto.FATAL_DESERIALIZING_REQUEST, "Error decoding " + protoClass.getSimpleName() + ": "+ ioe); } }
/** * Clears the value of the field. * * @return the builder */ @Override @SuppressWarnings("unchecked") public void clear() { message = (MType) (message != null ? message.getDefaultInstanceForType() : builder .getDefaultInstanceForType()); if (builder != null) { dispose(builder); builder = null; } onChanged(); }
@SuppressWarnings("unchecked") @Override public T defaultValue() { return (T) internalBuilder.getDefaultInstanceForType(); }
@SuppressWarnings("unchecked") public M fromBytes(byte[] messageBuffer, int offset, int len) throws InvalidProtocolBufferException, UninitializedMessageException { if (defaultInstance == null) { defaultInstance = Protobufs.getMessageBuilder(typeRef.getRawClass()) .getDefaultInstanceForType(); } return (M) defaultInstance.newBuilderForType() .mergeFrom(messageBuffer, offset, len) .build(); }