title: java.lang.InstantiationError when creating instance of static inner class with Objenesis
body: <p>I am trying to create a utility method that should be able to deep-clone any object.
  (<code>Object.clone()</code> only works on Object implementing <code>Cloneable</code> and I heard it's flawed anyways.)</p>
  
  <p>I am using <a href="http://objenesis.org/" rel="nofollow noreferrer">Objenesis</a> to create new instances of objects without the use of constructors.</p>
  
  <p>However, when trying to clone a JFrame I get the following Exception:
  <br/>(using this class because I think it should be a good and complex test)</p>
  
  <pre><code>java.lang.InstantiationError: [Ljava.util.concurrent.ConcurrentHashMap$Node;
      at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
      at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
  </code></pre>
  
  <p>I am open to any solution, not necessarily limited to Objenesis.</p>
  
  <p>My Code:</p>
  
  <pre><code>private static ObjenesisStd OBJENESIS = new ObjenesisStd();
  
  @SuppressWarnings("unchecked")
  public static &lt;T&gt; T clone(T object, boolean deep){
      if(object == null){
          return null;
      }else{
          try {
              T clone = (T) OBJENESIS.newInstance(object.getClass());
              List&lt;Field&gt; fields = ReflectionUtil.getAllFieldsInHierarchy(object.getClass());
              for(Field field : fields){
                  boolean isAccessible = field.isAccessible();
                  boolean isFinal = ReflectionUtil.isFinal(field);
                  field.setAccessible(true);
                  ReflectionUtil.setFinal(field, false);
                  Class&lt;?&gt; type = field.getType();
                  if(!deep || type.isPrimitive() || type == String.class){
                      field.set(clone, field.get(object));
                  }else{
                      field.set(clone, clone(field.get(object), true));
                  }
                  field.setAccessible(isAccessible);
                  ReflectionUtil.setFinal(field, isFinal);
              }
              return clone;
          } catch (Throwable e) {
              e.printStackTrace();
              //throw new RuntimeException("Failed to clone object of type " + object.getClass(), e);
              return null;
          }
      }
  }
  
  
  public static void main(String[] args) {
      GetterSetterAccess access = new GetterSetterAccess(JFrame.class);
      JFrame frame = new JFrame("Test Frame");
      for(String attr : access.getAttributes()){
          System.out.println(attr + " " + access.getValue(frame, attr));
      }
  
      System.out.println("----------------------------------------------");
      frame = clone(frame, true);
  
  
      for(String attr : access.getAttributes()){
          System.out.println(attr + " " + access.getValue(frame, attr));
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> Got it to work with the accepted answer and a few more fixes:</p>
  
  <ul>
  <li>Avoided cloning Wrappers of Primitive Types (<code>Integer.class</code> etc.)</li>
  <li>Avoided cloning Classes (Objects of the class <code>Class.class</code>)</li>
  <li>Stored the cloned objects in a Map and reused them, so if Object A has a reference to Object B and Object B one to Object A it doesn't get stuck in an infinite loop. I also used a Map that checks for exact equality (<code>==</code>) instead of using <code>equals()</code>.</li>
  <li>Created a custom exception class which would just be passed on instead of throwing a new exception on every level (causing a huge caused-by-depth).</li>
  </ul>