use of net.morimekta.providence.PEnumValue in project providence by morimekta.
the class ConstParser method findEnumValue.
private int findEnumValue(String identifier, Token token, ThriftTokenizer tokenizer, String expectedType) throws IOException {
String[] parts = identifier.split("[.]");
String typeName;
String valueName;
if (parts.length == 3) {
typeName = parts[0] + "." + parts[1];
valueName = parts[2];
} else if (parts.length == 2) {
typeName = parts[0];
valueName = parts[1];
} else {
throw tokenizer.failure(token, identifier + " is not a valid " + expectedType + " value.");
}
try {
@SuppressWarnings("unchecked") PDeclaredDescriptor descriptor = registry.getDeclaredType(typeName, programContext);
if (descriptor instanceof PEnumDescriptor) {
PEnumDescriptor desc = (PEnumDescriptor) descriptor;
PEnumValue value = desc.findByName(valueName);
if (value != null) {
return value.asInteger();
}
}
throw tokenizer.failure(token, typeName + " is not an enum.");
} catch (IllegalArgumentException e) {
throw tokenizer.failure(token, "No type named " + typeName + ".");
}
}
Aggregations