31

I upgraded my Java EE web application to use newer PrimeFaces version and suddenly the call of an overloaded bean method in an action attribute of PrimeFaces commandlink did not work anymore. I tried to use JSF default commandlink to test it and this one did not work either.

The method signatures are as follows:

public void updateA(B b);
public void updateA(A a);

It always tried to cast A to B.

More curious, how could it work before the upgrade?

3 Answers 3

43

EL does not support it, no. It'll always be the first method of the Class#getMethods() array whose name (and amount of arguments) matches the EL method call. Whether it returns the same method everytime or not depends on the JVM make/version used. Perhaps you made a Java SE upgrade in the meanwhile as well. The javadoc even says this:

The elements in the array returned are not sorted and are not in any particular order.

You should not rely on unspecified behaviour. Give them a different name.

5
  • 1
    Is there any (technical) reason why this is not supported in JSF 2 any more? I'm pretty sure that this was possible in JSF 1.x versions, wasn't it?
    – MrD
    Commented Mar 6, 2014 at 14:53
  • This is not JSF specific.
    – BalusC
    Commented Mar 6, 2014 at 15:11
  • I had an equal problem, but in my case not always the first one was used. It seems to be quite randomly chosen.
    – Alex VII
    Commented Aug 4, 2014 at 12:36
  • 2
    @alexvii: as answered, the getMethods() doesn't necessarily return a fixed order.
    – BalusC
    Commented Aug 4, 2014 at 15:00
  • 7
    The fact that the searching for the most accurate overloaded method does not work with EL, sucks.
    – Jagger
    Commented Mar 5, 2015 at 9:44
4

The way you can get around this is to create a generic method and do the 'routing' inside that method. I know that this might not be ideal, but you end up with less configurations in functions and XHTML pages.

if (A.class.isInstance(obj)) {
    A o = (A) obj;
    return method(o, highRes);
} else if (B.class.isInstance(obj)) {
    B o = (B) obj;
    return method(o, highRes);
} else if (C.class.isInstance(obj)) {
    C o = (C) obj;
    return method(o, highRes);
} else {
    throw new FacesException("Unsupported Conversion: " + obj);
}
1
  • 2
    Not ideal at all, in fact, it's a terrible suggestion. Besides some very specific cases (such as the implementation of the equals() method), you should never use this type of comparisons. Quoting Scott Meyers, from Effective C++: 'Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself'. Commented Oct 1, 2018 at 13:22
0

In fact, the org.glassfish.javax.el jars support overloading, as opposed to com.sun.el, so use this in your project:

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.1-b08</version>
        </dependency>

Instead of:

        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>el</artifactId>
            <version>2.2.0</version>
        </dependency>

(check the versions appropriate for your project, those above are just examples)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.