📄 beanmodel.java
字号:
return model;
}
Object fd = classInfo.get(key);
if(fd != null) {
retval = invokeThroughDescriptor(fd, classInfo);
if (retval == UNKNOWN && model == nullModel) {
// This is the (somewhat subtle) case where the generic get() returns null
// and we have no bean info, so we respect the fact that
// the generic get() returns null and return null. (JR)
retval = nullModel;
}
}
}
if (retval == UNKNOWN) {
if (wrapper.isStrict()) {
throw new InvalidPropertyException("No such bean property: " + key);
} else if (logger.isDebugEnabled()) {
logNoSuchKey(key, classInfo);
}
retval = wrapper.wrap(null);
}
return retval;
}
catch(TemplateModelException e)
{
throw e;
}
catch(Exception e)
{
throw new TemplateModelException("get(" + key + ") failed on " +
"instance of " + object.getClass().getName(), e);
}
}
private void logNoSuchKey(String key, Map keyMap)
{
logger.debug("Key '" + key + "' was not found on instance of " +
object.getClass().getName() + ". Introspection information for " +
"the class is: " + keyMap);
}
/**
* Whether the model has a plain get(String) or get(Object) method
*/
protected boolean hasPlainGetMethod() {
return wrapper.getClassKeyMap(object.getClass()).get(BeansWrapper.GENERIC_GET_KEY) != null;
}
private TemplateModel invokeThroughDescriptor(Object desc, Map classInfo)
throws
IllegalAccessException,
InvocationTargetException,
TemplateModelException
{
// See if this particular instance has a cached implementation
// for the requested feature descriptor
TemplateModel member = null;
synchronized(memberMap)
{
member = (TemplateModel)memberMap.get(desc);
}
if(member != null)
return member;
TemplateModel retval = UNKNOWN;
if(desc instanceof IndexedPropertyDescriptor)
{
Method readMethod =
((IndexedPropertyDescriptor)desc).getIndexedReadMethod();
retval = member =
new SimpleMethodModel(object, readMethod,
BeansWrapper.getArgTypes(classInfo, readMethod), wrapper);
}
else if(desc instanceof PropertyDescriptor)
{
PropertyDescriptor pd = (PropertyDescriptor)desc;
retval = wrapper.invokeMethod(object, pd.getReadMethod(), null);
// (member == null) condition remains, as we don't cache these
}
else if(desc instanceof Method)
{
Method method = (Method)desc;
retval = member = new SimpleMethodModel(object, method,
BeansWrapper.getArgTypes(classInfo, method), wrapper);
}
else if(desc instanceof MethodMap)
{
retval = member =
new OverloadedMethodModel(object, (MethodMap)desc, wrapper);
}
// If new cacheable member was created, cache it
if(member != null)
{
synchronized(memberMap)
{
memberMap.put(desc, member);
}
}
return retval;
}
protected TemplateModel invokeGenericGet(Map keyMap, Class clazz, String key)
throws
IllegalAccessException,
InvocationTargetException,
TemplateModelException
{
Method genericGet = (Method)keyMap.get(BeansWrapper.GENERIC_GET_KEY);
if(genericGet == null)
return UNKNOWN;
return wrapper.invokeMethod(object, genericGet, new Object[] { key });
}
protected TemplateModel wrap(Object obj)
throws TemplateModelException
{
return wrapper.getOuterIdentity().wrap(obj);
}
protected Object unwrap(TemplateModel model)
throws
TemplateModelException
{
return wrapper.unwrap(model);
}
/**
* Tells whether the model is empty. It is empty if either the wrapped
* object is null, or it is a Boolean with false value.
*/
public boolean isEmpty()
{
if (object instanceof String) {
return ((String) object).length() == 0;
}
if (object instanceof Collection) {
return ((Collection) object).isEmpty();
}
if (object instanceof Map) {
return ((Map) object).isEmpty();
}
return object == null || Boolean.FALSE.equals(object);
}
public Object getAdaptedObject(Class hint) {
return object;
}
public Object getWrappedObject() {
return object;
}
public int size()
{
return wrapper.keyCount(object.getClass());
}
public TemplateCollectionModel keys()
{
return new CollectionAndSequence(new SimpleSequence(keySet(), wrapper));
}
public TemplateCollectionModel values() throws TemplateModelException
{
List values = new ArrayList(size());
TemplateModelIterator it = keys().iterator();
while (it.hasNext()) {
String key = ((TemplateScalarModel)it.next()).getAsString();
values.add(get(key));
}
return new CollectionAndSequence(new SimpleSequence(values, wrapper));
}
public String toString() {
return object.toString();
}
/**
* Helper method to support TemplateHashModelEx. Returns the Set of
* Strings which are available via the TemplateHashModel
* interface. Subclasses that override <tt>invokeGenericGet</tt> to
* provide additional hash keys should also override this method.
*/
protected Set keySet()
{
return wrapper.keySet(object.getClass());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -