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 /**
20 * AnnotationIntrospector
21 *
22 *
23 */
24 public class AnnotationIntrospector
25 {
26 protected List<IntrospectableAnnotationHandler> _handlers = new ArrayList<IntrospectableAnnotationHandler>();
27
28
29 /**
30 * IntrospectableAnnotationHandler
31 *
32 * Interface for all handlers that wish to introspect a class to find a particular annotation
33 */
34 public interface IntrospectableAnnotationHandler
35 {
36 public void handle(Class<?> clazz);
37 }
38
39
40
41 /**
42 * AbstractIntrospectableAnnotationHandler
43 *
44 * Base class for handlers that introspect a class to find a particular annotation.
45 * A handler can optionally introspect the parent hierarchy of a class.
46 */
47 public static abstract class AbstractIntrospectableAnnotationHandler implements IntrospectableAnnotationHandler
48 {
49 private boolean _introspectAncestors;
50
51 public abstract void doHandle(Class<?> clazz);
52
53
54 public AbstractIntrospectableAnnotationHandler(boolean introspectAncestors)
55 {
56 _introspectAncestors = introspectAncestors;
57 }
58
59 public void handle(Class<?> clazz)
60 {
61 Class<?> c = clazz;
62
63 //process the whole inheritance hierarchy for the class
64 while (c!=null && (!c.equals(Object.class)))
65 {
66 doHandle(c);
67 if (!_introspectAncestors)
68 break;
69
70 c = c.getSuperclass();
71 }
72 }
73 }
74
75 public void registerHandler (IntrospectableAnnotationHandler handler)
76 {
77 _handlers.add(handler);
78 }
79
80 public void introspect (Class<?> clazz)
81 {
82 if (_handlers == null)
83 return;
84 if (clazz == null)
85 return;
86
87 for (IntrospectableAnnotationHandler handler:_handlers)
88 {
89 try
90 {
91 handler.handle(clazz);
92 }
93 catch (RuntimeException e)
94 {
95 throw e;
96 }
97 catch (Exception e)
98 {
99 throw new RuntimeException(e);
100 }
101 }
102
103 }
104 }