Coverage Report - org.apache.commons.lang3.AnnotationUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnotationUtils
81%
91/111
90%
83/92
8,667
AnnotationUtils$1
91%
22/24
50%
4/8
8,667
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3;
 18  
 
 19  
 import java.lang.annotation.Annotation;
 20  
 import java.lang.reflect.InvocationTargetException;
 21  
 import java.lang.reflect.Method;
 22  
 import java.util.Arrays;
 23  
 
 24  
 import org.apache.commons.lang3.builder.ToStringBuilder;
 25  
 import org.apache.commons.lang3.builder.ToStringStyle;
 26  
 
 27  
 /**
 28  
  * <p>Helper methods for working with {@link Annotation} instances.</p>
 29  
  *
 30  
  * <p>This class contains various utility methods that make working with
 31  
  * annotations simpler.</p>
 32  
  *
 33  
  * <p>{@link Annotation} instances are always proxy objects; unfortunately
 34  
  * dynamic proxies cannot be depended upon to know how to implement certain
 35  
  * methods in the same manner as would be done by "natural" {@link Annotation}s.
 36  
  * The methods presented in this class can be used to avoid that possibility. It
 37  
  * is of course also possible for dynamic proxies to actually delegate their
 38  
  * e.g. {@link Annotation#equals(Object)}/{@link Annotation#hashCode()}/
 39  
  * {@link Annotation#toString()} implementations to {@link AnnotationUtils}.</p>
 40  
  *
 41  
  * <p>#ThreadSafe#</p>
 42  
  *
 43  
  * @since 3.0
 44  
  */
 45  
 public class AnnotationUtils {
 46  
 
 47  
     /**
 48  
      * A style that prints annotations as recommended.
 49  
      */
 50  1
     private static final ToStringStyle TO_STRING_STYLE = new ToStringStyle() {
 51  
         /** Serialization version */
 52  
         private static final long serialVersionUID = 1L;
 53  
 
 54  
         {
 55  1
             setDefaultFullDetail(true);
 56  1
             setArrayContentDetail(true);
 57  1
             setUseClassName(true);
 58  1
             setUseShortClassName(true);
 59  1
             setUseIdentityHashCode(false);
 60  1
             setContentStart("(");
 61  1
             setContentEnd(")");
 62  1
             setFieldSeparator(", ");
 63  1
             setArrayStart("[");
 64  1
             setArrayEnd("]");
 65  1
         }
 66  
 
 67  
         /**
 68  
          * {@inheritDoc}
 69  
          */
 70  
         @Override
 71  
         protected String getShortClassName(final java.lang.Class<?> cls) {
 72  1
             Class<? extends Annotation> annotationType = null;
 73  1
             for (final Class<?> iface : ClassUtils.getAllInterfaces(cls)) {
 74  1
                 if (Annotation.class.isAssignableFrom(iface)) {
 75  
                     @SuppressWarnings("unchecked") // OK because we just checked the assignability
 76  
                     final
 77  1
                     Class<? extends Annotation> found = (Class<? extends Annotation>) iface;
 78  1
                     annotationType = found;
 79  1
                     break;
 80  
                 }
 81  0
             }
 82  1
             return new StringBuilder(annotationType == null ? "" : annotationType.getName())
 83  
                     .insert(0, '@').toString();
 84  
         }
 85  
 
 86  
         /**
 87  
          * {@inheritDoc}
 88  
          */
 89  
         @Override
 90  
         protected void appendDetail(final StringBuffer buffer, final String fieldName, Object value) {
 91  2
             if (value instanceof Annotation) {
 92  0
                 value = AnnotationUtils.toString((Annotation) value);
 93  
             }
 94  2
             super.appendDetail(buffer, fieldName, value);
 95  2
         }
 96  
 
 97  
     };
 98  
 
 99  
     /**
 100  
      * <p>{@code AnnotationUtils} instances should NOT be constructed in
 101  
      * standard programming. Instead, the class should be used statically.</p>
 102  
      *
 103  
      * <p>This constructor is public to permit tools that require a JavaBean
 104  
      * instance to operate.</p>
 105  
      */
 106  0
     public AnnotationUtils() {
 107  0
     }
 108  
 
 109  
     //-----------------------------------------------------------------------
 110  
     /**
 111  
      * <p>Checks if two annotations are equal using the criteria for equality
 112  
      * presented in the {@link Annotation#equals(Object)} API docs.</p>
 113  
      *
 114  
      * @param a1 the first Annotation to compare, {@code null} returns
 115  
      * {@code false} unless both are {@code null}
 116  
      * @param a2 the second Annotation to compare, {@code null} returns
 117  
      * {@code false} unless both are {@code null}
 118  
      * @return {@code true} if the two annotations are {@code equal} or both
 119  
      * {@code null}
 120  
      */
 121  
     public static boolean equals(final Annotation a1, final Annotation a2) {
 122  20
         if (a1 == a2) {
 123  2
             return true;
 124  
         }
 125  18
         if (a1 == null || a2 == null) {
 126  4
             return false;
 127  
         }
 128  14
         final Class<? extends Annotation> type = a1.annotationType();
 129  14
         final Class<? extends Annotation> type2 = a2.annotationType();
 130  14
         Validate.notNull(type, "Annotation %s with null annotationType()", a1);
 131  14
         Validate.notNull(type2, "Annotation %s with null annotationType()", a2);
 132  14
         if (!type.equals(type2)) {
 133  0
             return false;
 134  
         }
 135  
         try {
 136  248
             for (final Method m : type.getDeclaredMethods()) {
 137  236
                 if (m.getParameterTypes().length == 0
 138  
                         && isValidAnnotationMemberType(m.getReturnType())) {
 139  236
                     final Object v1 = m.invoke(a1);
 140  236
                     final Object v2 = m.invoke(a2);
 141  236
                     if (!memberEquals(m.getReturnType(), v1, v2)) {
 142  2
                         return false;
 143  
                     }
 144  
                 }
 145  
             }
 146  0
         } catch (final IllegalAccessException ex) {
 147  0
             return false;
 148  0
         } catch (final InvocationTargetException ex) {
 149  0
             return false;
 150  12
         }
 151  12
         return true;
 152  
     }
 153  
 
 154  
     /**
 155  
      * <p>Generate a hash code for the given annotation using the algorithm
 156  
      * presented in the {@link Annotation#hashCode()} API docs.</p>
 157  
      *
 158  
      * @param a the Annotation for a hash code calculation is desired, not
 159  
      * {@code null}
 160  
      * @return the calculated hash code
 161  
      * @throws RuntimeException if an {@code Exception} is encountered during
 162  
      * annotation member access
 163  
      * @throws IllegalStateException if an annotation method invocation returns
 164  
      * {@code null}
 165  
      */
 166  
     public static int hashCode(final Annotation a) {
 167  5
         int result = 0;
 168  5
         final Class<? extends Annotation> type = a.annotationType();
 169  99
         for (final Method m : type.getDeclaredMethods()) {
 170  
             try {
 171  94
                 final Object value = m.invoke(a);
 172  94
                 if (value == null) {
 173  0
                     throw new IllegalStateException(
 174  
                             String.format("Annotation method %s returned null", m));
 175  
                 }
 176  94
                 result += hashMember(m.getName(), value);
 177  0
             } catch (final RuntimeException ex) {
 178  0
                 throw ex;
 179  0
             } catch (final Exception ex) {
 180  0
                 throw new RuntimeException(ex);
 181  94
             }
 182  
         }
 183  5
         return result;
 184  
     }
 185  
 
 186  
     /**
 187  
      * <p>Generate a string representation of an Annotation, as suggested by
 188  
      * {@link Annotation#toString()}.</p>
 189  
      *
 190  
      * @param a the annotation of which a string representation is desired
 191  
      * @return the standard string representation of an annotation, not
 192  
      * {@code null}
 193  
      */
 194  
     public static String toString(final Annotation a) {
 195  1
         final ToStringBuilder builder = new ToStringBuilder(a, TO_STRING_STYLE);
 196  3
         for (final Method m : a.annotationType().getDeclaredMethods()) {
 197  2
             if (m.getParameterTypes().length > 0) {
 198  0
                 continue; //wtf?
 199  
             }
 200  
             try {
 201  2
                 builder.append(m.getName(), m.invoke(a));
 202  0
             } catch (final RuntimeException ex) {
 203  0
                 throw ex;
 204  0
             } catch (final Exception ex) {
 205  0
                 throw new RuntimeException(ex);
 206  2
             }
 207  
         }
 208  1
         return builder.build();
 209  
     }
 210  
 
 211  
     /**
 212  
      * <p>Checks if the specified type is permitted as an annotation member.</p>
 213  
      *
 214  
      * <p>The Java language specification only permits certain types to be used
 215  
      * in annotations. These include {@link String}, {@link Class}, primitive
 216  
      * types, {@link Annotation}, {@link Enum}, and single-dimensional arrays of
 217  
      * these types.</p>
 218  
      *
 219  
      * @param type the type to check, {@code null}
 220  
      * @return {@code true} if the type is a valid type to use in an annotation
 221  
      */
 222  
     public static boolean isValidAnnotationMemberType(Class<?> type) {
 223  270
         if (type == null) {
 224  0
             return false;
 225  
         }
 226  270
         if (type.isArray()) {
 227  131
             type = type.getComponentType();
 228  
         }
 229  270
         return type.isPrimitive() || type.isEnum() || type.isAnnotation()
 230  
                 || String.class.equals(type) || Class.class.equals(type);
 231  
     }
 232  
 
 233  
     //besides modularity, this has the advantage of autoboxing primitives:
 234  
     /**
 235  
      * Helper method for generating a hash code for a member of an annotation.
 236  
      *
 237  
      * @param name the name of the member
 238  
      * @param value the value of the member
 239  
      * @return a hash code for this member
 240  
      */
 241  
     private static int hashMember(final String name, final Object value) {
 242  94
         final int part1 = name.hashCode() * 127;
 243  94
         if (value.getClass().isArray()) {
 244  46
             return part1 ^ arrayMemberHash(value.getClass().getComponentType(), value);
 245  
         }
 246  48
         if (value instanceof Annotation) {
 247  2
             return part1 ^ hashCode((Annotation) value);
 248  
         }
 249  46
         return part1 ^ value.hashCode();
 250  
     }
 251  
 
 252  
     /**
 253  
      * Helper method for checking whether two objects of the given type are
 254  
      * equal. This method is used to compare the parameters of two annotation
 255  
      * instances.
 256  
      *
 257  
      * @param type the type of the objects to be compared
 258  
      * @param o1 the first object
 259  
      * @param o2 the second object
 260  
      * @return a flag whether these objects are equal
 261  
      */
 262  
     private static boolean memberEquals(final Class<?> type, final Object o1, final Object o2) {
 263  236
         if (o1 == o2) {
 264  24
             return true;
 265  
         }
 266  212
         if (o1 == null || o2 == null) {
 267  0
             return false;
 268  
         }
 269  212
         if (type.isArray()) {
 270  114
             return arrayMemberEquals(type.getComponentType(), o1, o2);
 271  
         }
 272  98
         if (type.isAnnotation()) {
 273  4
             return equals((Annotation) o1, (Annotation) o2);
 274  
         }
 275  94
         return o1.equals(o2);
 276  
     }
 277  
 
 278  
     /**
 279  
      * Helper method for comparing two objects of an array type.
 280  
      *
 281  
      * @param componentType the component type of the array
 282  
      * @param o1 the first object
 283  
      * @param o2 the second object
 284  
      * @return a flag whether these objects are equal
 285  
      */
 286  
     private static boolean arrayMemberEquals(final Class<?> componentType, final Object o1, final Object o2) {
 287  114
         if (componentType.isAnnotation()) {
 288  4
             return annotationArrayMemberEquals((Annotation[]) o1, (Annotation[]) o2);
 289  
         }
 290  110
         if (componentType.equals(Byte.TYPE)) {
 291  10
             return Arrays.equals((byte[]) o1, (byte[]) o2);
 292  
         }
 293  100
         if (componentType.equals(Short.TYPE)) {
 294  10
             return Arrays.equals((short[]) o1, (short[]) o2);
 295  
         }
 296  90
         if (componentType.equals(Integer.TYPE)) {
 297  10
             return Arrays.equals((int[]) o1, (int[]) o2);
 298  
         }
 299  80
         if (componentType.equals(Character.TYPE)) {
 300  10
             return Arrays.equals((char[]) o1, (char[]) o2);
 301  
         }
 302  70
         if (componentType.equals(Long.TYPE)) {
 303  10
             return Arrays.equals((long[]) o1, (long[]) o2);
 304  
         }
 305  60
         if (componentType.equals(Float.TYPE)) {
 306  10
             return Arrays.equals((float[]) o1, (float[]) o2);
 307  
         }
 308  50
         if (componentType.equals(Double.TYPE)) {
 309  10
             return Arrays.equals((double[]) o1, (double[]) o2);
 310  
         }
 311  40
         if (componentType.equals(Boolean.TYPE)) {
 312  10
             return Arrays.equals((boolean[]) o1, (boolean[]) o2);
 313  
         }
 314  30
         return Arrays.equals((Object[]) o1, (Object[]) o2);
 315  
     }
 316  
 
 317  
     /**
 318  
      * Helper method for comparing two arrays of annotations.
 319  
      *
 320  
      * @param a1 the first array
 321  
      * @param a2 the second array
 322  
      * @return a flag whether these arrays are equal
 323  
      */
 324  
     private static boolean annotationArrayMemberEquals(final Annotation[] a1, final Annotation[] a2) {
 325  4
         if (a1.length != a2.length) {
 326  2
             return false;
 327  
         }
 328  4
         for (int i = 0; i < a1.length; i++) {
 329  2
             if (!equals(a1[i], a2[i])) {
 330  0
                 return false;
 331  
             }
 332  
         }
 333  2
         return true;
 334  
     }
 335  
 
 336  
     /**
 337  
      * Helper method for generating a hash code for an array.
 338  
      *
 339  
      * @param componentType the component type of the array
 340  
      * @param o the array
 341  
      * @return a hash code for the specified array
 342  
      */
 343  
     private static int arrayMemberHash(final Class<?> componentType, final Object o) {
 344  46
         if (componentType.equals(Byte.TYPE)) {
 345  4
             return Arrays.hashCode((byte[]) o);
 346  
         }
 347  42
         if (componentType.equals(Short.TYPE)) {
 348  4
             return Arrays.hashCode((short[]) o);
 349  
         }
 350  38
         if (componentType.equals(Integer.TYPE)) {
 351  4
             return Arrays.hashCode((int[]) o);
 352  
         }
 353  34
         if (componentType.equals(Character.TYPE)) {
 354  4
             return Arrays.hashCode((char[]) o);
 355  
         }
 356  30
         if (componentType.equals(Long.TYPE)) {
 357  4
             return Arrays.hashCode((long[]) o);
 358  
         }
 359  26
         if (componentType.equals(Float.TYPE)) {
 360  4
             return Arrays.hashCode((float[]) o);
 361  
         }
 362  22
         if (componentType.equals(Double.TYPE)) {
 363  4
             return Arrays.hashCode((double[]) o);
 364  
         }
 365  18
         if (componentType.equals(Boolean.TYPE)) {
 366  4
             return Arrays.hashCode((boolean[]) o);
 367  
         }
 368  14
         return Arrays.hashCode((Object[]) o);
 369  
     }
 370  
 }