com.maxifier.mxcache.provider.ReflectiveAnnotationProperty Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
*/
package com.maxifier.mxcache.provider;
import com.maxifier.mxcache.util.CodegenHelper;
import javax.annotation.Nonnull;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author Alexander Kochurov ([email protected])
*/
public class ReflectiveAnnotationProperty extends AnnotationProperty {
private final Method method;
public ReflectiveAnnotationProperty(String name, Class type, T defaultValue, Class annotationType, String annotationPropertyName) {
this(name, type, defaultValue, annotationType, getMethod(annotationType, annotationPropertyName));
}
public ReflectiveAnnotationProperty(String name, Class type, T defaultValue, Class annotationType, Method method) {
super(name, type, annotationType, defaultValue);
if (!canBeCasted(type, method)) {
throw new IllegalArgumentException("Method " + method + " cannot be casted to " + type);
}
this.method = method;
}
private boolean canBeCasted(Class type, Method method) {
return box(type).isAssignableFrom(box(method.getReturnType()));
}
private static Class box(Class type) {
Class boxed = CodegenHelper.getBoxedType(type);
return boxed == null ? type : boxed;
}
private static Method getMethod(Class annotationClass, String annotationPropertyName) {
try {
return annotationClass.getMethod(annotationPropertyName);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("No such property: " + annotationClass + "." + annotationPropertyName, e);
}
}
@Override
public T getFromAnnotation(@Nonnull A annotation) {
try {
//noinspection unchecked
return (T) method.invoke(annotation);
} catch (IllegalAccessException e) {
throw new PropertyConvertationException(e);
} catch (InvocationTargetException e) {
throw new PropertyConvertationException(e);
}
}
}