Recently, I had this error when developping a JSP page:

All the ReferentielBeanXXX extend the same class ReferentielBeanBase. In my JSP page, I just want to check:

    if ((ref instanceof fr.acmn.simu.ReferentielBeanIntermediation)) {
	if (ref.isSimuPossible()) {
            ...
        }
    }


and I got an error message with this statement and the message is:

    Impossible for fr.acmn.simu.ReferentielBeanMS to be instance of 
fr.acmn.simu.ReferentielBeanIntermediation.
    at com.caucho.java.JavaCompiler.executeInt(JavaCompiler.java:504)
    at com.caucho.java.JavaCompiler.compileInt(JavaCompiler.java:436)
    at com.caucho.java.JavaCompiler.compile(JavaCompiler.java:419)
    at com.caucho.jsp.JavaGenerator.compile(JavaGenerator.java:2424)
    at com.caucho.jsp.JspGenerator.generate(JspGenerator.java:273)
    at com.caucho.jsp.JspParser.parse(JspParser.java:236)
    at com.caucho.jsp.JspParser.parse(JspParser.java:142)
    at com.caucho.jsp.JspManager.createPage(JspManager.java:155)
    at com.caucho.jsp.PageManager.getPage(PageManager.java:340)
    at com.caucho.jsp.PageManager.getPage(PageManager.java:195)
    at com.caucho.jsp.QServlet.getPage(QServlet.java:220)
    at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:128)
    at com.caucho.server.http.Invocation.service(Invocation.java:288)
    at com.caucho.server.http.QRequestDispatcher.forward(QRequestDispatcher.java:214)
    at com.caucho.server.http.QRequestDispatcher.forward(QRequestDispatcher.java:99)
    at com.caucho.server.http.QRequestDispatcher.forward(QRequestDispatcher.java:76)


The method isSimuPossible() is specific of ‘ReferentielBeanIntermediation’, but not of ReferentielBeanBase. It doesn’t work. Even if I use

    if (ref.getClass().getName().equals(
            "fr.acmn.simu.ReferentielBeanIntermediation")) {
	if (ref.isSimuPossible()) {
           ...
        }
    }


the JSP compiler does not accept because isSimuPossible() is not a method of ReferentielBeanBase.

In fact, the only way (?) that I know to make this code work is using Reflection, like that:

if (ref.getClass().getName().equals(
        "fr.acmn.simu.ReferentielBeanIntermediation")) {
    Boolean isSimuDispo = 
          (Boolean) ref.getClass().getMethod("isSimuPossible", null)
                              .invoke(ref, new Object[0]);
    if (isSimuDispo.booleanValue()) {
        ....
    }
}

Advertisements