Skip to content

Commit 117df22

Browse files
authored
Merge pull request #334 from Degubi/annotatedelement_refactor
Refactor JsonbAnnotatedElement.java, remove JsonbAnnotated.java
2 parents 9759b35 + 3d310ac commit 117df22

File tree

2 files changed

+43
-82
lines changed

2 files changed

+43
-82
lines changed

src/main/java/org/eclipse/yasson/internal/model/JsonbAnnotated.java

-78
This file was deleted.

src/main/java/org/eclipse/yasson/internal/model/JsonbAnnotatedElement.java

+43-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@
1010
* Contributors:
1111
* Roman Grigoriadi
1212
******************************************************************************/
13-
1413
package org.eclipse.yasson.internal.model;
1514

15+
import java.lang.annotation.Annotation;
1616
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;
1724

1825
/**
19-
* Annotation holder for fields, getters and setters.
26+
* Annotation holder for classes, superclasses, interfaces, fields, getters and setters.
2027
*
2128
* @param <T> annotated element
2229
*/
23-
public class JsonbAnnotatedElement<T extends AnnotatedElement> extends JsonbAnnotated {
30+
public class JsonbAnnotatedElement<T extends AnnotatedElement> {
2431

32+
private final Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>(4);
33+
2534
private final T element;
2635

2736
/**
@@ -30,7 +39,10 @@ public class JsonbAnnotatedElement<T extends AnnotatedElement> extends JsonbAnno
3039
* @param element Element.
3140
*/
3241
public JsonbAnnotatedElement(T element) {
33-
super(element.getAnnotations());
42+
for (Annotation ann : element.getAnnotations()) {
43+
annotations.put(ann.annotationType(), ann);
44+
}
45+
3446
this.element = element;
3547
}
3648

@@ -42,4 +54,31 @@ public JsonbAnnotatedElement(T element) {
4254
public T getElement() {
4355
return element;
4456
}
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+
}
4584
}

0 commit comments

Comments
 (0)