Difficulty with ImportHandler#importClass()
pgarner Oct 26, 2014 1:29 PMThe following bean seems to load fine upon startup and when facelets are loaded, as evidenced by the log:
@ManagedBean(eager=true)
@ApplicationScoped
public class EnumConfig {
    @PostConstruct
    public void init() {
        System.out.println("Testing 123...");
        FacesContext.getCurrentInstance().getApplication().addELContextListener (
            new ELContextListener() {
         
                @Override
                public void contextCreated(ELContextEvent event) {
             
                    System.out.println("Testing 456...");
                    ImportHandler importHandler = event.getELContext().getImportHandler();
                    importHandler.importClass("foo.bar.business.model.enumeration.YesOrNo");
                    Class<?> clazz = importHandler.resolveClass("YesOrNo");
                    System.out.println("class = " + clazz);
                }
            }
        );
    }
}
21:26:48,703 INFO [stdout] (MSC service thread 1-4) Testing 123... 21:39:11,976 INFO [stdout] (default task-9) Testing 456... 21:39:11,976 INFO [stdout] (default task-18) class = null
YesOrNo is not imported because clazz == null in EnumConfig code snippet. This is a problem.  And as expected the value expression can't resolve YesOrNo#values(). The select list is rendered empty, with no YesOrNo values.
<f:selectItems
    value="#{YesOrNo.values()}"
    var="yesOrNo"
    itemValue="#{yesOrNo}"
    itemLabel="#{yesOrNo.name()}"/>
I set a breakpoint after ImportHandler#importClass and I can see that classNameMap is properly populated:
importHandler = {javax.el.ImportHandler@17821}
classNameMap = {java.util.HashMap@17822} size = 1
[0] = {java.util.HashMap$Node@17830}"YesOrNo" -> "foo.bar.business.model.enumeration.YesOrNo"
classMap = {java.util.HashMap@17823} size = 0
staticNameMap = {java.util.HashMap@17824} size = 0
notAClass = {java.util.HashSet@17825} size = 0
packages = {java.util.ArrayList@17826} size = 1
[0] = {java.lang.String@17286}"java.lang"
value = {char[9]@17287}
hash = -888658374
So, classNameMap is populated correctly but the import handler cannot resolve the class. The JavaDoc for ImportHandler#resolveClass() indicates I am using it correctly (without package name). Why is import handler unable to resolve the class?
FYI, using <o:importConstants> of OmniFaces works just fine.
<o:importConstants type="foo.bar.business.model.enumeration.YesOrNo"/>
What can I do to further troubleshoot this problem?
 
    