Reputation: 10902
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
}
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;
Upvotes: 3
Views: 3139
Reputation: 25613
Because of type erasure, information is not known at runtime, so there is no other way of checking it than instanceof
, getClass().getName()
or getDeclaredField("...").getGenericType().getActualTypeArguments()
things.
In my opinion you should stay with instanceof
as it is easily readable and other developer will easily understand what you wanted to do.
PS: I liked the exceptions ;-)
Upvotes: 1