jsf - Able to import constants using OmniFaces but not ImportHandler#importClass() -
the next bean loads eagerly e.g.
@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..."); event.getelcontext().getimporthandler().importclass( "foo.bar.business.model.enumeration.yesorno"); //importhandler.importpackage( // "foo.bar.business.model.enumeration.preference"); class<?> clazz = importhandler.resolveclass("yesorno"); system.out.println("clazz = " + 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...
i set breakpoint , can see "foo.bar.business.model.enumeration.yesorno" entered classnamemap
. sec entry made in notaclass
, indicating yesorno
not found, not good.
importhandler = {javax.el.importhandler@15909} classnamemap = {java.util.hashmap@15910} size = 1 [0] = {java.util.hashmap$node@15923}"yesorno" -> "foo.bar.business.model.enumeration.yesorno" classmap = {java.util.hashmap@15911} size = 0 staticnamemap = {java.util.hashmap@15912} size = 0 notaclass = {java.util.hashset@15913} size = 1 [0] = {java.lang.string@15030}"foo.bar.business.model.enumeration.yesorno" packages = {java.util.arraylist@15914} size = 1 [0] = {java.lang.string@15926}"java.lang"
21:39:11,976 info [stdout] (default task-18) clazz = null
here's javadoc importhandler#resolveclass()
, seems indicate using correctly (without bundle name):
public class resolveclass(string name)
resolve class name.
parameters:
name
- name of class (without bundle name) resolved.
returns:
if class has been imported previously, importclass(java.lang.string) or importpackage(java.lang.string), class instance. otherwise null.
throws:
elexception - if class abstract or interface, or not public.
referring el 3.0 spec.:
1.22.2 imports of packages, classes, , static fields
either class or bundle can explicitly imported el evaluation environment. importing bundle imports classes in package. classes can imported restricted classes can loaded current class loader.
by default, next packages imported el environment.
java.lang.*
a static field can imported statically. statically imported static field can referenced field name, without classname.
the imports of packages, classes, , static fields handled importhandler in elcontext.
here's importhandler relevant code(see github):
private class<?> getclassfor(string classname) { if (!notaclass.contains(classname)) { seek { homecoming class.forname(classname, false, getclass().getclassloader()); } grab (classnotfoundexception ex) { notaclass.add(classname); } } homecoming null; }
using <o:importconstants>
of omnifaces works fine:
<o:importconstants type="foo.bar.business.model.enumeration.yesorno"/>
here's omnifaces approach, using taghandler (see github):
/** * convert given type, should represent qualified name, concrete {@link class} instance. * @param type qualified name of class. * @return concrete {@link class} instance. * @throws illegalargumentexception when missing in classpath. */ static class<?> toclass(string type) { // package-private importfunctions can utilize it. seek { homecoming class.forname(type, true, thread.currentthread().getcontextclassloader()); } grab (classnotfoundexception e) { // perhaps it's inner enum specified com.example.someclass.someenum. // let's lenient on although proper type notation should com.example.someclass$someenum. int = type.lastindexof('.'); if (i > 0) { seek { homecoming toclass( new stringbuilder(type.substring(0, i)).append('$').append(type.substring(i + 1)).tostring()); } grab (illegalargumentexception ignore) { // go on illegalargumentexception on original classnotfoundexception. } } throw new illegalargumentexception(string.format(error_missing_class, type), e); } }
so javax.el.importhandler
invokes getclass().getclassloader()
whereas org.omnifaces.taghandler.importconstants
invokes thread.currentthread().getcontextclassloader()
.
shouldn't javax.el.importhandler
utilize current thread class loader can load classes in web-inf/classes? javax.el.importhandler
satisfy el spec. requirement the classes can imported restricted classes can loaded current class loader? don't know whether or not current class loader means thread context class loader associated current thread.
jsf el java-ee-7 wildfly-8
No comments:
Post a Comment