Index: modules/beans/src/main/java/java/beans/Encoder.java =================================================================== --- modules/beans/src/main/java/java/beans/Encoder.java (revision 420465) +++ modules/beans/src/main/java/java/beans/Encoder.java (working copy) @@ -51,12 +51,16 @@ * @com.intel.drl.spec_ref */ public Object get(Object oldInstance) { - if(oldInstance == null) { + ObjectNode node; + Object result; + + if (oldInstance == null) { return null; } - - ObjectNode node = (ObjectNode) nodes.get(oldInstance); - Object result = getValue(node); + + node = nodes.get(oldInstance); + result = getValue(node); + return result; } @@ -65,11 +69,13 @@ */ public Object remove(Object oldInstance) { //TODO - notify references on node deletion - if(oldInstance == null) { + ObjectNode node; + + if (oldInstance == null) { return null; } - - ObjectNode node = (ObjectNode) nodes.remove(oldInstance); + + node = nodes.remove(oldInstance); return getValue(node); } @@ -79,12 +85,12 @@ public PersistenceDelegate getPersistenceDelegate(Class type) { PersistenceDelegate result = (PersistenceDelegate) persistenceDelegates.get(type); - + if(result == null) { result = DefaultPersistenceDelegatesFactory.getPersistenceDelegate( type); } - + return result; } @@ -110,7 +116,7 @@ new NullPersistenceDelegate(); - if(pd == null) { + if (pd == null) { pd = new DefaultPersistenceDelegate(); } @@ -121,13 +127,15 @@ * @com.intel.drl.spec_ref */ public void writeStatement(Statement oldStm) { - ObjectNode node = (ObjectNode) nodes.get(oldStm.getTarget()); - if(node != null) { + ObjectNode node = nodes.get(oldStm.getTarget()); + + if (node != null) { try { + Statement statement; Object[] oldArgs = oldStm.getArguments(); + write(oldArgs); - - Statement statement = new Statement(node.getObjectValue(), + statement = new Statement(node.getObjectValue(), oldStm.getMethodName(), oldArgs); node.addStatement(statement); } catch (Exception e) { @@ -145,17 +153,17 @@ public void writeExpression(Expression oldExp) { try { Object oldInstance = oldExp.getValue(); - + ObjectNode node = null; Class type = null; - if(oldInstance != null) { + if (oldInstance != null) { type = oldInstance.getClass(); - node = (ObjectNode) nodes.get(oldInstance); + node = nodes.get(oldInstance); } - - if(node == null) { - if(isNull(type) || isPrimitive(type) || isString(type) + + if (node == null) { + if (isNull(type) || isPrimitive(type) || isString(type) //|| isClass(type) ) { @@ -168,14 +176,14 @@ nodes.put(oldInstance, node); // if an expression is not a constructor - if(!(oldExp.getTarget() instanceof Class || + if (!(oldExp.getTarget() instanceof Class || oldExp.getTarget() instanceof Field)) { - ObjectNode parent = (ObjectNode) nodes.get( - oldExp.getTarget()); + ObjectNode parent = nodes.get(oldExp.getTarget()); + parent.addExpression(oldExp); } - } else if(oldExp.getMethodName().equals("new")) { + } else if (oldExp.getMethodName().equals("new")) { node.addReference(); } else { node.addReferencedExpression(oldExp); @@ -197,112 +205,115 @@ * @com.intel.drl.spec_ref */ public ExceptionListener getExceptionListener() { - if(exceptionListener == null) { + if (exceptionListener == null) { exceptionListener = new ExceptionListener() { public void exceptionThrown(Exception e) { System.out.println(e.getClass() + ": " + e.getMessage()); } }; } - + return exceptionListener; } private Object write(Object oldInstance) throws Exception { - if(oldInstance == null) { + ObjectNode node; + + if (oldInstance == null) { return null; } - ObjectNode node = (ObjectNode) nodes.get(oldInstance); - if(node == null) { + node = nodes.get(oldInstance); + if (node == null) { Class type = oldInstance.getClass(); - + doWriteObject(oldInstance); - node = (ObjectNode) nodes.get(oldInstance); + node = nodes.get(oldInstance); } else { node.addReference(); } - + return node.getObjectValue(); } private Object[] write(Object[] oldInstances) throws Exception { - if(oldInstances != null) { + if (oldInstances != null) { Object[] newInstances = new Object[oldInstances.length]; - - for(int i = 0; i < oldInstances.length; ++i) { + + for (int i = 0; i < oldInstances.length; ++i) { newInstances[i] = write(oldInstances[i]); } - + return newInstances; } - + return null; } private Object getValue(ObjectNode node) { - if(node != null) { + if (node != null) { try { Object result = node.getObjectValue(); + return result; } catch (Exception e) { getExceptionListener().exceptionThrown(e); } } - + return null; } - + static boolean isNull(Class type) { return (type == null); } - + static boolean isPrimitive(Class type) { return (type == Boolean.class) || (type == Byte.class) || (type == Character.class) || (type == Double.class) || (type == Float.class) || (type == Integer.class) || (type == Long.class) || (type == Short.class); } - + static boolean isString(Class type) { return (type == String.class); - + } static boolean isClass(Class type) { return (type == Class.class); } - - + + static boolean isArray(Class type) { return type.isArray(); } static String getPrimitiveName(Class type) { String result = null; - - if(type == Boolean.class) { + + if (type == Boolean.class) { result = "boolean"; - } else if(type == Byte.class) { + } else if (type == Byte.class) { result = "byte"; - } else if(type == Character.class) { + } else if (type == Character.class) { result = "char"; - } else if(type == Double.class) { + } else if (type == Double.class) { result = "double"; - } else if(type == Float.class) { + } else if (type == Float.class) { result = "float"; - } else if(type == Integer.class) { + } else if (type == Integer.class) { result = "int"; - } else if(type == Long.class) { + } else if (type == Long.class) { result = "long"; - } else if(type == Short.class) { + } else if (type == Short.class) { result = "short"; - } else if(type == String.class) { + } else if (type == String.class) { result = "string"; - } else if(type == Class.class) { + } else if (type == Class.class) { result = "class"; } - + return result; } } Index: modules/beans/src/main/java/java/beans/EventHandler.java =================================================================== --- modules/beans/src/main/java/java/beans/EventHandler.java (revision 420465) +++ modules/beans/src/main/java/java/beans/EventHandler.java (working copy) @@ -53,22 +53,28 @@ * @com.intel.drl.spec_ref */ public Object invoke(Object proxy, Method method, Object[] arguments) { + Class proxyClass; + // workaround if arguments are null - treat them as array with // single null element - if(arguments == null) { + if (arguments == null) { arguments = new Object[] { null }; } - - Class proxyClass = proxy.getClass(); + + proxyClass = proxy.getClass(); // if a proxy - if(Proxy.isProxyClass(proxyClass)) { + if (Proxy.isProxyClass(proxyClass)) { InvocationHandler handler = Proxy.getInvocationHandler(proxy); + // if a valid object - if((handler instanceof EventHandler) && isValidInvocation(method, + if ((handler instanceof EventHandler) && isValidInvocation(method, arguments)) { try { - Object[] args = getArgs(arguments); // extract value from event property name - Method m = getMethod(proxy, method, arguments, args); // extract method to be invoked on target + // extract value from event property name + Object[] args = getArgs(arguments); + // extract method to be invoked on target + Method m = getMethod(proxy, method, arguments, args); + return m.invoke(target, args); } catch (Throwable t) { System.out.println(t.getClass() + ": " + t.getMessage()); @@ -121,9 +127,11 @@ String action, String eventPropertyName, String listenerMethodName) { - return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { listenerInterface }, - new EventHandler(target, action, eventPropertyName, - listenerMethodName)); + return (T) Proxy.newProxyInstance( + target.getClass().getClassLoader(), + new Class[] { listenerInterface }, + new EventHandler(target, action, eventPropertyName, + listenerMethodName)); } /** @@ -134,8 +142,10 @@ Object target, String action, String eventPropertyName) { - return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { listenerInterface }, - new EventHandler(target, action, eventPropertyName, null)); + return (T) Proxy.newProxyInstance( + target.getClass().getClassLoader(), + new Class[] { listenerInterface }, + new EventHandler(target, action, eventPropertyName, null)); } /** @@ -145,18 +155,21 @@ Class listenerInterface, Object target, String action) { - return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { listenerInterface }, - new EventHandler(target, action, null, null)); + return (T) Proxy.newProxyInstance( + target.getClass().getClassLoader(), + new Class[] { listenerInterface }, + new EventHandler(target, action, null, null)); } private boolean isValidInvocation(Method method, Object[] arguments) { boolean result = false; - - if(listenerMethodName == null) { // all listener methods are valid + + if (listenerMethodName == null) { // all listener methods are valid result = true; - } else if (listenerMethodName.equals(method.getName())) { // method's name matches + } else if (listenerMethodName.equals(method.getName())) { + // method's name matches // no arguments in call are valid - if((eventPropertyName == null) && ((arguments == null) + if ((eventPropertyName == null) && ((arguments == null) || (arguments.length == 0))) { result = true; // one-argument call is value @@ -169,12 +182,12 @@ } else { result = false; } - + return result; } private Object[] getArgs(Object[] arguments) throws Exception { - if(eventPropertyName == null) { + if (eventPropertyName == null) { return new Object[] {}; } else if ((arguments == null) || (arguments.length == 0) || (arguments[0] == null)) { @@ -182,16 +195,16 @@ } else { Object arg = arguments[0]; StringTokenizer st = new StringTokenizer(eventPropertyName, "."); - - while(st.hasMoreTokens()) { + + while (st.hasMoreTokens()) { String propertyName = st.nextToken(); PropertyDescriptor pd = findPropertyDescriptor(arg.getClass(), propertyName); - - if(pd != null) { + + if (pd != null) { Method getter = pd.getReadMethod(); - - if(getter != null) { + + if (getter != null) { arg = getter.invoke(arg, new Object[] {}); } else { throw new IntrospectionException( @@ -201,8 +214,8 @@ } else { Method method = findStaticGetter(arg.getClass(), propertyName); - - if(method != null) { + + if (method != null) { arg = method.invoke(null, new Object[] {}); } else { throw new IntrospectionException( @@ -221,54 +234,55 @@ Object[] arguments, Object[] args) throws Exception { Method result = null; - + // filtering - examine if the 'method' could be applied to proxy - + boolean found = false; // can be invoke with any listener method - if(listenerMethodName == null) { + if (listenerMethodName == null) { Class[] proxyInterfaces = proxy.getClass().getInterfaces(); - - for(int i = 0; i < proxyInterfaces.length; ++i) { + + for (int i = 0; i < proxyInterfaces.length; ++i) { Method[] interfaceMethods = proxyInterfaces[i].getMethods(); - - for(int k = 0; k < interfaceMethods.length; ++k) { + + for (int k = 0; k < interfaceMethods.length; ++k) { Method listenerMethod = interfaceMethods[k]; - - if(equalNames(listenerMethod, method) + + if (equalNames(listenerMethod, method) && canInvokeWithArguments(listenerMethod, arguments)) { found = true; break; } } - - if(found) { + + if (found) { break; } } // can be invoked with a specified listener method - } else if(method.getName().equals(listenerMethodName)){ + } else if (method.getName().equals(listenerMethodName)){ found = true; } - - if(found == false) { + + if (found == false) { return null; } // 'Method' can be applied to proxy - filtering succeeded try { result = findMethod(target.getClass(), args); - if(result == null) { + if (result == null) { PropertyDescriptor pd = findPropertyDescriptor( target.getClass(), action); - if(pd != null) { + + if (pd != null) { result = pd.getWriteMethod(); - - if(result == null) { + + if (result == null) { throw new NoSuchMethodException( "no setter for property " + action + " found"); } @@ -282,7 +296,7 @@ } catch (IntrospectionException ie) { throw new Exception("Exception while finding property descriptor"); } - + return result; } @@ -292,8 +306,9 @@ PropertyDescriptor result = null; BeanInfo beanInfo = Introspector.getBeanInfo(theClass); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); - for(int i = 0; i < pds.length; ++i) { - if(pds[i].getName().equals(propertyName)) { + + for (int i = 0; i < pds.length; ++i) { + if (pds[i].getName().equals(propertyName)) { result = pds[i]; break; } @@ -305,24 +320,24 @@ Class theClass, String propertyName) throws IntrospectionException { Method result = null; - Method[] methods = theClass.getMethods(); - for(int i = 0; i < methods.length; ++i) { + + for (int i = 0; i < methods.length; ++i) { int modifiers = methods[i].getModifiers(); - - if(Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) { + + if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) { String methodName = methods[i].getName(); String postfix = null; - - if(methodName.startsWith("get")) { + + if (methodName.startsWith("get")) { postfix = methodName.substring(3); - } else if(methodName.startsWith("is")) { + } else if (methodName.startsWith("is")) { postfix = methodName.substring(2); } else { continue; } - - if( + + if ( (methods[i].getParameterTypes().length != 0) || (methods[i].getReturnType() == void.class) ) { @@ -330,7 +345,7 @@ } postfix = Introspector.decapitalize(postfix); - if(postfix.equals(propertyName)) { + if (postfix.equals(propertyName)) { result = methods[i]; break; } @@ -342,14 +357,14 @@ private Method findMethod(Class type, Object[] args) { Method[] methods = type.getMethods(); - - for(int i = 0; i < methods.length; ++i) { - if(action.equals(methods[i].getName()) && canInvokeWithArguments( + + for (int i = 0; i < methods.length; ++i) { + if (action.equals(methods[i].getName()) && canInvokeWithArguments( methods[i], args)) { return methods[i]; } } - + return null; } @@ -366,24 +381,25 @@ private static boolean canInvokeWithArguments(Method m, Object[] arguments) { Class[] parameterTypes = m.getParameterTypes(); - - if(parameterTypes.length == arguments.length) { - for(int i = 0; i < arguments.length; ++i) { + + if (parameterTypes.length == arguments.length) { + for (int i = 0; i < arguments.length; ++i) { Class argumentType = (arguments[i] == null) ? null : arguments[i].getClass(); - if((argumentType == null) || isPrimitiveWrapper(argumentType, + + if ((argumentType == null) || isPrimitiveWrapper(argumentType, parameterTypes[i])) { // ... nothing to do - just not to break the cycle - } else if(!argumentType.isAssignableFrom(parameterTypes[i])) { + } else if (!argumentType.isAssignableFrom(parameterTypes[i])) { return false; } } } else { return false; } - + return true; - + } private static boolean equalNames(Method m1, Method m2) { Index: modules/beans/src/main/java/java/beans/EventSetDescriptor.java =================================================================== --- modules/beans/src/main/java/java/beans/EventSetDescriptor.java (revision 420465) +++ modules/beans/src/main/java/java/beans/EventSetDescriptor.java (working copy) @@ -51,28 +51,28 @@ public EventSetDescriptor(Class sourceClass, String eventSetName, Class listenerType, String listenerMethodName ) throws IntrospectionException { - + super(); - + setName(eventSetName); setDisplayName(eventSetName); this.eventSetName = eventSetName; this.listenerType = listenerType; - + this.listenerMethodDescriptors.add( new MethodDescriptor(findMethodByName(listenerType, listenerMethodName))); - + this.addListenerMethod = findMethodByPrefix(sourceClass, "add", "", listenerType); this.removeListenerMethod = findMethodByPrefix(sourceClass, "remove", "", listenerType); - - if(addListenerMethod == null && removeListenerMethod == null) { + + if (addListenerMethod == null && removeListenerMethod == null) { throw new IntrospectionException( "Add and remove methods are not available"); } - + this.getListenerMethod = findMethodByPrefix(sourceClass, "get", "s", listenerType); @@ -88,14 +88,14 @@ String addListenerMethodName, String removeListenerMethodName ) throws IntrospectionException { super(); - + setName(eventSetName); setDisplayName(eventSetName); this.eventSetName = eventSetName; this.listenerType = listenerType; - if(listenerMethodNames != null) { - for(int i = 0; i < listenerMethodNames.length; ++i) { + if (listenerMethodNames != null) { + for (int i = 0; i < listenerMethodNames.length; ++i) { try { listenerMethodDescriptors.add(new MethodDescriptor( findMethodByName(listenerType, listenerMethodNames[i]))); @@ -111,7 +111,7 @@ this.removeListenerMethod = findMethodByName(listenerType, removeListenerMethodName); this.getListenerMethod = null; - + this.unicast = isUnicastByDefault(addListenerMethod); } @@ -128,34 +128,34 @@ String removeListenerMethodName, String getListenerMethodName) throws IntrospectionException { super(); - + setName(eventSetName); setDisplayName(eventSetName); this.eventSetName = eventSetName; this.listenerType = listenerType; - if(listenerMethodNames != null) { - for(int i = 0; i < listenerMethodNames.length; ++i) { + if (listenerMethodNames != null) { + for (int i = 0; i < listenerMethodNames.length; ++i) { try { listenerMethodDescriptors.add( - new MethodDescriptor(findMethodByName(listenerType, - listenerMethodNames[i]))); + new MethodDescriptor(findMethodByName(listenerType, + listenerMethodNames[i]))); } catch (IntrospectionException ie) { listenerMethodDescriptors.clear(); throw ie; } } } - + this.addListenerMethod = findMethodByName(listenerType, addListenerMethodName); this.removeListenerMethod = findMethodByName(listenerType, removeListenerMethodName); this.getListenerMethod = findMethodByName(listenerType, getListenerMethodName); - + this.unicast = isUnicastByDefault(addListenerMethod); - + } /** @@ -168,15 +168,15 @@ Method addListenerMethod, Method removeListenerMethod) throws IntrospectionException { super(); - + setName(eventSetName); setDisplayName(eventSetName); this.eventSetName = eventSetName; this.listenerType = listenerType; - - if(listenerMethods != null) { - for(int i = 0; i < listenerMethods.length; ++i) { - if(checkMethod(listenerType, listenerMethods[i])) { + + if (listenerMethods != null) { + for (int i = 0; i < listenerMethods.length; ++i) { + if (checkMethod(listenerType, listenerMethods[i])) { this.listenerMethodDescriptors.add(new MethodDescriptor( listenerMethods[i])); } @@ -188,9 +188,9 @@ this.removeListenerMethod = checkRegistrationMethod(listenerType, removeListenerMethod); this.getListenerMethod = null; - + this.unicast = isUnicastByDefault(addListenerMethod); - + } /** @@ -201,16 +201,16 @@ Method removeListenerMethod, Method getListenerMethod ) throws IntrospectionException { - super(); - + super(); + setName(eventSetName); setDisplayName(eventSetName); this.eventSetName = eventSetName; this.listenerType = listenerType; - - if(listenerMethods != null) { - for(int i = 0; i < listenerMethods.length; ++i) { - if(checkMethod(listenerType, listenerMethods[i])) { + + if (listenerMethods != null) { + for (int i = 0; i < listenerMethods.length; ++i) { + if (checkMethod(listenerType, listenerMethods[i])) { this.listenerMethodDescriptors.add(new MethodDescriptor( listenerMethods[i])); } @@ -223,9 +223,9 @@ removeListenerMethod); this.getListenerMethod = checkGetListenerMethod(listenerType, getListenerMethod); - + this.unicast = isUnicastByDefault(addListenerMethod); - + } /** @@ -236,28 +236,28 @@ Method addListenerMethod, Method removeListenerMethod ) throws IntrospectionException { super(); - + setName(eventSetName); setDisplayName(eventSetName); this.eventSetName = eventSetName; this.listenerType = listenerType; - + for (int i = 0; i < listenerMethodDescriptors.length; ++i) { Method listenerMethod = listenerMethodDescriptors[i].getMethod(); - if(checkMethod(listenerType, listenerMethod)) { + + if (checkMethod(listenerType, listenerMethod)) { this.listenerMethodDescriptors.add( listenerMethodDescriptors[i]); } } - + this.addListenerMethod = checkRegistrationMethod(listenerType, addListenerMethod); this.removeListenerMethod = checkRegistrationMethod(listenerType, removeListenerMethod); this.getListenerMethod = null; - + this.unicast = isUnicastByDefault(addListenerMethod); - } /** @@ -267,8 +267,10 @@ Method[] result = new Method[listenerMethodDescriptors.size()]; Iterator i = listenerMethodDescriptors.iterator(); int idx = 0; - while(i.hasNext()) { + + while (i.hasNext()) { MethodDescriptor md = i.next(); + result[idx] = md.getMethod(); idx++; } @@ -283,7 +285,8 @@ new MethodDescriptor[listenerMethodDescriptors.size()]; Iterator i = listenerMethodDescriptors.iterator(); int idx = 0; - while(i.hasNext()) { + + while (i.hasNext()) { result[idx] = i.next(); idx++; } @@ -351,13 +354,14 @@ String listenerTypeName = listenerType.getName(); int idx = listenerTypeName.lastIndexOf("Listener"); String eventTypeName = listenerTypeName.substring(0, idx) + "Event"; + return Class.forName(eventTypeName, true, listenerType.getClassLoader()); } private boolean checkMethod(Class listenerType, Method listenerMethod) throws IntrospectionException { - if(listenerMethod != null + if (listenerMethod != null && !listenerMethod.getDeclaringClass().isAssignableFrom( listenerType)) { throw new IntrospectionException("No method \"" @@ -389,11 +393,15 @@ String prefix, String postfix, Class listenerType) { - String fullName = listenerType.getName(); // com.drl.beans.SmthListener + + // com.drl.beans.SmthListener + String fullName = listenerType.getName(); int idx = fullName.lastIndexOf("."); - String methodName = prefix + fullName.substring(idx + 1) + postfix; // prefix(e.g., add) + SmthListener + // prefix(e.g., add) + SmthListener + String methodName = prefix + fullName.substring(idx + 1) + postfix; + try { - if(prefix.equals("get")) { + if (prefix.equals("get")) { return sourceClass.getMethod(methodName, new Class[] {}); } else { return sourceClass.getMethod(methodName, @@ -404,34 +412,36 @@ } } - + private static boolean isUnicastByDefault(Method addMethod) { - if(addMethod != null) { + if (addMethod != null) { Class[] exceptionTypes = addMethod.getExceptionTypes(); - for(int i = 0; i < exceptionTypes.length; ++i) { - if(exceptionTypes[i].equals(TooManyListenersException.class)) { + + for (int i = 0; i < exceptionTypes.length; ++i) { + if (exceptionTypes[i].equals(TooManyListenersException.class)) { return true; } } } return false; } - + private static Method checkRegistrationMethod( Class listenerType, Method registrationMethod) throws IntrospectionException { - if(registrationMethod == null) { + if (registrationMethod == null) { return null; } else { Class returnType = registrationMethod.getReturnType(); - - if(returnType != void.class) { + Class[] parameterTypes; + + if (returnType != void.class) { throw new IntrospectionException(registrationMethod.getName() + " does not return "); } - - Class[] parameterTypes = registrationMethod.getParameterTypes(); - if(parameterTypes == null || parameterTypes.length != 1) { + + parameterTypes = registrationMethod.getParameterTypes(); + if (parameterTypes == null || parameterTypes.length != 1) { throw new IntrospectionException(registrationMethod.getName() + " should have a single input parameter"); } else if(parameterTypes[0] != listenerType){ @@ -447,18 +457,19 @@ private static Method checkGetListenerMethod( Class listenerType, Method getListenerMethod) throws IntrospectionException { - if(getListenerMethod == null) { + if (getListenerMethod == null) { return null; } else { Class[] parameterTypes = getListenerMethod.getParameterTypes(); - - if(parameterTypes.length != 0) { + Class returnType; + + if (parameterTypes.length != 0) { throw new IntrospectionException( "No input params are allowed for getListenerMethod"); } - - Class returnType = getListenerMethod.getReturnType(); - if(returnType.isArray() + + returnType = getListenerMethod.getReturnType(); + if (returnType.isArray() && returnType.getComponentType() == listenerType) { return getListenerMethod; } else {