From ad3f409d2a7981f5a4619e3a37c0866b55ce6526 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 21 Jul 2010 11:17:22 +0000 Subject: [PATCH] --- .../persistence/PersistentFactory.java | 16 +- .../wamblee/reflection/AnnotationUtils.java | 13 +- .../reflection/AnnotationUtilsTest.java | 149 ++++++++++++++++++ 3 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 support/general/src/test/java/org/wamblee/reflection/AnnotationUtilsTest.java 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 dd0d1839..6b24e3f0 100644 --- a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java +++ b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java @@ -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 pk = AnnotationUtils.analyse(aClass, Id.class); - Accessor version = AnnotationUtils.analyse(aClass, Version.class); - if (pk != null || version != null) { + 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); + } + Accessor version = null; + if ( versionAccessors.size() > 0 ) { + version = versionAccessors.get(0); + } + if (pk != null|| version != null) { return new EntityAccessor(pk, version); } return null; 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 d31163fb..5ea776b3 100644 --- a/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java +++ b/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java @@ -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 analyse(Class aClass, Class aAnnotation) { + List result = new ArrayList(); + List fields = ReflectionUtils.getAllFields(aClass); for (Field field : fields) { if (field.isAnnotationPresent(aAnnotation)) { - return new FieldAccessor(field); + result.add(new FieldAccessor(field)); } } List 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 index 00000000..0ab685cd --- /dev/null +++ b/support/general/src/test/java/org/wamblee/reflection/AnnotationUtilsTest.java @@ -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 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 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 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 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 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); + } +} -- 2.31.1