public Object parse(CharSequence csq, Cursor cursor) { CharSequence className = cursor.nextToken(csq, CharSet.WHITESPACES); if (className == null) throw new IllegalArgumentException("No class name found"); Class cls = Reflection.getInstance().getClass(className); if (cls != null) return cls; throw new IllegalArgumentException("Class \"" + className + "\" not found (see javolution.lang.Reflection)"); } };
/** * <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; }
private static int availableProcessors() { Reflection.Method availableProcessors = Reflection.getInstance().getMethod("java.lang.Runtime.availableProcessors()"); if (availableProcessors != null) { Integer processors = (Integer) availableProcessors.invoke(Runtime.getRuntime()); return processors.intValue(); } else // J2ME. return 1; }
/** * Sets explicitely the factory to be used for the specified class * (see {@link #getInstance}). * * @param factory the factory to use. * @param forClass the associated class. * @see #getInstance(Class) */ public static <T> void setInstance(ObjectFactory <T> factory, Class <T> forClass) { Reflection.getInstance().setField(factory, forClass, ObjectFactory.class); }
/** * Equivalent to {@link #getClass(CharSequence)} (for J2ME compatibility). */ public Class getClass(String name) { Object obj = name; if (obj instanceof CharSequence) return getClass((CharSequence) obj); // String not a CharSequence on J2ME TextBuilder tmp = TextBuilder.newInstance(); try { tmp.append(name); return getClass(tmp); } finally { TextBuilder.recycle(tmp); } }
/** * Defines the default XML format bound to the specified class. * If the specified class is <code>null</code> then the format is unbound * (unbound formats are used by custom {@link XMLBinding binding} instances). * The static binding is unique and can only be overriden by custom * {@link XMLBinding}. For example:[code] * // Overrides default binding for java.util.Collection. * class MyBinding extends XMLBinding { * XMLFormat<Collection> collectionXML = new XMLFormat<Collection>(null) { ... }; // Unbound. * public XMLFormat getFormat(Class cls) { * if (Collection.isAssignableFrom(cls)) { * return collectionXML; // Overrides default XML format. * } else { * return super.getFormat(cls); * } * } * }[/code] * * @param forClass the root class/interface to associate to this XML format * or <code>null</code> if this format is not bound. * @throws IllegalArgumentException if a XMLFormat is already bound to * the specified class. */ protected XMLFormat(Class <T> forClass) { _class = forClass; if (forClass == null) return; // Dynamic format. Reflection.getInstance().setField(this, forClass, XMLFormat.class); }
/** * Initializes the class with the specified name. * * @param className the name of the class to initialize. */ public static void initialize(String className) { try { Class cls = Reflection.getInstance().getClass(className); if (cls == null) { LogContext.warning("Class + " + className + " not found"); } } catch (Throwable error) { LogContext.error(error); } }
/** * <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(); }
/** * Defines the static format bound to the specified class. * * @param forClass the class to which the format is bound or <code>null</code> * if the format is not bound to any class. * @throws IllegalArgumentException if the specified class is already * bound to another format. */ protected TextFormat(Class <T> forClass) { if (forClass == null) return; // Dynamic format. Reflection.getInstance().setField(new LocalReference(this), forClass, LocalReference.class); }
String arrayName = structs.getClass().getName(); String structName = arrayName.substring(2, arrayName.length() - 1); structClass = Reflection.getInstance().getClass(structName); if (structClass == null) { throw new IllegalArgumentException("Struct class: " + structName + " not found");
/** * 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); }
String className = name.substring(0, sep); String fieldName = name.substring(sep + 1); Class cls = Reflection.getInstance().getClass(className); if (cls == null) { LogContext.warning("Class " + className + " not found");
/** * 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); }
cls = Reflection.getInstance().getClass(classQName.getLocalName()); if (cls == null) throw new XMLStreamException("Class " + classQName.getLocalName() +
LocalReference localReference = (LocalReference) Reflection.getInstance().getField(forClass, LocalReference.class, true); return (localReference == null) ? Predefined.OBJECT_FORMAT : (TextFormat <T> ) localReference.getDefault();