Short, at-a-glance summary of question (TLDR): how do I ensure a Java Object is of type Map<String, Object>?
I have a YAML document as a String. I create the YAML using SnakeYAML from a Map<String, Object> but it could be tampered with before I read it back again. I don't want to resort to PKI signing, it's enough to see whether the YAML representation is still a Map<String, Object>, even if the Object values may be all over the place. I care about type, not content.
So: how do I ensure an Object is indeed of type Map<String, Object>?
Here's how I am trying to circumvent the various SuppressWarnings of type erasure:
Object yamlObject = yaml.load(someYamlDocumentAsString);
// I only want to proceed if this YAML object is a Map<String, Object>
if ((yamlObject instanceof Map<?, ?>)) {
Map<?, ?> propertyMap = (Map<?, ?>) yamlObject;
for (Entry<?, ?> propertyEntry : propertyMap.entrySet()) {
// I am checking each key and throwing if it's not a "String"
Object propertyKey = propertyEntry.getKey();
if (!(propertyKey instanceof CharSequence)) {
throw new BananaException("FIXME"); // FIXME
}
}
} else {
throw new CoconutException("FIXME"); // FIXME
}
Can the above be done better?
Incidentally, I don't suppose Java has something akin to C#'s as keyword (from MSDN: The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception.)?
After the code above, can I safely suppress warnings for this:
@SuppressWarnings("unchecked")
Map<String, Object> aBeautifulAndCozyMap = (Map<String, Object>) propertyMap;
CoconutException, although it sounds almost as painful to catch as the dreadedPineappleException.