java - Can you access private variables using OGNL? -
is there anyway using ognl access private variables aren't exposed bean property (ie no get/set method pair)? wanted use ognl faster, cleaner method of reflection use in unit tests.
here code:
@test public void shouldsetup() throws ognlexception{ class a{ private object b = "foo"; private object c = "bar"; public object getb(){ return b; } } a = new a(); system.out.println( "this'll work..." ); ognl.getvalue( "b", ); system.out.println( "...and this'll fail..." ); ognl.getvalue( "c", ); system.out.println( "...and we'll never here." ); }
actually can. need set memberaccess in ognlcontext allow access non public fields , use getvalue(expressionaccessor expression, ognlcontext context, object root) method retrieve value.
@test public void shouldsetup() throws ognlexception { class { private object b = "foo"; private object c = "bar"; public object getb() { return b; } } a = new a(); // set defaultmemberaccess allowed access context ognlcontext context = new ognlcontext(); context.setmemberaccess(new defaultmemberaccess(true)); system.out.println( "this'll work..." ); // use context in getvalue method ognl.getvalue( "b", context, ); system.out.println( "...and this'll work..." ); // use context in getvalue method ognl.getvalue( "c", context, ); system.out.println( "...and we'll here." ); }
Comments
Post a Comment