public void setFields(final List<FieldData> fields) {
List<FieldData> proxyFields = (List<FieldData>) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{List.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("add")) {
FieldData fieldData = (FieldData) args[0];
testFieldName(fieldData);
}
if (method.getName().equals("addAll")) {
Collection<FieldData> addCollection = (Collection<FieldData>) args[0];
Iterator<FieldData> itr = addCollection.iterator();
while (itr.hasNext()) {
FieldData fieldData = itr.next();
testFieldName(fieldData);
}
}
return method.invoke(fields, args);
}
private void testFieldName(FieldData fieldData) throws ClassNotFoundException {
if (!StringUtils.isEmpty(fieldData.getFieldName()) && dynamicEntityDao != null) {
Class<?>[] dtos = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(Class.forName(getDtoClassName()));
if (ArrayUtils.isEmpty(dtos)) {
dtos = new Class<?>[]{Class.forName(getDtoClassName())};
}
Field field = null;
for (Class<?> dto : dtos) {
field = dynamicEntityDao.getFieldManager().getField(dto, fieldData.getFieldName());
if (field != null) {
break;
}
}
if (field == null) {
throw new IllegalArgumentException("Unable to find the field declared in FieldData (" + fieldData.getFieldName() + ") on the target class (" + getDtoClassName() + "), or any registered entity class that derives from it.");
}
}
}
});
this.fields = proxyFields;