use of com.google.protobuf.Descriptors.FieldDescriptor in project j2objc by google.
the class CompatibilityTest method testSetAndGetFieldWithFieldDescriptor.
public void testSetAndGetFieldWithFieldDescriptor() throws Exception {
FieldDescriptor[] fields = new FieldDescriptor[19];
Descriptor descriptor = TypicalData.Builder.getDescriptor();
for (int i = 1; i <= 18; i++) {
fields[i] = descriptor.findFieldByNumber(i);
}
TypicalData.Builder dataBuilder = TypicalData.newBuilder();
dataBuilder.setField(fields[1], new Integer(42));
dataBuilder.setField(fields[2], ByteString.copyFrom("foo".getBytes()));
dataBuilder.setField(fields[3], TypicalData.EnumType.VALUE9.getValueDescriptor());
dataBuilder.setField(fields[11], TypicalDataMessage.newBuilder().build());
dataBuilder.setField(fields[12], Boolean.TRUE);
dataBuilder.setField(fields[13], new Float(43.8));
dataBuilder.setField(fields[14], new Double(44.5));
dataBuilder.setField(fields[15], "bar");
dataBuilder.setField(fields[16], new Integer(24));
dataBuilder.setField(fields[17], new Long(4422));
dataBuilder.setField(fields[18], new Long(2244));
dataBuilder.addRepeatedField(fields[4], new Integer(72));
dataBuilder.addRepeatedField(fields[8], "aaa");
dataBuilder.addRepeatedField(fields[8], "bbb");
dataBuilder.setRepeatedField(fields[8], 1, "ccc");
ArrayList<Double> doubles = new ArrayList<Double>();
doubles.add(1.2);
doubles.add(3.4);
dataBuilder.setField(fields[7], doubles);
checkGetters(dataBuilder.build(), fields);
checkGetters(dataBuilder, fields);
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project j2objc by google.
the class CompatibilityTest method testGetJavaType.
public void testGetJavaType() throws Exception {
Descriptor descriptor = TypicalData.Builder.getDescriptor();
FieldDescriptor intField = descriptor.findFieldByNumber(1);
assertEquals(FieldDescriptor.JavaType.INT, intField.getJavaType());
FieldDescriptor bytesField = descriptor.findFieldByNumber(2);
assertEquals(FieldDescriptor.JavaType.BYTE_STRING, bytesField.getJavaType());
FieldDescriptor booleanField = descriptor.findFieldByNumber(5);
assertEquals(FieldDescriptor.JavaType.BOOLEAN, booleanField.getJavaType());
FieldDescriptor stringField = descriptor.findFieldByNumber(8);
assertEquals(FieldDescriptor.JavaType.STRING, stringField.getJavaType());
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project j2objc by google.
the class CompatibilityTest method testFindFieldByNumber.
public void testFindFieldByNumber() throws Exception {
Descriptor descriptor = TypicalData.Builder.getDescriptor();
Collection<FieldDescriptor> fields = descriptor.getFields();
for (FieldDescriptor field : fields) {
FieldDescriptor.Type type = field.getType();
int fieldId = field.getNumber();
switch(fieldId) {
case 1:
assertEquals(Type.INT32, type);
break;
case 2:
assertEquals(Type.BYTES, type);
break;
case 3:
assertEquals(Type.ENUM, type);
break;
}
FieldDescriptor result = descriptor.findFieldByNumber(fieldId);
assertEquals(field.getNumber(), result.getNumber());
assertEquals(field.getName(), result.getName());
}
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project beam by apache.
the class ProtobufUtil method verifyDeterministic.
/**
* Recursively checks whether the specified class uses any Protocol Buffers fields that cannot
* be deterministically encoded.
*
* @throws NonDeterministicException if the object cannot be encoded deterministically.
*/
static void verifyDeterministic(ProtoCoder<?> coder) throws NonDeterministicException {
Class<? extends Message> message = coder.getMessageType();
ExtensionRegistry registry = coder.getExtensionRegistry();
Set<Descriptor> descriptors = getRecursiveDescriptorsForClass(message, registry);
for (Descriptor d : descriptors) {
for (FieldDescriptor fd : d.getFields()) {
// be encoded deterministically.
if (fd.isMapField()) {
String reason = String.format("Protocol Buffers message %s transitively includes Map field %s (from file %s)." + " Maps cannot be deterministically encoded.", message.getName(), fd.getFullName(), fd.getFile().getFullName());
throw new NonDeterministicException(coder, reason);
}
}
}
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class MessageField method getFieldName.
/**
* Obtains Protobuf field name for the passed message.
*
* @param msg a message to inspect
* @param index a zero-based index of the field
* @return name of the field
*/
public static String getFieldName(Message msg, int index) {
final FieldDescriptor fieldDescriptor = getFieldDescriptor(msg, index);
final String fieldName = fieldDescriptor.getName();
return fieldName;
}
Aggregations