use of com.google.protobuf.Descriptors.EnumValueDescriptor in project BIMserver by opensourceBIM.
the class ProtocolBuffersConverter method convertFieldValue.
private Object convertFieldValue(SField field, Object val) throws ConvertException {
if (val instanceof EnumValueDescriptor) {
EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) val;
Class<?> enumClass;
try {
enumClass = Class.forName("org.bimserver.interfaces.objects." + enumValueDescriptor.getType().getName());
for (Object v : enumClass.getEnumConstants()) {
Enum<?> e = (Enum<?>) v;
if (e.ordinal() == enumValueDescriptor.getNumber()) {
val = e;
break;
}
}
} catch (ClassNotFoundException e) {
LOGGER.error("", e);
}
return val;
} else if (field.getType().getInstanceClass() == Date.class) {
return new Date((Long) val);
} else if (field.getType().getInstanceClass() == DataHandler.class) {
ByteString byteString = (ByteString) val;
ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource("test", byteString.toByteArray());
return new DataHandler(byteArrayDataSource);
} else if (val instanceof DynamicMessage) {
return convertProtocolBuffersMessageToSObject((DynamicMessage) val, null, field.isAggregate() ? field.getGenericType() : field.getType());
} else {
return val;
}
}
use of com.google.protobuf.Descriptors.EnumValueDescriptor in project elephant-bird by twitter.
the class Protobufs method readSingleFieldNoTag.
private static Object readSingleFieldNoTag(CodedInputStream input, FieldDescriptor fd, Builder enclosingBuilder) throws IOException {
switch(fd.getType()) {
case DOUBLE:
return input.readDouble();
case FLOAT:
return input.readFloat();
case INT64:
case UINT64:
return input.readInt64();
case INT32:
return input.readInt32();
case FIXED64:
return input.readFixed64();
case FIXED32:
return input.readFixed32();
case BOOL:
return input.readBool();
case STRING:
return input.readString();
case GROUP:
case MESSAGE:
Builder fieldBuilder = enclosingBuilder.newBuilderForField(fd);
input.readMessage(fieldBuilder, null);
return fieldBuilder.build();
case BYTES:
return input.readBytes();
case UINT32:
return input.readUInt32();
case ENUM:
EnumValueDescriptor eVal = fd.getEnumType().findValueByNumber(input.readEnum());
// unknown fields. but we don't have access to that here. return default
return eVal != null ? eVal : fd.getDefaultValue();
case SFIXED32:
return input.readSFixed32();
case SFIXED64:
return input.readSFixed64();
case SINT32:
return input.readSInt32();
case SINT64:
return input.readSInt64();
default:
throw new IllegalArgumentException("Unknown type " + fd.getType() + " for " + fd.getFullName());
}
}
use of com.google.protobuf.Descriptors.EnumValueDescriptor in project j2objc by google.
the class CompatibilityTest method checkGetters.
private void checkGetters(TypicalDataOrBuilder data, FieldDescriptor[] fields) {
assertEquals(42, data.getMyInt());
assertEquals("foo", new String(data.getMyBytes().toByteArray()));
assertEquals(TypicalData.EnumType.VALUE9, data.getMyEnumType());
assertTrue(data.getMyBool());
assertEquals(new Float(43.8), data.getMyFloat());
assertEquals(new Double(44.5), data.getMyDouble());
assertEquals("bar", data.getMyString());
assertEquals(24, data.getMyUint());
assertEquals(4422, data.getMyLong());
assertEquals(2244, data.getMyUlong());
assertEquals(1, data.getRepeatedInt32Count());
assertEquals(72, data.getRepeatedInt32(0));
assertEquals("aaa", data.getRepeatedString(0));
assertEquals("ccc", data.getRepeatedString(1));
assertEquals(1.2, data.getRepeatedDouble(0), 0.0001);
Object result;
result = data.getField(fields[1]);
assertTrue(result instanceof Integer);
assertEquals(42, result);
result = data.getField(fields[2]);
assertTrue(result instanceof ByteString);
assertEquals("foo", new String(((ByteString) result).toByteArray()));
result = data.getField(fields[3]);
assertTrue(result instanceof EnumValueDescriptor);
assertEquals(9, ((EnumValueDescriptor) result).getNumber());
result = data.getField(fields[11]);
assertTrue(result instanceof TypicalDataMessage);
assertEquals(TypicalDataMessage.newBuilder().build(), result);
result = data.getField(fields[12]);
assertTrue(result instanceof Boolean);
assertEquals(Boolean.TRUE, result);
result = data.getField(fields[13]);
assertTrue(result instanceof Float);
assertEquals(43.8, ((Float) result).floatValue(), 0.0001);
result = data.getField(fields[14]);
assertTrue(result instanceof Double);
assertEquals(44.5, ((Double) result).doubleValue(), 0.0001);
result = data.getField(fields[15]);
assertTrue(result instanceof String);
assertEquals("bar", result);
result = data.getField(fields[16]);
assertTrue(result instanceof Integer);
assertEquals(24, result);
result = data.getField(fields[17]);
assertTrue(result instanceof Long);
assertEquals(4422L, result);
result = data.getField(fields[18]);
assertTrue(result instanceof Long);
assertEquals(2244L, result);
assertEquals(1, data.getRepeatedFieldCount(fields[4]));
result = data.getRepeatedField(fields[4], 0);
assertTrue(result instanceof Integer);
assertEquals(72, result);
assertEquals(2, data.getRepeatedFieldCount(fields[8]));
result = data.getRepeatedField(fields[8], 1);
assertEquals("ccc", result);
assertEquals(2, data.getRepeatedFieldCount(fields[7]));
result = data.getRepeatedField(fields[7], 1);
assertEquals(3.4, ((Double) result).doubleValue(), 0.0001);
}
use of com.google.protobuf.Descriptors.EnumValueDescriptor in project BIMserver by opensourceBIM.
the class ProtocolBuffersConverter method convertSObjectToProtocolBuffersObject.
@SuppressWarnings({ "unchecked", "rawtypes" })
public Message convertSObjectToProtocolBuffersObject(SBase object, SClass definedType) throws ConvertException {
Builder builder = null;
MessageDescriptorContainer messageDescriptor = protocolBuffersMetaData.getMessageDescriptor(definedType.getSimpleName());
if (messageDescriptor == null) {
throw new ConvertException("No message descriptor found with name " + definedType.getSimpleName());
}
Descriptor definedDescriptor = messageDescriptor.getDescriptor();
try {
builder = DynamicMessage.getDefaultInstance(definedDescriptor).newBuilderForType();
} catch (SecurityException e) {
throw new ConvertException(e);
} catch (IllegalArgumentException e) {
throw new ConvertException(e);
}
SClass sClass = object.getSClass();
SClass superClass = sClass;
while (superClass.getSuperClass() != null && superClass.getSuperClass().getInstanceClass() != Object.class) {
superClass = superClass.getSuperClass();
}
if (sClass != superClass && messageDescriptor.getField("__actual_type") != null) {
builder.setField(messageDescriptor.getField("__actual_type"), sClass.getSimpleName());
builder.setField(messageDescriptor.getField("__" + sClass.getSimpleName()), convertSObjectToProtocolBuffersObject(object, sClass));
}
for (SField field : definedType.getAllFields()) {
try {
Object value = object.sGet(field);
FieldDescriptor fieldDescriptor = messageDescriptor.getField(field.getName());
if (value != null) {
if (value.getClass().isPrimitive() || value.getClass() == String.class || value.getClass() == Long.class || value.getClass() == Double.class || value.getClass() == Integer.class || value.getClass() == Boolean.class) {
builder.setField(fieldDescriptor, value);
} else if (value.getClass().isEnum()) {
Enum eNum = (Enum) value;
int ordinal = eNum.ordinal();
EnumValueDescriptor findValueByNumber = fieldDescriptor.getEnumType().findValueByNumber(ordinal);
builder.setField(fieldDescriptor, findValueByNumber);
} else if (value.getClass() == Date.class) {
builder.setField(fieldDescriptor, ((Date) value).getTime());
} else if (value.getClass() == DataHandler.class) {
DataHandler dataHandler = (DataHandler) value;
InputStream inputStream = dataHandler.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, byteArrayOutputStream);
ByteString byteString = ByteString.copyFrom(byteArrayOutputStream.toByteArray());
builder.setField(fieldDescriptor, byteString);
} else if (List.class.isAssignableFrom(value.getClass())) {
List list = (List) value;
List newList = new ArrayList();
for (Object o : list) {
if (fieldDescriptor.getJavaType() == JavaType.MESSAGE) {
newList.add(convertSObjectToProtocolBuffersObject((SBase) o, field.getGenericType()));
} else {
newList.add(o);
}
}
builder.setField(fieldDescriptor, newList);
} else {
LOGGER.error("Unimplemented: " + fieldDescriptor.getName() + ": " + value);
}
}
} catch (SecurityException e) {
LOGGER.error("", e);
} catch (IllegalArgumentException e) {
LOGGER.error("", e);
} catch (IOException e) {
LOGGER.error("", e);
}
}
return builder.build();
}
use of com.google.protobuf.Descriptors.EnumValueDescriptor in project BIMserver by opensourceBIM.
the class ReflectiveRpcChannel method callBlockingMethod.
@SuppressWarnings({ "unchecked", "rawtypes" })
public Message callBlockingMethod(MethodDescriptorContainer methodDescriptor, Message request) {
FieldDescriptor errorMessageField = methodDescriptor.getOutputField("errorMessage");
DynamicMessage response = DynamicMessage.getDefaultInstance(methodDescriptor.getOutputDescriptor());
Descriptor inputType = methodDescriptor.getInputDescriptor();
SMethod sMethod = servicesMap.getBySimpleName(methodDescriptor.getServiceDescriptorContainer().getName()).getSMethod(methodDescriptor.getName());
if (sMethod == null) {
LOGGER.info("Method " + methodDescriptor.getName() + " not found");
return null;
} else {
try {
KeyValuePair[] arguments = new KeyValuePair[inputType.getFields().size()];
int i = 0;
for (FieldDescriptor fieldDescriptor : inputType.getFields()) {
SParameter sParameter = sMethod.getParameter(i);
Object value = request.getField(fieldDescriptor);
if (value instanceof EnumValueDescriptor) {
EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) value;
Class en = getJavaType(fieldDescriptor);
arguments[i] = new KeyValuePair(fieldDescriptor.getName(), en.getEnumConstants()[enumValueDescriptor.getIndex()]);
} else if (value instanceof ByteString) {
ByteString byteString = (ByteString) value;
DataSource dataSource = new org.bimserver.utils.ByteArrayDataSource("bytes", byteString.toByteArray());
DataHandler dataHandler = new DataHandler(dataSource);
arguments[i] = new KeyValuePair(fieldDescriptor.getName(), dataHandler);
} else if (value instanceof DynamicMessage) {
arguments[i] = new KeyValuePair(fieldDescriptor.getName(), convertProtocolBuffersMessageToSObject((DynamicMessage) value, null, sParameter.getType()));
} else if (value instanceof Collection) {
Collection col = (Collection) value;
if (sParameter.getType().isList()) {
List list = new ArrayList(col);
arguments[i] = new KeyValuePair(fieldDescriptor.getName(), list);
} else if (sParameter.getType().isSet()) {
Set set = new HashSet(col);
arguments[i] = new KeyValuePair(fieldDescriptor.getName(), set);
}
} else {
arguments[i] = new KeyValuePair(fieldDescriptor.getName(), value);
}
i++;
}
Object result = sMethod.invoke(publicInterface, service, arguments);
Builder builder = response.newBuilderForType();
if (methodDescriptor.getOutputDescriptor().getName().equals("VoidResponse")) {
builder.setField(errorMessageField, "OKE");
} else {
FieldDescriptor valueField = protocolBuffersMetaData.getMessageDescriptor(response.getDescriptorForType().getName()).getField("value");
if (result != null) {
if (valueField.getType().getJavaType() == JavaType.ENUM) {
builder.setField(valueField, valueField.getEnumType().findValueByName(result.toString()));
} else if (valueField.getType().getJavaType() != JavaType.MESSAGE) {
if (result instanceof Date) {
builder.setField(valueField, ((Date) result).getTime());
} else {
if (valueField.isRepeated()) {
builder.setField(valueField, new ArrayList());
} else {
builder.setField(valueField, result);
}
}
} else if (result instanceof List) {
List list = new ArrayList();
List originalList = (List) result;
for (Object object : originalList) {
list.add(convertSObjectToProtocolBuffersObject((SBase) object, sMethod.getBestReturnType()));
}
builder.setField(valueField, list);
} else if (result instanceof Set) {
List list = new ArrayList();
Set originalSet = (Set) result;
for (Object object : originalSet) {
list.add(convertSObjectToProtocolBuffersObject((SBase) object, sMethod.getBestReturnType()));
}
builder.setField(valueField, list);
} else {
builder.setField(valueField, convertSObjectToProtocolBuffersObject((SBase) result, sMethod.getBestReturnType()));
}
}
builder.setField(errorMessageField, "OKE");
}
return builder.build();
// } catch (InvocationTargetException e) {
// Builder errorMessage = response.newBuilderForType();
// if (e.getTargetException() != null && e.getTargetException().getMessage() != null) {
// errorMessage.setField(errorMessageField, e.getTargetException().getMessage());
// } else {
// if (e.getMessage() != null) {
// errorMessage.setField(errorMessageField, e.getMessage());
// } else {
// errorMessage.setField(errorMessageField, "Unknown error");
// }
// }
// return errorMessage.build();
} catch (Exception e) {
if (!(e instanceof org.bimserver.shared.exceptions.ServiceException)) {
LOGGER.error("", e);
}
Builder errorMessage = response.newBuilderForType();
if (e.getMessage() != null) {
errorMessage.setField(errorMessageField, e.getMessage());
} else {
LOGGER.error("", e);
errorMessage.setField(errorMessageField, "Unknown error");
}
return errorMessage.build();
}
}
}
Aggregations