public Element create(final Object bean, final Class genArgType)
throws CoreException, ElementCreationException {
final Class<?> clazz = bean.getClass();
logger.debug("class {}", clazz.getCanonicalName());
final ElementType elType; // NOPMD PMD: AvoidFinalLocalVariableby wim on
// 9/20/10 2:59 PM
if (bean instanceof java.util.List) {
logger.debug("bean {} instance of list<{}>", clazz.getCanonicalName(),
genArgType.getCanonicalName() + ">");
elType = getElementType(genArgType.getCanonicalName());
} else {
elType = getElementType(clazz.getCanonicalName());
}
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)