ognl_ognl用于什么_java(5)

电脑杂谈  发布时间:2017-02-16 12:32:16  来源:网络整理

(2)OgnlRuntime.getProperty( context, source, property )获取和属性property想对应的值

8、OgnlRuntime.getProperty( context, source, property )代码:

 public static final Object getProperty( OgnlContext context, Object source, Object name ) throws OgnlException
    {
        PropertyAccessor        accessor;

        if (source == null) {
            throw new OgnlException("source is null for getProperty(null, \"" + name + "\")");
        }
        if ((accessor = getPropertyAccessor(getTargetClass(source))) == null) {
            throw new OgnlException("No property accessor for " + getTargetClass(source).getName());
        }
        return accessor.getProperty( context, source, name );
    }
注释:(1)判断source是否为空,如果为空直接抛异常,因为后面需要在 source对象中获取一些信息,所以不能为空

(2)accessor = getPropertyAccessor(getTargetClass(source))获取属性访问器对象,然后调用属性访问器的getProperty方法获取值。先看看属性访问器是怎么获得?

8.1getTargetClass(source)代码:

  public static Class getTargetClass(Object o)
    {
        return (o == null) ? null : ((o instanceof Class) ? (Class)o : o.getClass());
    }

注释:获取target所对应的class,返回结果是:class Person

8.2getPropertyAccessor(getTargetClass(source)代码:

  private static ClassCache               propertyAccessors = new ClassCache();
 public static final PropertyAccessor getPropertyAccessor( Class cls ) throws OgnlException
    {
        PropertyAccessor answer = (PropertyAccessor)getHandler( cls, propertyAccessors );
        if ( answer != null )
            return answer;

        throw new OgnlException( "No property accessor for class " + cls );
    }


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-32760-5.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...