Author: tchemit Date: 2010-03-12 17:09:15 +0100 (Fri, 12 Mar 2010) New Revision: 1776 Log: add usefull method JAXXUtil.checkJAXXContextEntries to test more than one context entries Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java 2010-03-12 15:32:43 UTC (rev 1775) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java 2010-03-12 16:09:15 UTC (rev 1776) @@ -37,14 +37,13 @@ public class JAXXUtil { - /** - * Logger - */ + /** Logger */ static private final Log log = LogFactory.getLog(JAXXUtil.class); public static final String PARENT = "parent"; // Maps root objects to lists of event listeners + private static Map<Object, WeakReference<List<EventListenerDescriptor>>> eventListeners = new WeakHashMap<Object, WeakReference<List<EventListenerDescriptor>>>(); @@ -56,15 +55,20 @@ private static class EventListenerDescriptor { Class<?> listenerClass; + String listenerMethodName; + String methodName; + Object eventListener; } /** - * Decodes the serialized representation of a JAXXObjectDescriptor. The string must be a byte-to-character mapping - * of the binary serialization data for a JAXXObjectDescriptor. See the comments in JAXXCompiler.createJAXXObjectDescriptorField - * for the rationale behind this (admittedly ugly) approach. + * Decodes the serialized representation of a JAXXObjectDescriptor. The + * string must be a byte-to-character mapping of the binary serialization + * data for a JAXXObjectDescriptor. See the comments in + * JAXXCompiler.createJAXXObjectDescriptorField for the rationale behind + * this (admittedly ugly) approach. * * @param descriptor descriptor to decode * @return the dedoced descriptor @@ -172,11 +176,11 @@ } else { for (EventListenerDescriptor descriptor : descriptors) { if (descriptor.listenerClass.equals(listenerClass) && - (listenerMethodName == null ? - descriptor.listenerMethodName == null : - listenerMethodName.equals( - descriptor.listenerMethodName)) && - methodName.equals(descriptor.methodName)) { + (listenerMethodName == null ? + descriptor.listenerMethodName == null : + listenerMethodName.equals( + descriptor.listenerMethodName)) && + methodName.equals(descriptor.methodName)) { return (E) descriptor.eventListener; } } @@ -211,41 +215,41 @@ methodContainerClass.getMethod(methodName, parameterTypes); descriptor.eventListener = Proxy.newProxyInstance(listenerClass.getClassLoader(), - new Class<?>[]{listenerClass}, - new InvocationHandler() { + new Class<?>[]{listenerClass}, + new InvocationHandler() { - @Override - public Object invoke(Object proxy, - Method method, - Object[] args) { - String methodName = method.getName(); - if ((listenerMethodName == null && - listenerMethods.contains(method)) || - methodName.equals(listenerMethodName)) { - try { - targetMethod.setAccessible(true); - return targetMethod.invoke( - methodContainer, args); - } catch (IllegalAccessException e) { - throw new RuntimeException( - "could not invoke on container " + - methodContainer, e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } - } - if (methodName.equals("toString")) { - return toString(); - } - if (methodName.equals("equals")) { - return descriptor.eventListener == args[0]; - } - if (methodName.equals("hashCode")) { - return hashCode(); - } - return null; - } - }); + @Override + public Object invoke(Object proxy, + Method method, + Object[] args) { + String methodName = method.getName(); + if ((listenerMethodName == null && + listenerMethods.contains(method)) || + methodName.equals(listenerMethodName)) { + try { + targetMethod.setAccessible(true); + return targetMethod.invoke( + methodContainer, args); + } catch (IllegalAccessException e) { + throw new RuntimeException( + "could not invoke on container " + + methodContainer, e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + if (methodName.equals("toString")) { + return toString(); + } + if (methodName.equals("equals")) { + return descriptor.eventListener == args[0]; + } + if (methodName.equals("hashCode")) { + return hashCode(); + } + return null; + } + }); descriptors.add(descriptor); return (E) descriptor.eventListener; } catch (NoSuchMethodException e) { @@ -361,8 +365,8 @@ * Return empty string if given object is null * * @param value the value to write - * @return the string representation of the given object or an empty - * string if object is null. + * @return the string representation of the given object or an empty string + * if object is null. */ public static String getStringValue(Object value) { String result; @@ -371,6 +375,30 @@ } /** + * Test if some entries exists in a given context and throw an + * IllegalArgumentException if not. + * <p/> + * + * @param context the context to test + * @param defs the definitions of entries to seek in context + * @throws IllegalArgumentException if the entry is not found in context. + */ + public static void checkJAXXContextEntries(JAXXContext context, + JAXXContextEntryDef<?>... defs) + throws IllegalArgumentException { + + for (JAXXContextEntryDef<?> def : defs) { + Object value = def.getContextValue(context); + + if (value == null) { + throw new IllegalArgumentException( + "the context entry [" + def + "] ] was not found in " + + "context " + context); + } + } + } + + /** * Test if a type of entry exists in a given context and throw an * IllegalArgumentException if not found. * <p/> @@ -408,6 +436,7 @@ src.applyDataBinding(binding); } } + /** * Convinient method to apply more than one binding on a JAXX ui. * @@ -446,8 +475,8 @@ } /** - * detects all PropertychangedListener added by Jaxx uis - * (should be a {@link DataBindingListener} + * detects all PropertychangedListener added by Jaxx uis (should be a {@link + * DataBindingListener} * * @param propertyNames the array of property names to find * @param listeners the array of listeners to filter