JDK-4530962 : Statement.execute() should throw exception if the meth invocation is ambiguous
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 1.4.0,5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,solaris_2.6
  • CPU: generic,sparc
  • Submitted: 2001-11-24
  • Updated: 2004-09-24
  • Resolved: 2004-09-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b28Fixed
Related Reports
Relates :  
Description
Name: dsR10051			Date: 11/23/2001


The behavior of method 
public void execute() throws Exception
in class java.beans.Statement is not follow to specification.
Javadoc for this method says:
    /**
     * The execute method finds a method whose name is the same
     * as the methodName property, and invokes the method on
     * the target.
     *
     * When the target's class defines many methods with the given name
     * the implementation should choose the most specific method using
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     * the algorithm specified in the Java Language Specification
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     * (15.11). The dynamic class of the target and arguments are used
     * in place of the compile-time type information and, like the
     * <code>java.lang.reflect.Method</code> class itself, conversion between
     * primitive values and their associated wrapper classes is handled
     * internally.
     ...
     */
    public void execute() throws Exception {

Algorithm declares compile-time error if no method is the most 
specific (because there are two or more maximally specific methods). 
In this case execute() method should throw Exception to satisfy 
the specification. 
The following example demonstrates that no exception occurs during  
the execute() method invokation in this case.

--- StatementTest_2.java ---
import java.beans.*;

public class StatementTest_2 {

    public static void main (String[] args) {
        StatementTest_2 target = new StatementTest_2();
        String methodName = "method_1";
        B argument0 = new B();
        B argument1 = new B();
        Object[] arguments = {argument0, argument1};
        Statement statement = new Statement(target, methodName, arguments);
        try {
            statement.execute();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("OKAY");
            return;
        }
        System.out.println("Failed: Exception expected");
        System.out.println("reference to method_1 is ambiguous,");
        System.out.println("both StatementTest_2.method_1(A,B) and " + 
                "StatementTest_2.method_1(B,A) match");
//        target.method_1(argument0, argument1);
    }

    public void method_1 (A a, B b) {
        System.out.println("method_1(A, B) called");
    }
    public void method_1 (B b, A a) {
        System.out.println("method_1(B, A) called");
    }
}

--- A.java ---
public class A {
}
--- B.java ---
public class B extends A {
}

--- Output ---
/set/jdk-builds/JDK1.4.0beta2-b86/solaris/bin/java StatementTest_2 
method_1(A, B) called
Failed: Exception expected
reference to method_1 is ambiguous,
both StatementTest_2.method_1(A,B) and StatementTest_2.method_1(B,A) match
--------------

If target.method_1(argument0, argument1) call is uncommented in the example,
compile-time error occurs:

/set/jdk-builds/JDK1.4.0beta2-b86/solaris/bin/javac StatementTest_2.java 
StatementTest_2.java:23: reference to method_1 is ambiguous, both method method_1(A,B) in StatementTest_2 and method method_1(B,A) in StatementTest_2 match
        target.method_1(argument0, argument1);
              ^
1 error
----------------------------
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta FIXED IN: tiger-beta INTEGRATED IN: tiger-b28 tiger-beta VERIFIED IN: 1.5.0_01
25-09-2004

EVALUATION There is nothing in the spec which claims that an execption should be thown if the methods are ambiguous. So this is not a bug, but rather an RFE. Potentially, this can introduce some incompatibilities. This issue is related to 4531018 and 4587727. The place to invoke the exception (assuming the fix for 4587727) is that findPublicMethod should return null if the list of found methods doesn't provide a precise match. Whatever the behavior that we decide, the spec should be updated to clarify the sematics whenever ambiguous methods are encountered. ###@###.### 2001-12-12 The search for the most specific method at runtime will follow the general algorithm specificied for compile time in the JLS: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448 If more than one method matches the signature then the most specific method will be found in the following precedence order: 1. If the method argument types exactly match the parameter types. 2. The most number of matches that the method argument types have with the param types. 3. If there are no matches, then the method which has the closest match in the inheritance hierarchy. 4. If multiple methods contain the same number of matches then then the method is ambiguous and no method will be returned. ###@###.### 2003-10-13
13-10-2003