Coverage Report - org.seasar.uruma.util.AnnotationUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnotationUtil
66%
31/47
59%
13/22
0
AnnotationUtil$ClassEntry
67%
18/27
41%
9/22
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.uruma.util;
 17  
 
 18  
 import java.lang.annotation.Annotation;
 19  
 import java.lang.reflect.Field;
 20  
 import java.lang.reflect.Method;
 21  
 import java.util.ArrayList;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.seasar.framework.beans.BeanDesc;
 27  
 import org.seasar.framework.beans.PropertyDesc;
 28  
 import org.seasar.framework.beans.factory.BeanDescFactory;
 29  
 
 30  
 /**
 31  
  * アノテーションを持つクラスに対するユーティリティクラスです。<br />
 32  
  * 
 33  
  * @author y-komori
 34  
  */
 35  
 public class AnnotationUtil {
 36  4
     private static Map<ClassEntry, List<Field>> fieldCache = new HashMap<ClassEntry, List<Field>>();
 37  
 
 38  4
     private static Map<ClassEntry, List<Method>> methodCache = new HashMap<ClassEntry, List<Method>>();
 39  
 
 40  4
     private static Map<ClassEntry, List<PropertyDesc>> pdCache = new HashMap<ClassEntry, List<PropertyDesc>>();
 41  
 
 42  0
     protected AnnotationUtil() {
 43  0
     }
 44  
 
 45  
     /**
 46  
      * 特定のアノテーションが付加されたフィールドを取得します。<br />
 47  
      * <p>
 48  
      * <code>clazz</code> で指定されたクラスから <code>annotationClass</code>
 49  
      * で指定されたアノテーションが付加された {@link Field} オブジェクトのリストを返します。<br />
 50  
      * フィールドは、親クラスまでさかのぼってすべて検索されます。
 51  
      * </p>
 52  
      * 
 53  
      * @param clazz
 54  
      *            対象クラス
 55  
      * @param annotationClass
 56  
      *            対象アノテーション
 57  
      * @return 見つかった {@link Field} オブジェクトのリスト
 58  
      */
 59  
     public static List<Field> getAnnotatedFields(final Class<?> clazz,
 60  
             final Class<? extends Annotation> annotationClass) {
 61  8
         ClassEntry entry = new ClassEntry(clazz, annotationClass);
 62  8
         List<Field> result = fieldCache.get(entry);
 63  8
         if (result != null) {
 64  4
             return result;
 65  
         } else {
 66  4
             result = new ArrayList<Field>();
 67  4
             BeanDesc desc = BeanDescFactory.getBeanDesc(clazz);
 68  4
             int fieldSize = desc.getFieldSize();
 69  28
             for (int i = 0; i < fieldSize; i++) {
 70  24
                 Field field = desc.getField(i);
 71  24
                 if (field.getAnnotation(annotationClass) != null) {
 72  12
                     result.add(field);
 73  12
                     fieldCache.put(entry, result);
 74  
                 }
 75  
             }
 76  4
             return result;
 77  
         }
 78  
     }
 79  
 
 80  
     /**
 81  
      * 特定のアノテーションが付加されたメソッドを取得します。<br />
 82  
      * <p>
 83  
      * <code>clazz</code> で指定されたクラスから <code>annotationClass</code>
 84  
      * で指定されたアノテーションが付加された {@link Method} オブジェクトのリストを返します。<br />
 85  
      * メソッドは、親クラスまでさかのぼってすべて検索されます。
 86  
      * </p>
 87  
      * 
 88  
      * @param clazz
 89  
      *            対象クラス
 90  
      * @param annotationClass
 91  
      *            対象アノテーション
 92  
      * @return 見つかったメソッドのリスト
 93  
      */
 94  
     public static List<Method> getAnnotatedMethods(final Class<?> clazz,
 95  
             final Class<? extends Annotation> annotationClass) {
 96  0
         ClassEntry entry = new ClassEntry(clazz, annotationClass);
 97  0
         List<Method> result = methodCache.get(entry);
 98  0
         if (result != null) {
 99  0
             return result;
 100  
         } else {
 101  0
             result = new ArrayList<Method>();
 102  0
             BeanDesc desc = BeanDescFactory.getBeanDesc(clazz);
 103  
 
 104  0
             String[] methodNames = desc.getMethodNames();
 105  0
             for (int i = 0; i < methodNames.length; i++) {
 106  0
                 Method[] methods = desc.getMethods(methodNames[i]);
 107  
 
 108  0
                 for (int j = 0; j < methods.length; j++) {
 109  0
                     if (methods[j].isAnnotationPresent(annotationClass)) {
 110  0
                         result.add(methods[j]);
 111  
                     }
 112  
                 }
 113  
             }
 114  0
             methodCache.put(entry, result);
 115  
 
 116  0
             return result;
 117  
         }
 118  
     }
 119  
 
 120  
     /**
 121  
      * 特定のアノテーションが付加されたフィールドに対応する {@link PropertyDesc} を取得します。<br />
 122  
      * <p>
 123  
      * <code>class</code> で指定されたクラスから <code>annotationClass</code>
 124  
      * で指定されたアノテーションが付加されたフィールドオブジェクトを検索し、それに対応する {@link PropertyDesc}
 125  
      * オブジェクトのリストを返します。 フィールドは、親クラスまでさかのぼってすべて検索されます。
 126  
      * </p>
 127  
      * 
 128  
      * @param clazz
 129  
      *            対象クラス
 130  
      * @param annotationClass
 131  
      *            対象アノテーション
 132  
      * @return 見つかった {@link PropertyDesc} のリスト
 133  
      */
 134  
     public static List<PropertyDesc> getAnnotatedPropertyDescs(
 135  
             final Class<?> clazz,
 136  
             final Class<? extends Annotation> annotationClass) {
 137  140
         ClassEntry entry = new ClassEntry(clazz, annotationClass);
 138  140
         List<PropertyDesc> result = pdCache.get(entry);
 139  140
         if (result != null) {
 140  108
             return result;
 141  
         } else {
 142  32
             result = new ArrayList<PropertyDesc>();
 143  32
             BeanDesc desc = BeanDescFactory.getBeanDesc(clazz);
 144  32
             int fieldSize = desc.getFieldSize();
 145  268
             for (int i = 0; i < fieldSize; i++) {
 146  236
                 Field field = desc.getField(i);
 147  236
                 if (field.getAnnotation(annotationClass) != null) {
 148  128
                     String propertyName = field.getName();
 149  128
                     if (desc.hasPropertyDesc(propertyName)) {
 150  128
                         result.add(desc.getPropertyDesc(propertyName));
 151  128
                         pdCache.put(entry, result);
 152  
                     }
 153  
                 }
 154  
             }
 155  32
             return result;
 156  
         }
 157  
     }
 158  
 
 159  
     private static class ClassEntry {
 160  
         private Class<?> clazz;
 161  
 
 162  
         private Class<? extends Annotation> annotationClass;
 163  
 
 164  
         ClassEntry(final Class<?> clazz,
 165  148
                 final Class<? extends Annotation> annotationClass) {
 166  148
             this.clazz = clazz;
 167  148
             this.annotationClass = annotationClass;
 168  148
         }
 169  
 
 170  
         @Override
 171  
         public int hashCode() {
 172  288
             final int PRIME = 31;
 173  288
             int result = 1;
 174  288
             result = PRIME
 175  
                     * result
 176  
                     + ((this.annotationClass == null) ? 0
 177  
                             : this.annotationClass.hashCode());
 178  288
             result = PRIME * result
 179  
                     + ((this.clazz == null) ? 0 : this.clazz.hashCode());
 180  288
             return result;
 181  
         }
 182  
 
 183  
         @Override
 184  
         public boolean equals(final Object obj) {
 185  112
             if (this == obj)
 186  0
                 return true;
 187  112
             if (obj == null)
 188  0
                 return false;
 189  112
             if (getClass() != obj.getClass())
 190  0
                 return false;
 191  112
             final ClassEntry other = (ClassEntry) obj;
 192  112
             if (this.annotationClass == null) {
 193  0
                 if (other.annotationClass != null)
 194  0
                     return false;
 195  112
             } else if (!this.annotationClass.equals(other.annotationClass))
 196  0
                 return false;
 197  112
             if (this.clazz == null) {
 198  0
                 if (other.clazz != null)
 199  0
                     return false;
 200  112
             } else if (!this.clazz.equals(other.clazz))
 201  0
                 return false;
 202  112
             return true;
 203  
         }
 204  
     }
 205  
 }