@Override public boolean isInitialized() { for (final FieldDescriptor field : getDescriptorForType().getFields()) { // Check that all required fields are present. if (field.isRequired()) { if (!hasField(field)) { return false; } } // Check that embedded messages are initialized. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { @SuppressWarnings("unchecked") final List<Message> messageList = (List<Message>) getField(field); for (final Message element : messageList) { if (!element.isInitialized()) { return false; } } } else { if (hasField(field) && !((Message) getField(field)).isInitialized()) { return false; } } } } return true; }
@Override public boolean isInitialized() { for (final FieldDescriptor field : getDescriptorForType().getFields()) { // Check that all required fields are present. if (field.isRequired()) { if (!hasField(field)) { return false; } } // Check that embedded messages are initialized. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { @SuppressWarnings("unchecked") final List<Message> messageList = (List<Message>) getField(field); for (final Message element : messageList) { if (!element.isInitialized()) { return false; } } } else { if (hasField(field) && !((Message) getField(field)).isInitialized()) { return false; } } } } return true; }
protected final boolean supportsConfigField(final String name) throws CouldNotPerformException { synchronized (CONFIG_LOCK) { try { Descriptors.FieldDescriptor findFieldByName = config.getDescriptorForType().findFieldByName(name); return findFieldByName != null; } catch (NullPointerException ex) { return false; } } }
protected final boolean supportsConfigField(final String name) throws CouldNotPerformException { synchronized (CONFIG_LOCK) { try { Descriptors.FieldDescriptor findFieldByName = config.getDescriptorForType().findFieldByName(name); return findFieldByName != null; } catch (NullPointerException ex) { return false; } } }
/** * Creates a random protobuf generator, for a main type (instance) and a list * of any extra instances that will be needed to generate that instance. * * @param random random number generator * @param instance the protobuf instance used as a template to generate more * random protobuf * @param moreInstances protobuf templates for any inner messages the * protobuf depends on * @return the random protobuf generator */ public static <T extends GeneratedMessage> RandomProtobufGenerator<T> create(Random random, GeneratedMessage instance, GeneratedMessage... moreInstances) { // NOTE: it would be nice to determine this internally e.g. through (java or proto) reflection. Map<Descriptor, GeneratedMessage> moreInstancesMap = Maps.newHashMap(); for (GeneratedMessage gm : moreInstances) { moreInstancesMap.put(gm.getDescriptorForType(), gm); } return new RandomProtobufGenerator<T>(random, instance, moreInstancesMap); }
@Override public String generateId(final M message) { // in case we are performing a unit test let's generate a human readable uuid. if (JPService.testMode()) { if (message.getDescriptorForType().findFieldByName(TYPE_FIELD_LABEL) != null) { if (message.hasField(message.getDescriptorForType().findFieldByName(TYPE_FIELD_LABEL))) { return message.getField(message.getDescriptorForType().findFieldByName(TYPE_FIELD_LABEL)) + ":"+ UUID.randomUUID().toString(); } } } return UUID.randomUUID().toString(); } }
protected final boolean hasConfigField(final String name) throws CouldNotPerformException { synchronized (CONFIG_LOCK) { try { Descriptors.FieldDescriptor findFieldByName = config.getDescriptorForType().findFieldByName(name); if (findFieldByName == null) { return false; } return config.hasField(findFieldByName); } catch (Exception ex) { return false; } } }
protected final boolean hasConfigField(final String name) throws CouldNotPerformException { synchronized (CONFIG_LOCK) { try { Descriptors.FieldDescriptor findFieldByName = config.getDescriptorForType().findFieldByName(name); if (findFieldByName == null) { return false; } return config.hasField(findFieldByName); } catch (Exception ex) { return false; } } }
public static List<String> unknownFields(GeneratedMessage message) { return unknownFields(message.getDescriptorForType().getFullName(), message); }
protected final Object getConfigField(String name, final CONFIG config) throws CouldNotPerformException { try { Descriptors.FieldDescriptor findFieldByName = config.getDescriptorForType().findFieldByName(name); if (findFieldByName == null) { throw new NotAvailableException("Field[" + name + "] does not exist for type " + config.getClass().getName()); } return config.getField(findFieldByName); } catch (Exception ex) { throw new CouldNotPerformException("Could not return value of config field [" + name + "] for " + this, ex); } }
protected final Object getConfigField(String name, final CONFIG config) throws CouldNotPerformException { try { Descriptors.FieldDescriptor findFieldByName = config.getDescriptorForType().findFieldByName(name); if (findFieldByName == null) { throw new NotAvailableException("Field[" + name + "] does not exist for type " + config.getClass().getName()); } return config.getField(findFieldByName); } catch (Exception ex) { throw new CouldNotPerformException("Could not return value of config field [" + name + "] for " + this, ex); } }
protected final Object getDataField(String name) throws CouldNotPerformException { try { Descriptors.FieldDescriptor findFieldByName = getData().getDescriptorForType().findFieldByName(name); if (findFieldByName == null) { throw new NotAvailableException("Field[" + name + "] does not exist for type " + getData().getClass().getName()); } return getData().getField(findFieldByName); } catch (Exception ex) { throw new CouldNotPerformException("Could not return value of field [" + name + "] for " + this, ex); } }
protected final boolean hasDataField(final String name) throws CouldNotPerformException { try { Descriptors.FieldDescriptor findFieldByName = getData().getDescriptorForType().findFieldByName(name); if (findFieldByName == null) { return false; } return getData().hasField(findFieldByName); } catch (Exception ex) { return false; } }
/** * Generates a random protocol buffer, filling in all required fields but * with a p chance of not setting an optional field and p chance of having * an empty repeated field. */ @SuppressWarnings("unchecked") public E generate(double p) { Builder builder = instance.newBuilderForType(); Descriptor descriptor = instance.getDescriptorForType(); for (FieldDescriptor field : descriptor.getFields()) { if (!field.isRequired() && random.nextDouble() < p) { continue; } builder.setField(field, getRandomValue(field, p)); } return (E) builder.build(); }
@Override public boolean isInitialized() { for (final FieldDescriptor field : getDescriptorForType().getFields()) { // Check that all required fields are present. if (field.isRequired()) { if (!hasField(field)) { return false; } } // Check that embedded messages are initialized. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { @SuppressWarnings("unchecked") final List<Message> messageList = (List<Message>) getField(field); for (final Message element : messageList) { if (!element.isInitialized()) { return false; } } } else { if (hasField(field) && !((Message) getField(field)).isInitialized()) { return false; } } } } return true; }
@Override public boolean isInitialized() { for (final FieldDescriptor field : getDescriptorForType().getFields()) { // Check that all required fields are present. if (field.isRequired()) { if (!hasField(field)) { return false; } } // Check that embedded messages are initialized. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { @SuppressWarnings("unchecked") final List<Message> messageList = (List<Message>) getField(field); for (final Message element : messageList) { if (!element.isInitialized()) { return false; } } } else { if (hasField(field) && !((Message) getField(field)).isInitialized()) { return false; } } } } return true; }