You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by to...@apache.org on 2007/07/16 10:18:34 UTC

svn commit: r556545 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/ test/java/org/apache/harmony/beans/tests/java/beans/ test/support/java/org/apache/harmony/beans/tests/support/mock/

Author: tonywu
Date: Mon Jul 16 01:18:27 2007
New Revision: 556545

URL: http://svn.apache.org/viewvc?view=rev&rev=556545
Log:
Fix bug: EventSetDescriptor with less check for listeners

Added:
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/MockPropertyChangeValidListener.java   (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/EventSetDescriptor.java
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/EventSetDescriptorTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/EventSetDescriptor.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/EventSetDescriptor.java?view=diff&rev=556545&r1=556544&r2=556545
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/EventSetDescriptor.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/EventSetDescriptor.java Mon Jul 16 01:18:27 2007
@@ -38,6 +38,10 @@
     private boolean unicast;
 
     private boolean inDefaultEventSet = true;
+    
+    private Class awtEventListener;
+    
+    private Class awtEventAction;
 
     public EventSetDescriptor(Class<?> sourceClass, String eventSetName,
             Class<?> listenerType, String listenerMethodName)
@@ -95,7 +99,7 @@
         for (String element : listenerMethodNames) {
             Method m = findListenerMethodByName(element);
 
-            checkEventType(eventSetName, m);
+            //checkEventType(eventSetName, m);
             listenerMethodDescriptors.add(new MethodDescriptor(m));
         }
 
@@ -114,6 +118,28 @@
         this.unicast = isUnicastByDefault(addListenerMethod);
     }
 
+    private Method findListenerMethodByName(
+            String listenerMethodName) throws IntrospectionException {
+        Method method = null;
+        Method[] methods = listenerType.getMethods();
+        for (Method m : methods) {
+            if (m.getName().equals(listenerMethodName)) {
+                Class[] paramTypes = m.getParameterTypes();
+                if (paramTypes.length == 1
+                        && paramTypes[0].getName().endsWith("Event")) {
+                    method = m;
+                    break;
+                }
+                
+            }
+        }
+        if (null == method) {
+            throw new IntrospectionException(Messages.getString("beans.31", //$NON-NLS-1$
+                    listenerMethodName, listenerType.getName()));
+        }
+        return method;
+    }
+
     public EventSetDescriptor(String eventSetName, Class<?> listenerType,
             Method[] listenerMethods, Method addListenerMethod,
             Method removeListenerMethod) throws IntrospectionException {
@@ -285,20 +311,9 @@
         return inDefaultEventSet;
     }
 
