X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fpersistence%2FPersistentFactory.java;h=2ad1526599adf13e29df8a6afe21b9671b02e948;hb=0160b65d4c0f112ae839140980e80b4fa6f348d5;hp=bd1217e61cb7b2ae66dc2a7b0749228c26362da7;hpb=7c1f4aa47fcc3098427073b5ce9f6abed8befa7c;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 bd1217e6..2ad15265 100644 --- a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java +++ b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java @@ -16,9 +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; @@ -26,7 +23,8 @@ import java.util.concurrent.ConcurrentHashMap; import javax.persistence.Id; import javax.persistence.Version; -import org.wamblee.reflection.ReflectionUtils; +import org.wamblee.reflection.Accessor; +import org.wamblee.reflection.AnnotationUtils; /** * Factory which creates a {@link Persistent} object for a given JPA entity for @@ -131,52 +129,19 @@ public class PersistentFactory { } private static EntityAccessor analyse(Class aClass) { - Accessor pk = analyse(aClass, Id.class); - Accessor version = analyse(aClass, Version.class); - if (pk != null || version != null) { - return new EntityAccessor(pk, version); + List pkAccessors = AnnotationUtils.analyse(aClass, Id.class); + List versionAccessors = AnnotationUtils.analyse(aClass, + Version.class); + Accessor pk = null; + if (pkAccessors.size() > 0) { + pk = pkAccessors.get(0); } - 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); - } + Accessor version = null; + if (versionAccessors.size() > 0) { + version = versionAccessors.get(0); } - 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); - } - } + if (pk != null || version != null) { + return new EntityAccessor(pk, version); } return null; }