Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com>
  • Loading branch information
Degubi committed Oct 27, 2019
1 parent 4c0ce93 commit 0256e6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 81 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@

package org.eclipse.yasson.internal.model;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

import java.util.Map;
import java.util.HashMap;

import javax.json.bind.JsonbException;

import org.eclipse.yasson.internal.properties.Messages;
import org.eclipse.yasson.internal.properties.MessageKeys;

/**
* Annotation holder for fields, getters and setters.
*
* @param <T> annotated element
*/
public class JsonbAnnotatedElement<T extends AnnotatedElement> extends JsonbAnnotated {

public class JsonbAnnotatedElement<T extends AnnotatedElement> {
private final Map<Class<? extends Annotation>, Annotation> annotations;
private final T element;

/**
Expand All @@ -30,7 +39,12 @@ public class JsonbAnnotatedElement<T extends AnnotatedElement> extends JsonbAnno
* @param element Element.
*/
public JsonbAnnotatedElement(T element) {
super(element.getAnnotations());
Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();

for (Annotation ann : element.getAnnotations()) {
annotations.put(ann.annotationType(), ann);
}
this.annotations = annotations;
this.element = element;
}

Expand All @@ -42,4 +56,25 @@ public JsonbAnnotatedElement(T element) {
public T getElement() {
return element;
}

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return annotationClass.cast(annotations.get(annotationClass));
}

public Annotation[] getAnnotations() {
return annotations.values().toArray(new Annotation[0]);
}

/**
* Adds annotation.
*
* @param annotation Annotation to add.
*/
public void putAnnotation(Annotation annotation) {
if (annotations.containsKey(annotation.annotationType())) {
throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR,
"Annotation already present: " + annotation));
}
annotations.put(annotation.annotationType(), annotation);
}
}

0 comments on commit 0256e6e

Please sign in to comment.