You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by qi...@apache.org on 2008/06/03 03:57:56 UTC

svn commit: r662622 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/ main/java/org/apache/harmony/beans/internal/nls/ test/java/org/apache/harmony/beans/tests/java/beans/ test/support/java/org/apache/harmony/beans/tests/su...

Author: qiuxx
Date: Mon Jun  2 18:57:55 2008
New Revision: 662622

URL: http://svn.apache.org/viewvc?rev=662622&view=rev
Log:
Apply patch for HARMONY-5854, ( [classlib][beans] java.beans.Statement.findMethod() throw java.lang.NoSuchMethodException: Cannot decide which method to call)

Added:
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/TInspectorCluster.java   (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java?rev=662622&r1=662621&r2=662622&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java Mon Jun  2 18:57:55 2008
@@ -148,7 +148,7 @@
                             .isAssignableFrom(theArguments[i].getClass());
                     if (!isNull && !isPrimitiveWrapper && !isAssignable) {
                         throw new IllegalArgumentException(Messages
-                                .getString("beans.62")); //$NON-NLS-1$
+                                .getString("beans.63")); //$NON-NLS-1$
                     }
                 }
                 result = Array.newInstance(clazz, length);
@@ -368,8 +368,23 @@
         for (int i = 1; i < foundMethodsArr.length; i++) {
             int difference = comparator.compare(chosenOne, foundMethodsArr[i]);
             //if 2 methods have same relevance, throw exception
-            if(difference == 0){
-                throw new NoSuchMethodException("Cannot decide which method to call: "+methodName); //$NON-NLS-1$
+            if (difference == 0) {
+                // if 2 methods have the same signature, check their return type
+                Class<?> oneReturnType = chosenOne.getReturnType();
+                Class<?> foundMethodReturnType = foundMethodsArr[i]
+                        .getReturnType();
+                if (oneReturnType.equals(foundMethodReturnType)) {
+                    // if 2 methods have the same signature and return type,
+                    // throw NoSuchMethodException
+                    throw new NoSuchMethodException(Messages.getString(
+                            "beans.62", methodName)); //$NON-NLS-1$
+                }
+
+                if (oneReturnType.isAssignableFrom(foundMethodReturnType)) {
+                    // if chosenOne is super class or interface of
+                    // foundMethodReturnType, set chosenOne to foundMethodArr[i]
+                    chosenOne = foundMethodsArr[i];
+                }
             }
             if(difference > 0){
                 chosenOne = foundMethodsArr[i];

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties?rev=662622&r1=662621&r2=662622&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties Mon Jun  2 18:57:55 2008
@@ -114,4 +114,4 @@
 beans.60=Indexed write method must take an int as its first argument
 beans.61=Indexed write method is not compatible with indexed read method
 beans.62=Cannot decide which method to call to match {0}
-beans.62=The type of element is mismatch with the type of array
\ No newline at end of file
+beans.63=The type of element is mismatch with the type of array

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java?rev=662622&r1=662621&r2=662622&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/StatementTest.java Mon Jun  2 18:57:55 2008
@@ -28,6 +28,19 @@
 import junit.textui.TestRunner;
 
 import org.apache.harmony.beans.tests.support.SampleException;
+import org.apache.harmony.beans.tests.support.TInspectorCluster;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.Ancestor;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.BooleanInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.CharacterInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.DoubleInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.FloatInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.IntegerInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.LongInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.ObjectInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.ObjectListInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.Offspring;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.ShortInspector;
+import org.apache.harmony.beans.tests.support.TInspectorCluster.StringInspector;
 
 /**
  * Test the class java.beans.Statement.
@@ -871,6 +884,139 @@
         t.execute();
         MockObject.assertCalled("overloadedmethodB", arguments);
     }
+    
+    /*
+     * Test for special case of the same signature but differnt return type
+     */
+    public void testExecute_SameSignatureDifferentReturn() throws Exception {
+        // Regression for Harmony-5854
+        Object[] ancestorArguments = new Object[] { new Ancestor() {
+        } };
+        Object[] offspringArguments = new Object[] { new Offspring() {
+        } };
+        String methodName = "visit";
+        Statement statement = null;
+
+        statement = new Statement(new StringInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_STRING);
+
+        statement = new Statement(new StringInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_STRING);
+
+        statement = new Statement(new BooleanInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_BOOLEAN);
+
+        statement = new Statement(new BooleanInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_BOOLEAN);
+
+        statement = new Statement(new CharacterInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_CHARACTER);
+
+        statement = new Statement(new CharacterInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_CHARACTER);
+
+        statement = new Statement(new ShortInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_SHORT);
+
+        statement = new Statement(new ShortInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_SHORT);
+
+        statement = new Statement(new IntegerInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_INTEGER);
+
+        statement = new Statement(new IntegerInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_INTEGER);
+
+        statement = new Statement(new LongInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_LONG);
+
+        statement = new Statement(new LongInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_LONG);
+
+        statement = new Statement(new FloatInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_FLOAT);
+
+        statement = new Statement(new FloatInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_FLOAT);
+
+        statement = new Statement(new DoubleInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_DOUBLE);
+
+        statement = new Statement(new DoubleInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_DOUBLE);
+
+        statement = new Statement(new ObjectInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_OBJECT);
+
+        statement = new Statement(new ObjectInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_OBJECT);
+
+        statement = new Statement(new ObjectListInspector(), "visit",
+                ancestorArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, ancestorArguments,
+                TInspectorCluster.ANCESTOR_OBJECT_LIST);
+
+        statement = new Statement(new ObjectListInspector(), "visit",
+                offspringArguments);
+        statement.execute();
+        TInspectorCluster.assertMethodCalled(methodName, offspringArguments,
+                TInspectorCluster.OFFSPRING_OBJECT_LIST);
+    }
 
     /*
      * Super class of MockObject.

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/TInspectorCluster.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/TInspectorCluster.java?rev=662622&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/TInspectorCluster.java (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/TInspectorCluster.java Mon Jun  2 18:57:55 2008
@@ -0,0 +1,298 @@
+/*
+ *  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;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import junit.framework.Assert;
+
+public class TInspectorCluster {
+
+    public static final String ANCESTOR_STRING = "ancestor";
+
+    public static final String OFFSPRING_STRING = "offspring";
+
+    public static final boolean ANCESTOR_BOOLEAN = Boolean.FALSE;
+
+    public static final boolean OFFSPRING_BOOLEAN = Boolean.TRUE;
+
+    public static final char ANCESTOR_CHARACTER = 'A';
+
+    public static final char OFFSPRING_CHARACTER = 'O';
+
+    public static final short ANCESTOR_SHORT = 2;
+
+    public static final short OFFSPRING_SHORT = -2;
+
+    public static final int ANCESTOR_INTEGER = Integer.MIN_VALUE;
+
+    public static final int OFFSPRING_INTEGER = Integer.MAX_VALUE;
+
+    public static final long ANCESTOR_LONG = 453298580984320l;
+
+    public static final long OFFSPRING_LONG = -453298580984320l;
+
+    public static final float ANCESTOR_FLOAT = 0.5f;
+
+    public static final float OFFSPRING_FLOAT = -0.5f;
+
+    public static final double ANCESTOR_DOUBLE = 0.12;
+
+    public static final double OFFSPRING_DOUBLE = -0.12;
+
+    public static final MockA ANCESTOR_OBJECT = new MockA();
+
+    public static final MockB OFFSPRING_OBJECT = new MockB();
+
+    public static final List<MockA> ANCESTOR_OBJECT_LIST = new ArrayList<MockA>();
+
+    public static final List<MockB> OFFSPRING_OBJECT_LIST = new ArrayList<MockB>();
+
+    private static String calledMethodName = null;
+
+    private static Object calledMethodResult = null;
+
+    private static Object[] calledmethodArguments = null;
+
+    static {
+        ANCESTOR_OBJECT_LIST.add(new MockA());
+        OFFSPRING_OBJECT_LIST.add(new MockB());
+    }
+
+    public interface Ancestor {
+    }
+
+    public interface Offspring extends Ancestor {
+    }
+
+    public interface Visitor<T> {
+
+        public T visit(Ancestor o);
+
+    }
+
+    /*
+     * check whether the right method is called
+     */
+    public static void assertMethodCalled(String methodName,
+            Object[] arguments, Object expectResult) {
+        Assert.assertEquals(methodName, calledMethodName);
+        Assert.assertTrue(Arrays.equals(arguments, calledmethodArguments));
+        Assert.assertEquals(expectResult, calledMethodResult);
+        reset();
+    }
+
+    private static void reset() {
+        calledMethodName = null;
+        calledMethodResult = null;
+        calledmethodArguments = null;
+    }
+
+    public static class StringInspector implements Visitor<String> {
+
+        public String visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_STRING;
+            return null;
+        }
+
+        public String visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_STRING;
+            return null;
+        }
+
+    }
+
+    public static class BooleanInspector implements Visitor<Boolean> {
+
+        public Boolean visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_BOOLEAN;
+            return null;
+        }
+
+        public Boolean visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_BOOLEAN;
+            return null;
+        }
+
+    }
+
+    public static class CharacterInspector implements Visitor<Character> {
+
+        public Character visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_CHARACTER;
+            return null;
+        }
+
+        public Character visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_CHARACTER;
+            return null;
+        }
+
+    }
+
+    public static class ShortInspector implements Visitor<Short> {
+
+        public Short visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_SHORT;
+            return null;
+        }
+
+        public Short visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_SHORT;
+            return null;
+        }
+
+    }
+
+    public static class IntegerInspector implements Visitor<Integer> {
+
+        public Integer visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_INTEGER;
+            return null;
+        }
+
+        public Integer visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_INTEGER;
+            return null;
+        }
+
+    }
+
+    public static class LongInspector implements Visitor<Long> {
+
+        public Long visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_LONG;
+            return null;
+        }
+
+        public Long visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_LONG;
+            return null;
+        }
+
+    }
+
+    public static class FloatInspector implements Visitor<Float> {
+
+        public Float visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_FLOAT;
+            return null;
+        }
+
+        public Float visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_FLOAT;
+            return null;
+        }
+
+    }
+
+    public static class DoubleInspector implements Visitor<Double> {
+
+        public Double visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_DOUBLE;
+            return null;
+        }
+
+        public Double visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_DOUBLE;
+            return null;
+        }
+
+    }
+
+    public static class ObjectInspector implements Visitor<Object> {
+
+        public Object visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_OBJECT;
+            return null;
+        }
+
+        public Object visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_OBJECT;
+            return null;
+        }
+
+    }
+
+    public static class ObjectListInspector implements Visitor<List> {
+
+        public List visit(Ancestor o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = ANCESTOR_OBJECT_LIST;
+            return null;
+        }
+
+        public List visit(Offspring o) {
+            calledMethodName = "visit";
+            calledmethodArguments = new Object[] { o };
+            calledMethodResult = OFFSPRING_OBJECT_LIST;
+            return null;
+        }
+    }
+
+}
+
+class MockA {
+
+    public static final String NAME = "A";
+
+}
+
+class MockB {
+
+    public static final String NAME = "B";
+
+}

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