Added a caching mechanism to AnnotationValues, for instantiating the annotation.

This commit is contained in:
Reinier Zwitserloot committed 2009-06-27 03:10:04 +02:00
1 parent 47bd3e3c06
commit 157bbba3ac
1 file changed
+4 -1
+4 -1
View File
@@ -77,8 +77,11 @@ public class AnnotationValues<A extends Annotation> {
"No value supplied but " + method.getName() + " has no default either.", -1);
}
private A cachedInstance = null;
@SuppressWarnings("unchecked")
public A getInstance() throws AnnotationValueDecodeFail {
if ( cachedInstance != null ) return cachedInstance;
InvocationHandler invocations = new InvocationHandler() {
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
AnnotationValue v = values.get(method.getName());
@@ -134,7 +137,7 @@ public class AnnotationValues<A extends Annotation> {
}
};
return (A) Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, invocations);
return cachedInstance = (A) Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, invocations);
}
private Object guessToType(Object guess, Class<?> expected, AnnotationValue v, int pos) {