}
logger.debug("creating Element ({})", elType.toString());
if (beanElementMap.containsKey(bean)) {
return beanElementMap.get(bean); // NOPMD by wim on 9/20/10 2:54 PM
}
final Element element = new Element(elType);
beanElementMap.put(bean, element);
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
public void doWith(final Method method) throws IllegalArgumentException,
IllegalAccessException {
try {
logger.debug("Method is {}", method.getName());
if (((method.getName().startsWith("get") &&
method.getName().length() > 3) ||
(method.getName().startsWith("is") &&
method.getName().length() > 2)) &&
!method.getName().equals("getClass")) {
final Class retType = method.getReturnType();
logger.debug("checking method return type {}",
retType.getCanonicalName());
if (isPrimitiveType(retType)) {
String propName = getPropertyName(method);
logger.debug("property name {} type {}", propName,
retType.getCanonicalName());
if( elType.isBeanId( propName )){
if( method.getReturnType().equals( Long.class)){
element.setId( (Long)method.invoke(bean));
}
else if( method.getReturnType().equals( String.class)){
String id = (String)method.invoke(bean);
if( id!= null ){
element.setId( new Long(id));
}
else {
element.setId( null );
}
}
}
else if( elType.isBeanVersionId(propName)){
Long v = 0L;
if (retType.equals(java.lang.Integer.class)
|| retType.getCanonicalName().equals("int")) {
v = new Long((Integer)method.invoke(bean));
// above line as suggested by FB: DM_NUMBER_CTOR v = new Long((Integer)
// (value));
} else if (retType.equals(java.lang.Long.class)
|| retType.getCanonicalName().equals("long")) {
v = (Long) method.invoke(bean);
}
element.setVersion(v); // property must be Int of Long!!
}
else {
setProperty(propName, method.invoke(bean), retType, element);
}
} else {
Class genArgType = null;
String type = retType.getCanonicalName();
if (method.getReturnType().equals(java.util.List.class)) {
genArgType = getGenericArgType(method.getGenericReturnType());
type = genArgType.getCanonicalName();
}
logger.debug("composite {}", type);
String propName = getPropertyName(method);
logger.debug("about to add reference {}", propName);
final Object ref = method.invoke(bean);
if (ref != null) {
try {
if (ref instanceof java.util.List) {
logger.debug("LIST SIZE {}",
((java.util.List) ref).size());
if (isPrimitiveType(genArgType)) {
logger.debug("LIST ELEMENTS TYPE {}",
genArgType.getCanonicalName());
setProperty(propName, (java.util.List) ref, genArgType,
element);
} else { // a list of composite types
logger.debug("adding list reference {}", propName);
final List<Element> elements = new ArrayList<Element>();
for (Object o : (java.util.List) ref) {
if (o != null) {
elements.add(create(o, genArgType));
}
}
logger.debug("end");
element.setListOfElements(propName, elements);
}
} else { // not a list
logger.debug("adding reference {}", propName);
element.addElement(propName, create(ref, genArgType));
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace(); // NOPMD by wim on 9/20/10 2:57 PM
} catch (ElementCreationException e) {