View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.annotations;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /**
25   * AnnotationIntrospector
26   *
27   *
28   */
29  public class AnnotationIntrospector
30  {    
31      protected List<IntrospectableAnnotationHandler> _handlers = new ArrayList<IntrospectableAnnotationHandler>();
32      
33      
34      /**
35       * IntrospectableAnnotationHandler
36       *
37       * Interface for all handlers that wish to introspect a class to find a particular annotation
38       */
39      public interface IntrospectableAnnotationHandler
40      {
41          public void handle(Class<?> clazz);
42      }
43      
44      
45      
46      /**
47       * AbstractIntrospectableAnnotationHandler
48       *
49       * Base class for handlers that introspect a class to find a particular annotation.
50       * A handler can optionally introspect the parent hierarchy of a class.
51       */
52      public static abstract class AbstractIntrospectableAnnotationHandler implements IntrospectableAnnotationHandler
53      {
54          private boolean _introspectAncestors;
55          
56          public abstract void doHandle(Class<?> clazz);
57          
58          
59          public AbstractIntrospectableAnnotationHandler(boolean introspectAncestors)
60          {
61              _introspectAncestors = introspectAncestors;
62          }
63          
64          public void handle(Class<?> clazz)
65          {
66              Class<?> c = clazz;
67              
68              //process the whole inheritance hierarchy for the class
69              while (c!=null && (!c.equals(Object.class)))
70              {
71                  doHandle(c);
72                  if (!_introspectAncestors)
73                      break;
74                  
75                  c = c.getSuperclass();
76              }   
77          }
78      }
79      
80      public void registerHandler (IntrospectableAnnotationHandler handler)
81      {
82          _handlers.add(handler);
83      }
84      
85      public void introspect (Class<?> clazz)
86      {
87          if (_handlers == null)
88              return;
89          if (clazz == null)
90              return;
91          
92          for (IntrospectableAnnotationHandler handler:_handlers)
93          {
94              try
95              {
96                  handler.handle(clazz);
97              }
98              catch (RuntimeException e)
99              {
100                 throw e;
101             }
102             catch (Exception e)
103             {
104                 throw new RuntimeException(e);
105             }
106         }
107      
108     }
109 }