use of com.google.protobuf.Descriptors.Descriptor 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.Descriptor in project j2objc by google.
the class CompatibilityTest method testGetUnsetField.
public void testGetUnsetField() throws Exception {
TypicalData data = TypicalData.newBuilder().build();
Descriptor descriptor = TypicalData.getDescriptor();
assertEquals(0, data.getField(descriptor.findFieldByNumber(1)));
Object result = data.getField(descriptor.findFieldByNumber(3));
assertTrue(result instanceof EnumValueDescriptor);
assertEquals(TypicalData.EnumType.VALUE1.getValueDescriptor().getNumber(), ((EnumValueDescriptor) result).getNumber());
assertTrue(data.getField(descriptor.findFieldByNumber(11)) instanceof TypicalDataMessage);
}
use of com.google.protobuf.Descriptors.Descriptor 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.Descriptor in project core-java by SpineEventEngine.
the class KnownTypesShould method provide_proto_descriptor_by_type_name.
@Test
public void provide_proto_descriptor_by_type_name() {
final String typeName = "spine.test.types.Task";
final Descriptor typeDescriptor = (Descriptor) getDescriptor(typeName);
assertNotNull(typeDescriptor);
assertEquals(typeName, typeDescriptor.getFullName());
}
use of com.google.protobuf.Descriptors.Descriptor in project core-java by SpineEventEngine.
the class Sample method builderForType.
/**
* Generates a new stub {@link Message.Builder} with all the fields set to
* {@link Random random} values.
*
* <p> All the fields are guaranteed to be not {@code null} and not default.
* Number and {@code boolean} fields may or may not have their default values ({@code 0} and
* {@code false}).
*
* @param clazz Java class of the stub message
* @param <M> type of the required message
* @param <B> type of the {@link Message.Builder} for the message
* @return new instance of the {@link Message.Builder} for given type
* @see #valueFor(FieldDescriptor)
*/
public static <M extends Message, B extends Message.Builder> B builderForType(Class<M> clazz) {
checkClass(clazz);
final B builder = builderFor(clazz);
final Descriptor builderDescriptor = builder.getDescriptorForType();
final Collection<FieldDescriptor> fields = builderDescriptor.getFields();
for (FieldDescriptor field : fields) {
final Object value = valueFor(field);
if (field.isRepeated()) {
builder.addRepeatedField(field, value);
} else {
builder.setField(field, value);
}
}
return builder;
}
Aggregations