Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from ldreux/master
Browse files Browse the repository at this point in the history
Support primitive types for annotation builder
  • Loading branch information
kaqqao committed May 3, 2017
2 parents f9a3e50 + 228cfe6 commit 35e59a7
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 22 deletions.
66 changes: 44 additions & 22 deletions src/main/java/io/leangen/geantyref/AnnotationInvocationHandler.java
Expand Up @@ -23,26 +23,43 @@
* An implementation of {@link Annotation} that mimics the behavior of normal annotations.
* It is an {@link InvocationHandler}, meant to be used via {@link TypeFactory#annotation(Class, Map)}.
* <p>
* The constructor checks that the all the elements required by the annotation interface are provided
* The constructor checks that the all the elements required by the annotation interface are provided
* and that the types are compatible. If extra elements are provided, they are ignored.
* If a value is of an incompatible type is provided or no value is provided for an element
* If a value is of an incompatible type is provided or no value is provided for an element
* without a default value, {@link AnnotationFormatException} is thrown.
* </p>
*
* <p>
* Note: {@link #equals(Object)} and {@link #hashCode()} and implemented as specified
* by {@link Annotation}, so instances are safe to mix with normal annotations.
*
* @see Annotation
*/
class AnnotationInvocationHandler implements Annotation, InvocationHandler, Serializable {

private static final long serialVersionUID = 4531865662440020201L;
/**
* Maps primitive {@code Class}es to their corresponding wrapper {@code Class}.
*/
private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<>();

static {
primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
primitiveWrapperMap.put(Byte.TYPE, Byte.class);
primitiveWrapperMap.put(Character.TYPE, Character.class);
primitiveWrapperMap.put(Short.TYPE, Short.class);
primitiveWrapperMap.put(Integer.TYPE, Integer.class);
primitiveWrapperMap.put(Long.TYPE, Long.class);
primitiveWrapperMap.put(Double.TYPE, Double.class);
primitiveWrapperMap.put(Float.TYPE, Float.class);
}

private final Class<? extends Annotation> annotationType;
private final Map<String, Object> values;
private final int hashCode;

AnnotationInvocationHandler(Class<? extends Annotation> annotationType, Map<String, Object> values) throws AnnotationFormatException {
Class[] interfaces = annotationType.getInterfaces();
if(annotationType.isAnnotation() && interfaces.length == 1 && interfaces[0] == Annotation.class) {
if (annotationType.isAnnotation() && interfaces.length == 1 && interfaces[0] == Annotation.class) {
this.annotationType = annotationType;
this.values = Collections.unmodifiableMap(normalize(annotationType, values));
this.hashCode = calculateHashCode();
Expand All @@ -51,27 +68,19 @@ class AnnotationInvocationHandler implements Annotation, InvocationHandler, Seri
}
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (values.containsKey(method.getName())) {
return values.get(method.getName());
}
return method.invoke(this, args);
}

@Override
public Class<? extends Annotation> annotationType() {
return annotationType;
}

private static Map<String, Object> normalize(Class<? extends Annotation> annotationType, Map<String, Object> values) throws AnnotationFormatException {
static Map<String, Object> normalize(Class<? extends Annotation> annotationType, Map<String, Object> values) throws AnnotationFormatException {
Set<String> missing = new HashSet<>();
Set<String> invalid = new HashSet<>();
Map<String, Object> valid = new HashMap<>();
for (Method element : annotationType.getDeclaredMethods()) {
String elementName = element.getName();
if (values.containsKey(elementName)) {
if (element.getReturnType().isInstance(values.get(elementName))) {
Class<?> returnType = element.getReturnType();
if (returnType.isPrimitive()) {
returnType = primitiveWrapperMap.get(returnType);
}

if (returnType.isInstance(values.get(elementName))) {
valid.put(elementName, values.get(elementName));
} else {
invalid.add(elementName);
Expand All @@ -93,6 +102,19 @@ private static Map<String, Object> normalize(Class<? extends Annotation> annotat
return valid;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (values.containsKey(method.getName())) {
return values.get(method.getName());
}
return method.invoke(this, args);
}

@Override
public Class<? extends Annotation> annotationType() {
return annotationType;
}

/**
* Performs an equality check as described in {@link Annotation#equals(Object)}.
*
Expand Down Expand Up @@ -151,7 +173,7 @@ public String toString() {
for (String elementName : sorted) {
String value;
if (values.get(elementName).getClass().isArray()) {
value = Arrays.deepToString(new Object[] {values.get(elementName)})
value = Arrays.deepToString(new Object[]{values.get(elementName)})
.replaceAll("^\\[\\[", "[")
.replaceAll("]]$", "]");
} else {
Expand Down Expand Up @@ -206,8 +228,8 @@ private int calculateHashCode(Object element) {
if (element instanceof float[]) {
return Arrays.hashCode((float[]) element);
}
if(element instanceof double[]) {
return Arrays.hashCode((double[])element);
if (element instanceof double[]) {
return Arrays.hashCode((double[]) element);
}
if (element instanceof boolean[]) {
return Arrays.hashCode((boolean[]) element);
Expand Down
@@ -0,0 +1,58 @@
package io.leangen.geantyref;

import org.junit.Test;

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

import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class AnnotationInvocationHandlerTest {
@Test
public void normalize() throws Exception {
// Given
Map<String, Object> values = new HashMap<>();
values.put("aBoolean", Boolean.FALSE);
values.put("anInt", 42);

// When
Map<String, Object> normalize = AnnotationInvocationHandler.normalize(MyAnnotation.class, unmodifiableMap(values));

// Then
assertThat(normalize, equalTo(values));
}

@Test(expected = AnnotationFormatException.class)
public void normalizeWithBadValues() throws Exception {
// Given
Map<String, Object> values = new HashMap<>();
values.put("aBoolean", "Some text");
values.put("anInt", 42);

// When
AnnotationInvocationHandler.normalize(MyAnnotation.class, unmodifiableMap(values));
}

@Test
public void normalizeDefaultValues() throws Exception {
// Given
Map<String, Object> values = new HashMap<>();
values.put("aBoolean", Boolean.FALSE);
values.put("anInt", 0);

// When
Map<String, Object> normalize = AnnotationInvocationHandler.normalize(MyAnnotation.class, emptyMap());

// Then
assertThat(normalize, equalTo(values));
}

@interface MyAnnotation {
boolean aBoolean() default false;

int anInt() default 0;
}
}

0 comments on commit 35e59a7

Please sign in to comment.