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