您可以使用OGNL访问私有变量吗?



是否有使用OGNL访问不作为bean属性暴露的私有变量(即没有get/set方法对)?我想使用OGNL作为一种在单元测试中使用的更快、更清晰的反射方法。

下面是我的代码:
@Test
public void shouldSetup() throws OgnlException{
    class A{
        private Object b = "foo";
        private Object c = "bar";
        public Object getB(){ return b; }
    }
    A a = new A();
    System.out.println( "This'll work..." );
    Ognl.getValue( "b", a );
    System.out.println( "...and this'll fail..." );
    Ognl.getValue( "c", a );
    System.out.println( "...and we'll never get here." );
}

其实你可以。需要在OgnlContext中设置MemberAccess,允许访问非公共字段,使用getValue(ExpressionAccessor expression, OgnlContext context, Object root)方法检索值。

@Test
public void shouldSetup() throws OgnlException {
    class A {
        private Object b = "foo";
        private Object c = "bar";
        public Object getB() { return b; }
    }
    A a = new A();
    // set DefaultMemberAccess with allowed access into the 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, a );
    System.out.println( "...and this'll work..." );
    // use context in getValue method
    Ognl.getValue( "c", context, a );
    System.out.println( "...and we'll get here." );
}

我能做的最好的工作是直接使用反射:

static Object getValue( final String ognlPath, final Object o ){
    final StringTokenizer st = new StringTokenizer( ognlPath, "." );
    Object currentO = o;
    String nextFieldName = null;
    try{
        while( st.hasMoreTokens() ){
            nextFieldName = st.nextToken();
            if( currentO == null ){
                throw new IllegalStateException( "Cannot find field '" + nextFieldName + "' on null object." );
            }
            Field f = currentO.getClass().getDeclaredField( nextFieldName );
            f.setAccessible( true );
            currentO = f.get( currentO );
        }
        return currentO;
    }catch( NoSuchFieldException e ){
        throw new RuntimeException( "Could not find field '" + nextFieldName + "' on " + currentO.getClass().getCanonicalName(), e );
    }catch( IllegalAccessException e ){
        throw new RuntimeException( "Failed to get from path '" + ognlPath + "' on object: " + o, e );
    }
}

相关内容

  • 没有找到相关文章

最新更新