Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate an instance of a class and initialize all its fields
    text
    copied!<p>I want to create an instance of a class, but I also need to initialize also all its fields recursively.</p> <p>The code you see related do the objectFactory is because some of this classes could be JAXB classes, so for every package there is an ObjectFactory with methods like createJaxbObject(....).</p> <p>EDITED:</p> <p>My final solutions is this one:</p> <pre><code>public Object getInstance(Class&lt;?&gt; instanceClass, Boolean simple, String jaxbName) { Object instance = null; try { if (instanceClass.isPrimitive()) return primitiveValues.get(instanceClass.getName()); if (List.class.isAssignableFrom(instanceClass)) return new ArrayList(); else if (instanceClass.isEnum()) return instanceClass.getEnumConstants()[0]; else if (instanceClass.isArray()) return java.lang.reflect.Array.newInstance(instanceClass, 1); else if (BigInteger.class.isAssignableFrom(instanceClass)) return new BigInteger("0"); else if (instanceClass.equals(String.class)) return ""; else if (instanceClass.equals(Boolean.class)) return false; else if (instanceClass.equals(EntityObjectStringType.class)) return new EntityObjectStringType(); else if (JAXBElement.class.isAssignableFrom(instanceClass)) { try { Method m = null; Class&lt;?&gt; objFactoryClass = null; Iterator&lt;String&gt; it = EditorServlet.objectFactories .iterator(); Object of = null; while (it.hasNext()) { objFactoryClass = Class.forName(it.next()); of = objFactoryClass.getConstructor().newInstance(); m = getMethodFromObjectFactory(objFactoryClass, jaxbName); if (m != null) if (m.getParameterTypes().length &gt; 0) break; } Object jaxbElement = getInstance(m.getParameterTypes()[0], m.getParameterTypes()[0].getSimpleName()); return m.invoke(of, jaxbElement); } catch (NoSuchMethodException e) { logger.error("JAXB NoSuchMethodException"); } } else try { logger.info("Costruttori per " + instanceClass.getName() + " " + instanceClass.getConstructors().length); instance = instanceClass.getConstructor().newInstance(); } catch (NoSuchMethodException noSuchMethodException) { logger.error("getConstructors NoSuchMethodException"); } } catch (IllegalArgumentException e) { logger.error("IllegalArgumentException " + instanceClass.getName()); } catch (SecurityException e) { logger.error("SecurityException " + instanceClass.getName()); } catch (InstantiationException e) { logger.error("InstantiationException " + instanceClass.getName() + " " + instanceClass.isPrimitive()); } catch (IllegalAccessException e) { logger.error("IllegalAccessException " + instanceClass.getName()); } catch (InvocationTargetException e) { logger.error("InvocationTargetException " + instanceClass.getName()); } catch (ClassNotFoundException e) { logger.error("ClassNotFoundException " + instanceClass.getName()); } if (!simple) { for (Field field : instanceClass.getDeclaredFields()) { try { Object fieldInstance = getInstance(field.getType(), field.getName()); field.setAccessible(true); field.set(instance, fieldInstance); } catch (IllegalArgumentException e) { logger.error("IllegalArgumentException " + instanceClass.getName()); } catch (IllegalAccessException e) { logger.error("IllegalAccessException " + instanceClass.getName()); } } } return instance; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload