Bug 56797

Summary: EL-API does not find overriden method
Product: Tomcat 7 Reporter: S. Dabek <dabek>
Component: JasperAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 7.0.55   
Target Milestone: ---   
Hardware: All   
OS: All   

Description S. Dabek 2014-07-31 15:55:58 UTC
Util.findWrapper(..) returns with a MethodNotFoundException (util.method.ambiguous) under the following conditions:
- A method is overriden and the return type in the overriden method is inherited from the original method's return type
- The method is called with a parameter whose type is inherited from the declared type (assignable)

Apparently, this leads to a situation with multiple matches.
If only one of those conditions is true, the error does not occur.

Example:
--------
	
TesterBeanA.java:

    public CharSequence getCharSequence(CharSequence cs) {
        return cs;
    }
	
TesterBeanAA.java

    @Override
    public String getCharSequence(CharSequence cs) {
        return cs.toString();
    }

Test Case:
----------

TestMethodExpressionImpl.java:

    @Test
    public void testInvokeOverridenParamAndReturnType() {
        MethodExpression me = factory.createMethodExpression(context,
                "${beanAA.getCharSequence('test')}", null ,
                new Class<?>[] { TesterBeanB.class });
        assertEquals("test", me.invoke(context, null));
    }

Note 1: The error also occurs if getCharSequence(..) in TesterBeanA is abstract.
I suggest adding a check for isAbstract() upon reading method modifiers and drop abstract method right away.

Note 2: This worked in Tomcat 7.0.40.
Comment 1 Christopher Schultz 2014-07-31 17:54:59 UTC
That's because the method is ambiguous. Even Java can't figure it out.

Something is wrong with your sample code: it won't compile.

public class AmbiguousTest
{
    public static class TesterBeanA {
        public CharSequence getCharSequence(CharSequence cs) {
          return cs;
        }
    }

    public static class TesterBeanAA {

        @Override
        public String getCharSequence(CharSequence cs) {
            return cs.toString();
        }
    }
    public static void main(String[] args) {
        new TesterBeanAA().getCharSequence("test");
    }
}

The code above will not compile under Java 8:
$ javac AmbiguousTest.java 
AmbiguousTest.java:11: error: method does not override or implement a method from a supertype
        @Override
        ^
1 error

You may have to come up with a better test case.
Comment 2 S. Dabek 2014-07-31 19:27:16 UTC
The test-code is fine. Of course, it won't compile if you don't extend TesterBeanAA from TesterBeanA.
Just add the code to the test-classes of the same name in the tomcat7 trunk.

And of course an overriden method can't be ambiguous. That's the essence of polymorphism.
Comment 3 Christopher Schultz 2014-08-03 13:16:32 UTC
Aw, crap. I missed that, obviously. It's always easier to run a unit test when it comes in a self-contained example. ;)
Comment 4 Mark Thomas 2014-08-05 11:24:38 UTC
Thanks for the report. This has been fixed in 8.0.x for 8.0.11 onwards and in 7.0.x for 7.0.56 onwards.