// obj: "object with properties" to write private void writeObject(Object obj) throws NoSuchMethodException, InstantiationException, InvocationTargetException, IllegalAccessException, SecurityException { // begin bean encoding: this.driver.startElement(DTD.ELEMENT_OBJECT); Class cls = obj.getClass(); final String mappedName = this.context.aliasFor(cls); this.driver.setAttribute( DTD.ATTRIBUTE_CLASS, (mappedName != null ? mappedName : cls.getName())); // encode properties: while (cls != Object.class) { // process inheritance: for (Field f : cls.getDeclaredFields()) { // process composition: if (Modifier.isStatic(f.getModifiers()) || !ReflectionUtil.hasClassFieldProperty(cls, f) || this.context.excluded(f)) { continue; // skip static or non-property or excluded field. } // get property field: if (!f.isAccessible()) { f.setAccessible(true); } // write property value: final String aliasedFieldName = this.context.aliasFor(f, f.getName()); this.driver.startElement(aliasedFieldName); this.write0(f.get(obj)); this.driver.endElement(); } cls = cls.getSuperclass(); } // end bean encoding: this.driver.endElement(); }
@Override public <T> T defaultInstanceFor(Class<T> c) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { final Object cached = cachedDefCtors.get(c); if (cached != null) { if (cached.getClass() == Constructor.class) { return ((Constructor<T>) cached).newInstance(); } if (cached.getClass() == NoSuchMethodException.class) { throw (NoSuchMethodException) cached; } if (cached.getClass() == InstantiationException.class) { throw (InstantiationException) cached; } if (cached.getClass() == InvocationTargetException.class) { throw (InvocationTargetException) cached; } if (cached.getClass() == IllegalAccessException.class) { throw (IllegalAccessException) cached; } } try { final Constructor<T> ctor = ReflectionUtil.defaultConstructor(c); T ret = ctor.newInstance(); cachedDefCtors.putIfAbsent(c, ctor); return ret; } catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException noDefCtorX) { cachedDefCtors.putIfAbsent(c, noDefCtorX); throw noDefCtorX; } }