📄 beanconverter.java
字号:
Method setter = null;
PropertyDescriptor descriptor = (PropertyDescriptor) props.get(key);
if (descriptor != null)
{
setter = descriptor.getWriteMethod();
}
if (setter == null)
{
log.warn("setter method for property " + key + " is not visible to DWR."); //$NON-NLS-1$ //$NON-NLS-2$
log.info("You can add a public set" + Character.toTitleCase(key.charAt(0)) + key.substring(1) + "() method, or switch to using the ObjectConverter to read from members directly."); //$NON-NLS-1$ //$NON-NLS-2$
}
else
{
Class propType = descriptor.getPropertyType();
String[] split = LocalUtil.splitInbound(val);
String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
TypeHintContext incc = new TypeHintContext(setter, 0);
Object output = config.convertInbound(propType, nested, inctx, incc);
setter.invoke(bean, new Object[] { output });
}
}
return bean;
}
catch (ConversionException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new ConversionException(ex);
}
}
/* (non-Javadoc)
* @see uk.ltd.getahead.dwr.Converter#convertOutbound(java.lang.Object, java.lang.String, uk.ltd.getahead.dwr.OutboundContext)
*/
public String convertOutbound(Object data, String varname, OutboundContext outctx) throws ConversionException
{
StringBuffer buffer = new StringBuffer();
buffer.append("var "); //$NON-NLS-1$
buffer.append(varname);
buffer.append("={};"); //$NON-NLS-1$
try
{
BeanInfo info = getBeanInfo(data);
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
for (int i = 0; i < descriptors.length; i++)
{
PropertyDescriptor descriptor = descriptors[i];
String name = descriptor.getName();
try
{
// We don't marshall things we can't read
Method getter = descriptor.getReadMethod();
if (getter == null)
{
continue;
}
// We don't marshall getClass()
if (name.equals("class")) //$NON-NLS-1$
{
continue;
}
// Access rules mean we might not want to do this one
if (!isAllowed(name))
{
log.debug("Skipping marshalling " + name + " due to include/exclude rules"); //$NON-NLS-1$ //$NON-NLS-2$
continue;
}
if (!isAvailable(data, name))
{
log.debug("Skipping marshalling " + name + " due to availability rules"); //$NON-NLS-1$ //$NON-NLS-2$
continue;
}
Object value = getter.invoke(data, new Object[0]);
OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
// Make sure the nested thing is declared
buffer.append(nested.getInitCode());
// And now declare our stuff
buffer.append(varname);
buffer.append('.');
buffer.append(name);
buffer.append('=');
buffer.append(nested.getAssignCode());
buffer.append(';');
// In an attempt to work around a FF1.4 bug we split long lines
if (i % 10 == 0)
{
buffer.append('\n');
}
}
catch (Exception ex)
{
log.warn("Failed to convert " + name, ex); //$NON-NLS-1$
}
}
}
catch (IntrospectionException ex)
{
throw new ConversionException(ex);
}
buffer.append('\n');
return buffer.toString();
}
/**
* HibernateBeanConverter (and maybe others) may want to provide alternate
* versions of bean.getClass()
* @param bean The class to find bean info from
* @return BeanInfo for the given class
* @throws IntrospectionException
*/
protected BeanInfo getBeanInfo(Object bean) throws IntrospectionException
{
BeanInfo info = Introspector.getBeanInfo(bean.getClass());
return info;
}
/**
* Check with the access rules to see if we are allowed to convert a property
* @param property The property to test
* @return true if the property may be marshalled
*/
protected boolean isAllowed(String property)
{
if (exclusions != null)
{
// Check each exclusions and return false if we get a match
for (Iterator it = exclusions.iterator(); it.hasNext();)
{
String test = (String) it.next();
if (property.equals(test))
{
return false;
}
}
// So we passed all the exclusions. The setters enforce mutual
// exclusion between exclusions and inclusions so we don't need to
// 'return true' here, we can carry on. This has the advantage that
// we can relax the mutual exclusion at some stage.
}
if (inclusions != null)
{
// Check each inclusion and return true if we get a match
for (Iterator it = inclusions.iterator(); it.hasNext();)
{
String test = (String) it.next();
if (property.equals(test))
{
return true;
}
}
// Since we are white-listing with inclusions and there was not
// match, this property is not allowed.
return false;
}
// default to allow if there are no inclusions or exclusions
return true;
}
/**
* Some child converters (like Hibernate at least) need to check that a
* property should be marshalled. This allows them to veto a marshal
* @param data The object to check on
* @param property The property of the <code>data</code> object
* @return true if we should continue and marshall it.
*/
public boolean isAvailable(Object data, String property)
{
// This just shuts the eclipse lint up
if (false) { data = property; property = (String) data; }
return true;
}
/**
* The list of excluded properties
*/
private List exclusions = null;
/**
* The list of included properties
*/
private List inclusions = null;
/**
* A type that allows us to fulfill an interface or subtype requirement
*/
private Class instanceType = null;
/**
* The log stream
*/
private static final Logger log = Logger.getLogger(BeanConverter.class);
/**
* To forward marshalling requests
*/
private ConverterManager config = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -