+ /**
+ * Returns the accessor for a given annotation.
+ *
+ * @param aClass
+ * Class to analyse.
+ * @param aAnnotation
+ * Annotation that must be 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 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)) {
+ result.add(new FieldAccessor(field));
+ }
+ }
+ List<Method> 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);
+ result.add(new PropertyAccessor(method, setter));
+ } catch (NoSuchMethodException e) {
+ throw new RuntimeException("Error obtaining setter for " +
+ method.getName() + " in class " + aClass.getName(), e);
+ }
+ }
+ }
+ return result;
+ }
+