📄 objectconverter.java
字号:
{
all.append(',');
}
}
log.warn("Fields exist for (" + all + ")."); //$NON-NLS-1$ //$NON-NLS-2$
}
else
{
Class propType = field.getType();
String[] split = LocalUtil.splitInbound(val);
String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
if (!field.isAccessible())
{
if (force)
{
field.setAccessible(true);
}
else
{
log.debug("Field: " + field.getName() + " is not accessible. use <param name='force' value='true'/>"); //$NON-NLS-1$ //$NON-NLS-2$
continue;
}
}
Object output = config.convertInbound(propType, nested, inctx, inctx.getCurrentTypeHintContext());
field.set(bean, new Object[] { output });
}
}
return bean;
}
catch (MarshallException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new MarshallException(ex);
}
}
/* (non-Javadoc)
* @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
*/
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException
{
// Where we collect out converted children
Map ovs = new TreeMap();
OutboundVariable ov = outctx.createOutboundVariable(data);
try
{
Field[] fields = getAllFields(data);
for (int i = 0; i < fields.length; i++)
{
Field field = fields[i];
String name = field.getName();
try
{
// 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;
}
if (!field.isAccessible())
{
if (force)
{
field.setAccessible(true);
}
else
{
log.debug("Field: " + field.getName() + " is not accessible. use <param name='force' value='true'/>"); //$NON-NLS-1$ //$NON-NLS-2$
continue;
}
}
Object value = field.get(data);
OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
ovs.put(name, nested);
}
catch (Exception ex)
{
log.warn("Failed to convert " + name, ex); //$NON-NLS-1$
}
}
}
catch (Exception ex)
{
throw new MarshallException(ex);
}
ConverterUtil.addMapInit(ov, ovs);
return ov;
}
/**
* Get the fields from a bean. You can't use <code>class.getFields()</code>
* in place of this because it only gives you accessible fields, and
* although <code>class.getDeclaredFields()</code> does give in-accessible
* fields is doesn't walk up the tree.
* @param bean The class to find bean info from
* @return An array of all the fields for the given object
*/
protected Field[] getAllFields(Object bean)
{
Set allFields = new HashSet();
Class clazz = bean.getClass();
while (clazz != Object.class)
{
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
allFields.add(fields[i]);
}
clazz = clazz.getSuperclass();
}
return (Field[]) allFields.toArray(new Field[allFields.size()]);
}
/**
* 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)
{
return true;
}
/**
* Do we force accessibillity for hidden fields
*/
private boolean force;
/**
* 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(ObjectConverter.class);
/**
* To forward marshalling requests
*/
private ConverterManager config = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -