From: Erik Brakkee Date: Wed, 21 Jul 2010 10:48:18 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~237 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=0e34adc30ee8d46fe064646df7f967d71ff2e589;p=utils --- diff --git a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java index 2f5bb7c2..dd0d1839 100644 --- a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java +++ b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java @@ -16,10 +16,6 @@ package org.wamblee.persistence; import java.io.Serializable; -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -27,9 +23,7 @@ import javax.persistence.Id; import javax.persistence.Version; import org.wamblee.reflection.Accessor; -import org.wamblee.reflection.FieldAccessor; -import org.wamblee.reflection.PropertyAccessor; -import org.wamblee.reflection.ReflectionUtils; +import org.wamblee.reflection.AnnotationUtils; /** * Factory which creates a {@link Persistent} object for a given JPA entity for @@ -134,56 +128,14 @@ public class PersistentFactory { } private static EntityAccessor analyse(Class aClass) { - Accessor pk = analyse(aClass, Id.class); - Accessor version = analyse(aClass, Version.class); + Accessor pk = AnnotationUtils.analyse(aClass, Id.class); + Accessor version = AnnotationUtils.analyse(aClass, Version.class); if (pk != null || version != null) { return new EntityAccessor(pk, version); } return null; } - /** - * Returns the accessor for a given annotation. - * - * @param aClass - * Class to analyse. - * @param aAnnotation - * Annotation that must be present. - * @return Accessor to use or null if the annotation is not present. - */ - // TODO move generic analysis part to the reflection package. - public static Accessor analyse(Class aClass, - Class aAnnotation) { - List fields = ReflectionUtils.getAllFields(aClass); - for (Field field : fields) { - if (field.isAnnotationPresent(aAnnotation)) { - return new FieldAccessor(field); - } - } - List methods = ReflectionUtils.getAllMethods(aClass, - Object.class); - for (Method method : methods) { - if (method.isAnnotationPresent(aAnnotation)) { - String setterName = null; - if (method.getName().startsWith("get")) { - setterName = method.getName().replaceFirst("get", "set"); - } else if (method.getName().startsWith("is")) { - setterName = method.getName().replaceFirst("is", "set"); - } - try { - Class returnType = method.getReturnType(); - Method setter = method.getDeclaringClass() - .getDeclaredMethod(setterName, returnType); - return new PropertyAccessor(method, setter); - } catch (NoSuchMethodException e) { - throw new RuntimeException("Error obtaining setter for " + - method.getName() + " in class " + aClass.getName(), e); - } - } - } - return null; - } - /** * Creates the {@link Persistent} wrapper for interfacing with primary key * and version of the entity. diff --git a/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java b/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java index 5c701fb5..d31163fb 100644 --- a/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java +++ b/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java @@ -15,6 +15,11 @@ */ package org.wamblee.reflection; +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; + /** * Utlities for working with annotations. * @@ -23,4 +28,46 @@ package org.wamblee.reflection; */ public class AnnotationUtils { + /** + * Returns the accessor for a given annotation. + * + * @param aClass + * Class to analyse. + * @param aAnnotation + * Annotation that must be present. + * @return Accessor to use or null if the annotation is not present. + */ + // TODO move generic analysis part to the reflection package. + public static Accessor analyse(Class aClass, + Class aAnnotation) { + List fields = ReflectionUtils.getAllFields(aClass); + for (Field field : fields) { + if (field.isAnnotationPresent(aAnnotation)) { + return new FieldAccessor(field); + } + } + List methods = ReflectionUtils.getAllMethods(aClass, + Object.class); + for (Method method : methods) { + if (method.isAnnotationPresent(aAnnotation)) { + String setterName = null; + if (method.getName().startsWith("get")) { + setterName = method.getName().replaceFirst("get", "set"); + } else if (method.getName().startsWith("is")) { + setterName = method.getName().replaceFirst("is", "set"); + } + try { + Class returnType = method.getReturnType(); + Method setter = method.getDeclaringClass() + .getDeclaredMethod(setterName, returnType); + return new PropertyAccessor(method, setter); + } catch (NoSuchMethodException e) { + throw new RuntimeException("Error obtaining setter for " + + method.getName() + " in class " + aClass.getName(), e); + } + } + } + return null; + } + }