3

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;
3
  • 3
    I don't believe I've ever heard of a better exception than CoconutException, although it sounds almost as painful to catch as the dreaded PineappleException. Commented Sep 11, 2012 at 17:45
  • 3
    Not the worst of spiky fruit. Imaging catching a huge litchi! Commented Sep 11, 2012 at 17:48
  • A well thrown PineconeException (en.wikipedia.org/wiki/Conifer_cone) can leave a welt. Commented Sep 11, 2012 at 19:03

1 Answer 1

1

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 ;-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.