/** * <p> Returns the default format for the specified class/interface. * If there no direct mapping for the specified class, the mapping * for the specified class interfaces is searched, if none is found * the mapping for the parents classes is searched, if still none is * found the format for <code>java.lang.Object</code> is returned.</p> * * <p> A default xml format exists for the following predefined types: * <code><ul> * <li>java.lang.Object</li> * <li>java.util.Collection</li> * <li>java.util.Map</li> * </ul></code> * The default XML representation (java.lang.Object) consists of the * of a "value" attribute holding its textual representation * (see {@link TextFormat#getInstance}).</p> * * @return the class/interface bound to this format. */ public static <T> XMLFormat <T> getInstance(Class <? extends T> forClass) { XMLFormat objectFormat = XMLBinding.OBJECT_XML; // Also forces initialization or XMLBinding. XMLFormat xmlFormat = (XMLFormat) Reflection.getInstance().getField(forClass, XMLFormat.class, true); return (xmlFormat != null) ? xmlFormat : objectFormat; }
/** * Overrides the default format for the specified class ({@link LocalContext local setting}). * * @param forClass the class for which the format is locally overriden. * @param format the new format (typically unbound). * @throws IllegalArgumentException if the speficied class has not default format defined. */ public static <T> void setInstance(Class <? extends T> forClass, TextFormat <T> format) { Predefined.init(); // Forces initialization. LocalReference localReference = (LocalReference) Reflection.getInstance().getField(forClass, LocalReference.class, false); if (localReference == null) throw new IllegalArgumentException("Cannot override default format for class " + forClass + " (no default format defined)"); localReference.set(format); }
/** * <p> Returns the current format for instances of the specified class.</p> * * @param forClass the class to which a format has been bound. * @return the most specialized format compatible with the specified class. */ public static <T> TextFormat <T> getInstance(Class <? extends T> forClass) { Predefined.init(); // Forces initialization. LocalReference localReference = (LocalReference) Reflection.getInstance().getField(forClass, LocalReference.class, true); return (localReference == null) ? Predefined.OBJECT_FORMAT : (TextFormat <T> ) localReference.get(); }
/** * Returns a factory implementation producing instances of the specified * class. By default this method returns a factory creating new objects * using the class public no-arg constructor (through reflection). * If that constructor is not accessible, the factory instance can be * {@link #setInstance set explicitly}:[code] * class LogContext { * public static final Class<LogContext> NULL = Null.class; * ... * private static class Null extends LogContext ... // Private. * static { * // Allows Null instances to be factory produced (even so the class is not accessible). * ObjectFactory.setInstance(new ObjectFactory<Null> { * protected Null create() { return new Null() }}, * Null.class); * } * }[/code] * * @param forClass the class for which an object factory is returned. * @return an object factory producing instances of the specified class. */ public static <T> ObjectFactory <T> getInstance(Class <T> forClass) { ObjectFactory factory = (ObjectFactory) Reflection.getInstance().getField(forClass, ObjectFactory.class, false); return factory != null ? factory : new Generic(forClass); }
LocalReference localReference = (LocalReference) Reflection.getInstance().getField(forClass, LocalReference.class, true); return (localReference == null) ? Predefined.OBJECT_FORMAT : (TextFormat <T> ) localReference.getDefault();