-    /**
-     * @return type of event associated with the current event set descriptor
-     * @throws ClassNotFoundException if event class is not found
-     */
-    private Class<?> getEventType() throws ClassNotFoundException {
-        String listenerTypeName = listenerType.getName();
-        String prefix = listenerTypeName.substring(0,
-                listenerTypeName.indexOf(
-                        BeanInfoImpl.extractShortClassName(listenerTypeName)));
-        String eventTypeName = prefix + prepareEventTypeName(getName());
-        
-        return Class.forName(eventTypeName, true, listenerType.getClassLoader());
-    }
-
+    
+    private static final String AWT_EVENT_PREFIX="java.awt.event.";
+    
     private boolean checkMethod(Class<?> listenerType, Method listenerMethod)
             throws IntrospectionException {
         if (listenerMethod != null && listenerType != null &&    
@@ -309,28 +324,7 @@
         return true;
     }
 
-    /**
-     * Searches for the method in listener that has the given name. Parameter
-     * check is also performed for found methods.
-     * @param listenerMethodName name of listener method
-     * @return found method if any
-     * @throws IntrospectionException if no method is found or other error
-     * has occured
-     */
-    private Method findListenerMethodByName(String listenerMethodName) 
-            throws IntrospectionException {
-        try {
-            return listenerType.getMethod(listenerMethodName, getEventType());
-        } catch (NoSuchMethodException nsme) {
-            throw new IntrospectionException(Messages.getString(
-                    "beans.31", //$NON-NLS-1$
-                    listenerMethodName, listenerType.getName()));
-        } catch (ClassNotFoundException cnfe) {
-            throw new IntrospectionException(Messages.getString(
-                    "beans.32", listenerType.getName())); //$NON-NLS-1$
-        }
-    }
-    
+        
     /**
      * Searches for {add|remove}Listener methods in the event source.
      * Parameter check is also performed.
@@ -343,18 +337,55 @@
             String methodName) throws IntrospectionException {
         try {
             return sourceClass.getMethod(methodName, listenerType);
+        } catch (NoSuchMethodException e) {
+            return findAddRemoveListnerMethodWithLessCheck(sourceClass,
+                    methodName, listenerType);
         } catch (Exception e) {
-            throw new IntrospectionException(
-                    Messages.getString("beans.31", //$NON-NLS-1$
-                            methodName, listenerType.getName()));
+            throw new IntrospectionException(Messages.getString("beans.31", //$NON-NLS-1$
+                    methodName, listenerType.getName()));
         }
     }
+    
+    private Method findAddRemoveListnerMethodWithLessCheck(
+            Class<?> sourceClass, String methodName, Class listenerTYpe)
+            throws IntrospectionException {
+        String expectedListenerTypeName = listenerType.getName();
+        expectedListenerTypeName = expectedListenerTypeName
+                .substring(expectedListenerTypeName.lastIndexOf(".") + 1);
+        Method method = null;
+        Method[] methods = sourceClass.getMethods();
+        for (Method m : methods) {
+            if (m.getName().equals(methodName)) {
+                Class[] paramTypes = m.getParameterTypes();
+                if (paramTypes.length == 1) {
+                    String paramTypeName = paramTypes[0].getName();
+                    paramTypeName = paramTypeName.substring(paramTypeName
+                            .lastIndexOf(".") + 1);
+                    if (paramTypeName.endsWith("Listener")) {
+                        paramTypeName = paramTypeName.substring(0,
+                                paramTypeName.length() - "Listener".length());
+                        if (expectedListenerTypeName.startsWith(paramTypeName)) {
+                            method = m;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+        if (null == method) {
+            throw new IntrospectionException(Messages.getString("beans.31", //$NON-NLS-1$
+                    methodName, listenerType.getName()));
+        }
+        return method;
+    }
 
     /**
-     * @param sourceClass class of event source
-     * @param methodName name of the custom getListeners() method
-     * @return found Method object for custom getListener or null if nothing
-     *  is found
+     * @param sourceClass
+     *            class of event source
+     * @param methodName
+     *            name of the custom getListeners() method
+     * @return found Method object for custom getListener or null if nothing is
+     *         found
      */
     private Method findGetListenerMethod(Class<?> sourceClass,
             String methodName) {
@@ -371,7 +402,7 @@
         String shortName = BeanInfoImpl.extractShortClassName(
                 listenerType.getName());
         String methodName = prefix + shortName + postfix;
-
+        Class listenerType = this.awtEventListener == null? this.listenerType:this.awtEventListener;
         try {
             if (prefix.equals("get")) { //$NON-NLS-1$
                 return sourceClass.getMethod(methodName);

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/EventSetDescriptorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/EventSetDescriptorTest.java?view=diff&rev=556545&r1=556544&r2=556545
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/EventSetDescriptorTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/EventSetDescriptorTest.java Mon Jul 16 01:18:27 2007
@@ -34,6 +34,7 @@
 import org.apache.harmony.beans.tests.support.mock.MockFakeListener;
 import org.apache.harmony.beans.tests.support.mock.MockPropertyChangeEvent;
 import org.apache.harmony.beans.tests.support.mock.MockPropertyChangeListener;
+import org.apache.harmony.beans.tests.support.mock.MockPropertyChangeValidListener;
 
 /**
  * Unit test for EventSetDescriptor
@@ -479,6 +480,21 @@
             fail("Should throw IntrospectionException.");
         } catch (IntrospectionException e) {
         }
+    }
+    
+    public void testEventSetDescriptorClassStringClassStringArrayStringString_listenerMethodNamesValid()
+            throws Exception {
+        Class<MockSourceClass> sourceClass = MockSourceClass.class;
+        String eventSetName = "MockPropertyChange";
+        Class<?> listenerType = MockPropertyChangeValidListener.class;
+        String[] listenerMethodNames = { "mockPropertyChange_Valid",
+                "mockPropertyChange2", };
+        String addMethod = "addMockPropertyChangeListener";
+        String removeMethod = "removeMockPropertyChangeListener";
+        EventSetDescriptor eventSetDescriptor = new EventSetDescriptor(
+                sourceClass, eventSetName, listenerType, listenerMethodNames,
+                addMethod, removeMethod);
+        assertEquals(2, eventSetDescriptor.getListenerMethods().length);
     }
 
     public void testEventSetDescriptorClassStringClassStringArrayStringString_listenerMethodNamesEmpty()

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/MockPropertyChangeValidListener.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/MockPropertyChangeValidListener.java?view=auto&rev=556545
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/MockPropertyChangeValidListener.java (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/MockPropertyChangeValidListener.java Mon Jul 16 01:18:27 2007
@@ -0,0 +1,50 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.beans.tests.support.mock;
+
+import java.util.EventListener;
+
+public interface MockPropertyChangeValidListener extends EventListener {
+    /*
+     * fire MockPropertyChange event.
+     */
+
+    public void mockPropertyChange(MockPropertyChangeEvent e);
+
+    public void mockPropertyChange2(MockPropertyChangeEvent e);
+
+    public void mockPropertyChange3(MockPropertyChangeEvent e);
+
+    public void mockNotAEventObject(MockFakeEvent event);
+
+    public void mockPropertyChange_Valid(MockEvent event); 
+    
+    public void mockPropertyChange_Valid(Mock2Event event);
+    
+    public void mockPropertyChange_Valid(MockPropertyChangeEvent event);
+   
+
+}
+
+class MockEvent 
+{
+}
+
+class Mock2Event
+{
+}

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/MockPropertyChangeValidListener.java
------------------------------------------------------------------------------
    svn:eol-style = native