1 package com.fasterxml.classmate;
2
3 import java.io.Serializable;
4 import java.lang.annotation.Annotation;
5 import java.util.*;
6
7 import com.fasterxml.classmate.util.ClassKey;
8
9 /**
10  * Interface for object that determines handling of annotations in regards
11  * to inheritance, overrides.
12  */

13 @SuppressWarnings("serial")
14 public abstract class AnnotationConfiguration implements Serializable
15 {
16     /**
17      * Method called to figure out how to handle instances of specified annotation
18      * type when used as class annotation.
19      */

20     public abstract AnnotationInclusion getInclusionForClass(Class<? extends Annotation> annotationType);
21
22     /**
23      * Method called to figure out how to handle instances of specified annotation
24      * type when used as constructor annotation.
25      *<p>
26      * Note that constructor annotations can never be inherited so this just determines
27      * between inclusion or non-inclusion.
28      */

29     public abstract AnnotationInclusion getInclusionForConstructor(Class<? extends Annotation> annotationType);
30     
31     /**
32      * Method called to figure out how to handle instances of specified annotation
33      * type when used as field annotation.
34      *<p>
35      * Note that field annotations can never be inherited so this just determines
36      * between inclusion or non-inclusion.
37      */

38     public abstract AnnotationInclusion getInclusionForField(Class<? extends Annotation> annotationType);
39     
40     /**
41      * Method called to figure out how to handle instances of specified annotation
42      * type when used as method annotation.
43      *<p>
44      * Note that method annotations can be inherited for member methods, but not for static
45      * methods; for static methods thereby this just determines between inclusion and
46      * non-inclusion.
47      */

48     public abstract AnnotationInclusion getInclusionForMethod(Class<? extends Annotation> annotationType);
49
50     /**
51      * Method called to figure out how to handle instances of specified annotation
52      * type when used as parameter annotation.
53      *<p>
54      * Note that parameter annotations can be inherited for member methods, but not for static
55      * methods; for static methods thereby this just determines between inclusion and
56      * non-inclusion.
57      */

58     public abstract AnnotationInclusion getInclusionForParameter(Class<? extends Annotation> annotationType);
59     
60     /**
61      * Simple implementation that can be configured with default behavior
62      * for unknown annotations, as well as explicit behaviors for
63      * enumerated annotation types. Same default is used for both class and
64      * member method annotations (constructor, field and static method
65      * annotations are never inherited)
66      */

67     public static class StdConfiguration extends AnnotationConfiguration implements Serializable
68     {
69         protected final AnnotationInclusion _defaultInclusion;
70
71         protected final HashMap<ClassKey,AnnotationInclusion> _inclusions = new HashMap<ClassKey,AnnotationInclusion>();
72         
73         public StdConfiguration(AnnotationInclusion defaultBehavior)
74         {
75             _defaultInclusion = defaultBehavior;
76         }
77         
78         @Override
79         public AnnotationInclusion getInclusionForClass(Class<? extends Annotation> annotationType) {
80             return _inclusionFor(annotationType);
81         }
82
83         @Override
84         public AnnotationInclusion getInclusionForConstructor(Class<? extends Annotation> annotationType) {
85             return _inclusionFor(annotationType);
86         }
87
88         @Override
89         public AnnotationInclusion getInclusionForField(Class<? extends Annotation> annotationType) {
90             return getInclusionForClass(annotationType);
91         }
92         
93         @Override
94         public AnnotationInclusion getInclusionForMethod(Class<? extends Annotation> annotationType) {
95             return getInclusionForClass(annotationType);
96         }
97
98         @Override
99         public AnnotationInclusion getInclusionForParameter(Class<? extends Annotation> annotationType) {
100             return getInclusionForClass(annotationType);
101         }
102
103         public void setInclusion(Class<? extends Annotation> annotationType, AnnotationInclusion incl)
104         {
105             _inclusions.put(new ClassKey(annotationType), incl);
106         }
107
108         protected AnnotationInclusion _inclusionFor(Class<? extends Annotation> annotationType)
109         {
110             ClassKey key = new ClassKey(annotationType);
111             AnnotationInclusion beh = _inclusions.get(key);
112             return (beh == null) ? _defaultInclusion : beh;
113         }
114     }
115 }
116