(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 21 Jul 2010 11:17:22 +0000 (11:17 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 21 Jul 2010 11:17:22 +0000 (11:17 +0000)
support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java
support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java
support/general/src/test/java/org/wamblee/reflection/AnnotationUtilsTest.java [new file with mode: 0644]

index dd0d1839756bb1bb21ecc7c4824e492e2491ba6b..6b24e3f06e3f32a8b8f1391e09961d12d2e5ce9c 100644 (file)
@@ -16,6 +16,7 @@
 package org.wamblee.persistence;
 
 import java.io.Serializable;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -128,9 +129,18 @@ public class PersistentFactory {
     }
 
     private static EntityAccessor analyse(Class aClass) {
-        Accessor<Serializable> pk = AnnotationUtils.analyse(aClass, Id.class);
-        Accessor<Integer> version = AnnotationUtils.analyse(aClass, Version.class);
-        if (pk != null || version != null) {
+        List<Accessor> pkAccessors = AnnotationUtils.analyse(aClass, Id.class);
+        List<Accessor> versionAccessors = AnnotationUtils.analyse(aClass,
+            Version.class);
+        Accessor<Serializable> pk = null; 
+        if ( pkAccessors.size() > 0 ) { 
+            pk = pkAccessors.get(0);
+        }
+        Accessor<Integer> version = null; 
+        if ( versionAccessors.size() > 0 ) { 
+            version = versionAccessors.get(0);
+        }
+        if (pk != null|| version != null) {
             return new EntityAccessor(pk, version);
         }
         return null;
index d31163fb11812487096953b9662d716f7dca5d1b..5ea776b3217dac607e939adcfcc49025c2eeb8df 100644 (file)
@@ -18,6 +18,7 @@ package org.wamblee.reflection;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -35,15 +36,17 @@ public class AnnotationUtils {
      *            Class to analyse.
      * @param aAnnotation
      *            Annotation that must be present.
-     * @return Accessor to use or null if the annotation is not present.
+     * @return List of accessors. Empty list is returned if no match is found. 
      */
     // TODO move generic analysis part to the reflection package.
-    public static Accessor analyse(Class aClass,
+    public static List<Accessor> analyse(Class aClass,
         Class<? extends Annotation> aAnnotation) {
+        List<Accessor> result = new ArrayList<Accessor>(); 
+        
         List<Field> fields = ReflectionUtils.getAllFields(aClass);
         for (Field field : fields) {
             if (field.isAnnotationPresent(aAnnotation)) {
-                return new FieldAccessor(field);
+                result.add(new FieldAccessor(field));
             }
         }
         List<Method> methods = ReflectionUtils.getAllMethods(aClass,
@@ -60,14 +63,14 @@ public class AnnotationUtils {
                     Class returnType = method.getReturnType();
                     Method setter = method.getDeclaringClass()
                         .getDeclaredMethod(setterName, returnType);
-                    return new PropertyAccessor(method, setter);
+                    result.add(new PropertyAccessor(method, setter));
                 } catch (NoSuchMethodException e) {
                     throw new RuntimeException("Error obtaining setter for " +
                         method.getName() + " in class " + aClass.getName(), e);
                 }
             }
         }
-        return null;
+        return result;
     }
 
 }
diff --git a/support/general/src/test/java/org/wamblee/reflection/AnnotationUtilsTest.java b/support/general/src/test/java/org/wamblee/reflection/AnnotationUtilsTest.java
new file mode 100644 (file)
index 0000000..0ab685c
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2005-2010 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.reflection;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.List;
+
+import org.junit.Test;
+
+import static junit.framework.TestCase.*; 
+
+public class AnnotationUtilsTest {
+    
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.FIELD, ElementType.METHOD})
+    public static @interface MyAnnotation { 
+        
+    }
+    
+    public static class X1 { 
+        @MyAnnotation
+        public int x; 
+    }
+    
+    public static class X2 { 
+        @MyAnnotation
+        private int x;
+        
+    }
+
+    public static class X3 { 
+        private int x;
+        
+        public void setX(int aX) {
+            x = aX;
+        }
+        
+        @MyAnnotation
+        public int getX() {
+            return x;
+        }
+    }
+    
+    public static class X4 { 
+        private int x;
+        
+        private void setX(int aX) {
+            x = aX;
+        }
+        
+        @MyAnnotation
+        private int getX() {
+            return x;
+        }
+    }
+
+    public static class X5 { 
+        private int x;
+        
+        @MyAnnotation
+        private int y; 
+        
+        private void setX(int aX) {
+            x = aX;
+        }
+        
+        @MyAnnotation
+        private int getX() {
+            return x;
+        }
+    }
+
+
+    
+    @Test
+    public void testPublicField() {
+        List<Accessor> accessors = AnnotationUtils.analyse(X1.class, MyAnnotation.class);
+        assertEquals(1, accessors.size());
+        assertTrue(accessors.get(0) instanceof FieldAccessor);
+
+        X1 obj = new X1();
+        assertEquals(0, obj.x);
+        accessors.get(0).set(obj, 100);
+        assertEquals(100,obj.x);
+    }
+    
+    @Test
+    public void testPrivateField() { 
+        List<Accessor> accessors = AnnotationUtils.analyse(X2.class, MyAnnotation.class);
+        assertEquals(1, accessors.size());
+        assertTrue(accessors.get(0) instanceof FieldAccessor);
+        X2 obj = new X2();
+        assertEquals(0, obj.x);
+        accessors.get(0).set(obj, 100);
+        assertEquals(100, obj.x);
+    }
+    
+    @Test
+    public void testPublicProperty() { 
+        List<Accessor> accessors = AnnotationUtils.analyse(X3.class, MyAnnotation.class);
+        assertEquals(1, accessors.size());
+        assertTrue(accessors.get(0) instanceof PropertyAccessor);
+        X3 obj = new X3();
+        assertEquals(0, obj.x);
+        accessors.get(0).set(obj, 100);
+        assertEquals(100, obj.x);
+    }
+    
+    @Test
+    public void testPrivateProperty() { 
+        List<Accessor> accessors = AnnotationUtils.analyse(X4.class, MyAnnotation.class);
+        assertEquals(1, accessors.size());
+        assertTrue(accessors.get(0) instanceof PropertyAccessor);
+        X4 obj = new X4();
+        assertEquals(0, obj.x);
+        accessors.get(0).set(obj, 100);
+        assertEquals(100, obj.x);
+    }
+    
+    @Test
+    public void testMultipleMatches() { 
+        List<Accessor> accessors = AnnotationUtils.analyse(X5.class, MyAnnotation.class);
+        assertEquals(2, accessors.size());
+        X5 obj = new X5();
+        assertEquals(0, obj.x);
+        assertEquals(0, obj.y);
+        for (Accessor accessor: accessors) { 
+            accessor.set(obj, 100);
+        }
+        assertEquals(100, obj.x);
+        assertEquals(100, obj.y);
+    }
+}