title: Runtime type of java.util.Map key and value
body: <p><strong>Problem:<br/></strong>
  I am trying to find the java type of a map's key and value, but without having to iterate over the map and then using <code>instanceof</code>, and want to know it even if the map is empty.
  <br/>
  The context:
  <br/>
  <code>CKEditorConfig</code> which can be downloaded from here (http://ckeditor.com/download) is implemented using a map. But you cannot set this map using injection because setter method is not provided, and there's no constructor provided to set it using constructor injection. However, there are five addConfigValue method (see below), which I can use to add those values.
  <br/><br/></p>
  
  <pre><code>public void addConfigValue(final String key, final Number value);
  public void addConfigValue(final String key, final String value);
  public void addConfigValue(final String key, final Boolean value);
  public void addConfigValue(final String key, final Map&lt;String, ? extends Object&gt; value);
  public void addConfigValue(final String key, final List&lt;? extends Object&gt; value);
  </code></pre>
  
  <p><br/><br/>
  I will be using ckeditor in a couple of places in my j2ee/java web app, and so I figured I should create a factory class that returns the settings needed to initialize the editor by some name. But I wanted to externalize these config and set them in the spring context file. so since I cannot create the config object I have to use regular maps, and then build the config object using the map. but I need to validate those maps coming from spring before building the object.
  <br/><br/>
  This is my method for building the config object:</p>
  
  <pre><code>    @SuppressWarnings("unchecked")
      public CKEditorConfig buildConfigFromMap(Map&lt;String, Object&gt; configMap) {
          CKEditorConfig config = new CKEditorConfig();
          for (String key : configMap.keySet()) {
              Object val = configMap.get(key);
              if (val instanceof Number)
                  config.addConfigValue(key, (Number) val);
              else if (val instanceof String)
                  config.addConfigValue(key, (String) val);
              else if (val instanceof Boolean)
                  config.addConfigValue(key, (Boolean) val);
              else if (val instanceof List) {
                  config.addConfigValue(key, (List&lt;Object&gt;) val);
              } else if (val instanceof Map) {
                  // TODO
              }
              throw new IllegalArgumentException("invalid map value types");
          }
              return config;
      }
  </code></pre>
  
  <p>And as you can see the if the object is a map, I need to be able to validate if its key is a String. I know I can do something like this:</p>
  
  <pre><code>} else if (val instanceof Map) {
      for (Object mapKey : ((Map&lt;Object, Object&gt;) val).keySet()) {
      if (!(mapKey instanceof String))
          throw new IllegalArgumentException("invalid map value types");
      }
  config.addConfigValue(key, (Map&lt;String, Object&gt;) val);
  }
  </code></pre>
  
  <p>but the whole exercise makes me think I am not doing the right thing? any suggestions? I am open to using a totally different approach. I appreciate your input.</p>