Ok Matt, the issue link is http://issues.appfuse.org/browse/APF-563?page=all

Thanks,
André Faria

Matt Raible escreveu:
Can you please enter an issue for this in JIRA (http://issues.appfuse.org/browse/APF)?  Even better, attach a patch. ;-)

Thanks,

Matt

On 12/30/06, André Faria <[EMAIL PROTECTED]> wrote:
I resolved this way

public class DaoUtils {

    protected static final Log log = LogFactory.getLog(DaoUtils.class);
    private static final String GET_INITIALS = "get";

    @SuppressWarnings("unchecked")
    public static Object getPersistentId(Object o) throws PersistenceException {
        Object objId = null;
        final String eMsg = "Error executing get<IdValue> method on persistent class.";
        String logMsg = "Persistent identity for object of type '?1' is accessible with method '?2'";

        try {
            final AccessibleObject annotatedAccessibleObject = getAnnotatedAccessibleObject(o, Id.class, EmbeddedId.class);

            if (annotatedAccessibleObject != null) {
                Method getter = null;
                if (annotatedAccessibleObject instanceof Method) {
                    getter = (Method) annotatedAccessibleObject;
                } else if (annotatedAccessibleObject instanceof Field) {
                    getter = findGetter(o.getClass(), ((Field) annotatedAccessibleObject).getName());
                }

                objId = getter.invoke(o);
                if (log.isDebugEnabled()) {
                    logMsg = logMsg.replace("?1", o.getClass().getName());
                    logMsg = logMsg.replace("?2", getter.getName());
                    log.debug(logMsg);
                }
            }
        } catch (IllegalArgumentException e) {
            throw new PersistenceException(eMsg, e);
        } catch (IllegalAccessException e) {
            throw new PersistenceException(eMsg, e);
        } catch (InvocationTargetException e) {
            throw new PersistenceException(eMsg, e);
        } catch (SecurityException e) {
            throw new PersistenceException(eMsg, e);
        } catch (NoSuchMethodException e) {
            throw new PersistenceException("There is no getter method for " + o.getClass().getSimpleName() + " ID property", e);
        }

        return objId;
    }

    private static AccessibleObject getAnnotatedAccessibleObject(Object o, Class<? extends Annotation>... annotations) throws PersistenceException {

        final Set<AccessibleObject> members = new HashSet<AccessibleObject>();
        members.addAll(Arrays.asList(o.getClass().getDeclaredMethods()));
        members.addAll(Arrays.asList(o.getClass().getDeclaredFields()));

        for (AccessibleObject member : members) {
            for (Class<? extends Annotation> annotation : annotations)
                if (member.isAnnotationPresent(annotation))
                    return member;
        }

        return null;
    }

    private static Method findGetter(Class type, String property) throws SecurityException, NoSuchMethodException {
        String methodName = GET_INITIALS + Character.toUpperCase(property.charAt(0)) + property.substring(1);
        try {
            return type.getMethod(methodName);
        } catch (SecurityException e) {
            throw e;
        } catch (NoSuchMethodException e) {
            throw e;
        }
    }

}



but I think that the last two  methods shold be in ReflectionUtils..
what do you think?


André Faria




Matt Raible escreveu:
Bryan wrote the code for this and he's on vacation this week.  Do you
have a suggestion on how to fix this?

Matt

On 12/29/06, André Faria <[EMAIL PROTECTED]> wrote:
       The method getPersistentId in DaoUtils do not work if the annotations
were int the field...

[EMAIL PROTECTED]
 private int personKey;



    --------------------------------------------------------------------- To
unsubscribe, e-mail: [EMAIL PROTECTED] For additional
commands, e-mail: [EMAIL PROTECTED]



public class DaoUtils {

       protected static final Log log = LogFactory.getLog(DaoUtils.class);
       private static final String GET_INITIALS = "get";

      [EMAIL PROTECTED]("unchecked")
       public static Object getPersistentId(Object o) throws PersistenceException {
               Object objId = null;
               final String eMsg = "Error executing get<IdValue> method on persistent class.";
               String logMsg = "Persistent identity for object of type '?1' is accessible with method '?2'";

               try {
                       final AccessibleObject annotatedAccessibleObject = getAnnotatedAccessibleObject(o, Id.class, EmbeddedId.class);

                       if (annotatedAccessibleObject != null) {
                               Method getter = null;
                               if (annotatedAccessibleObject instanceof Method) {
                                       getter = (Method) annotatedAccessibleObject;
                               } else if (annotatedAccessibleObject instanceof Field) {
                                       getter = findGetter( o.getClass(), ((Field) annotatedAccessibleObject).getName());
                               }

                               objId = getter.invoke(o);
                               if (log.isDebugEnabled ()) {
                                       logMsg = logMsg.replace("?1", o.getClass().getName());
                                       logMsg = logMsg.replace("?2", getter.getName());
                                        log.debug(logMsg);
                               }
                       }
               } catch (IllegalArgumentException e) {
                       throw new PersistenceException(eMsg, e);
               } catch (IllegalAccessException e) {
                       throw new PersistenceException(eMsg, e);
               } catch (InvocationTargetException e) {
                       throw new PersistenceException(eMsg, e);
               } catch (SecurityException e) {
                       throw new PersistenceException(eMsg, e);
               } catch (NoSuchMethodException e) {
                       throw new PersistenceException("There is no getter method for " + o.getClass().getSimpleName() + " ID property", e);
               }

               return objId;
       }

       private static AccessibleObject getAnnotatedAccessibleObject(Object o, Class<? extends Annotation>... annotations) throws PersistenceException {

               final Set<AccessibleObject> members = new HashSet<AccessibleObject>();
               members.addAll(Arrays.asList(o.getClass().getDeclaredMethods()));
               members.addAll (Arrays.asList(o.getClass().getDeclaredFields()));

               for (AccessibleObject member : members) {
                       for (Class<? extends Annotation> annotation : annotations)
                               if ( member.isAnnotationPresent(annotation))
                                       return member;
               }

               return null;
       }

       private static Method findGetter(Class type, String property) throws SecurityException, NoSuchMethodException {
               String methodName = GET_INITIALS + Character.toUpperCase(property.charAt(0)) + property.substring(1);
               try {
                       return type.getMethod(methodName);
               } catch (SecurityException e) {
                       throw e;
               } catch (NoSuchMethodException e) {
                       throw e;
               }
       }

}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




--
http://raibledesigns.com
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to