10
10
* Contributors:
11
11
* Roman Grigoriadi
12
12
******************************************************************************/
13
-
14
13
package org .eclipse .yasson .internal .model ;
15
14
15
+ import java .lang .annotation .Annotation ;
16
16
import java .lang .reflect .AnnotatedElement ;
17
+ import java .util .HashMap ;
18
+ import java .util .Map ;
19
+
20
+ import javax .json .bind .JsonbException ;
21
+
22
+ import org .eclipse .yasson .internal .properties .MessageKeys ;
23
+ import org .eclipse .yasson .internal .properties .Messages ;
17
24
18
25
/**
19
- * Annotation holder for fields, getters and setters.
26
+ * Annotation holder for classes, superclasses, interfaces, fields, getters and setters.
20
27
*
21
28
* @param <T> annotated element
22
29
*/
23
- public class JsonbAnnotatedElement <T extends AnnotatedElement > extends JsonbAnnotated {
30
+ public class JsonbAnnotatedElement <T extends AnnotatedElement > {
24
31
32
+ private final Map <Class <? extends Annotation >, Annotation > annotations = new HashMap <>(4 );
33
+
25
34
private final T element ;
26
35
27
36
/**
@@ -30,7 +39,10 @@ public class JsonbAnnotatedElement<T extends AnnotatedElement> extends JsonbAnno
30
39
* @param element Element.
31
40
*/
32
41
public JsonbAnnotatedElement (T element ) {
33
- super (element .getAnnotations ());
42
+ for (Annotation ann : element .getAnnotations ()) {
43
+ annotations .put (ann .annotationType (), ann );
44
+ }
45
+
34
46
this .element = element ;
35
47
}
36
48
@@ -42,4 +54,31 @@ public JsonbAnnotatedElement(T element) {
42
54
public T getElement () {
43
55
return element ;
44
56
}
57
+
58
+ /**
59
+ * Get an annotation by type.
60
+ * @param <AT> Type of annotation
61
+ * @param annotationClass Type of annotation
62
+ * @return Annotation by passed type
63
+ */
64
+ public <AT extends Annotation > AT getAnnotation (Class <AT > annotationClass ) {
65
+ return annotationClass .cast (annotations .get (annotationClass ));
66
+ }
67
+
68
+ public Annotation [] getAnnotations () {
69
+ return annotations .values ().toArray (new Annotation [0 ]);
70
+ }
71
+
72
+ /**
73
+ * Adds annotation.
74
+ *
75
+ * @param annotation Annotation to add.
76
+ */
77
+ public void putAnnotation (Annotation annotation ) {
78
+ if (annotations .containsKey (annotation .annotationType ())) {
79
+ throw new JsonbException (Messages .getMessage (MessageKeys .INTERNAL_ERROR ,
80
+ "Annotation already present: " + annotation ));
81
+ }
82
+ annotations .put (annotation .annotationType (), annotation );
83
+ }
45
84
}
0 commit comments