(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
我不觉得自己多勇敢
亲
早晚会还给历史公正的评价的