View Javadoc

1   /*
2    * Copyright 2010 the original author or authors.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.callbackparams.annotation.util;
18  
19  import java.lang.annotation.Annotation;
20  import java.lang.reflect.Field;
21  
22  /**
23   * @deprecated
24   * This class is currently used by some of the utilities in the package
25   * {@link org.callbackparams.sandbox} but they will be modified and this package
26   * will be removed within the 1.0-release of the framework.
27   * @author Henrik Kaipe
28   */
29  public class EnumConstantDescriptor {
30  
31      private final Field coreEnumField;
32  
33      public EnumConstantDescriptor(final Enum<?> enumConstant) {
34          for (Field f : enumConstant.getDeclaringClass().getFields()) {
35              if (f.isEnumConstant()) {
36                  if (false == f.isAccessible()) {
37                      try {
38                          f.setAccessible(true);
39                      } catch (final SecurityException x) {
40                          /* Never mind */
41                      }
42                  }
43                  try {
44                      if (enumConstant == f.get(null)) {
45                          this.coreEnumField = f;
46                          return;
47                      }
48                  } catch (Exception x) {
49                      throw new Error(x);
50                  }
51              }
52          }
53  
54          throw new AssertionError("Unable to determine field for enum-constant "
55                  + enumConstant);
56      }
57  
58      /**
59       * Same as an incocation of {@link #getAnnotation(java.lang.Class, boolean)}
60       * with force=false.
61       * @see #getAnnotation(java.lang.Class, boolean)
62       */
63      public <A extends Annotation> A getAnnotation(Class<A> a) {
64          return getAnnotation(a, false);
65      }
66  
67      /**
68       * Tries to find an annotation of the specified type.
69       * Primarly the annotations for the enum-constant will be investigated. If
70       * the desired annotation cannot be found on the enum-constant then the
71       * annotation will be picked from the enum-class.
72       *
73       * @return the desired annotation or (in case force=false) null in case it
74       * cannot be found
75       * @throws AssertionError in case the desired annotation can not be found
76       * and force=true
77       */
78      public <A extends Annotation> A getAnnotation(
79              final Class<A> a, final boolean force) {
80          A annotation = coreEnumField.getAnnotation(a);
81          if (null != annotation) {
82              return annotation;
83          }
84          annotation = coreEnumField.getDeclaringClass().getAnnotation(a);
85          if (null != annotation) {
86              return annotation;
87          } else if (false == force) {
88              return null;
89          } else {
90              throw new AssertionError("Neither the enum-constant "
91                      + coreEnumField.getName() + " nor its class "
92                      + coreEnumField.getDeclaringClass() + " is annotated with "
93                      + a);
94          }
95      }
96  }