01: /*******************************************************************************************
02: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
03: * http://backport175.codehaus.org *
04: * --------------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of Apache License Version 2.0 *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: *******************************************************************************************/package com.tc.backport175.proxy;
08:
09: import com.tc.backport175.Annotation;
10: import com.tc.backport175.bytecode.AnnotationElement;
11:
12: import java.lang.reflect.InvocationHandler;
13: import java.lang.reflect.Proxy;
14:
15: /**
16: * Creates a proxy instance (Java dynamic proxy) for a given reader.
17: *
18: * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér</a>
19: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
20: */
21: public class ProxyFactory {
22:
23: /**
24: * Creates a new proxy for the annotation specified.
25: *
26: * @param annotation the annotation data structure abstraction
27: * @param loader the class loader for the target class
28: * @return the proxy for the annotation
29: */
30: public static Annotation newAnnotationProxy(
31: final AnnotationElement.Annotation annotation,
32: ClassLoader loader) {
33: ClassLoader backportLoader = Annotation.class.getClassLoader();
34: if (loader != backportLoader) {
35: loader = backportLoader;
36: }
37: final Class interfaceClass;
38: try {
39: interfaceClass = Class.forName(annotation
40: .getInterfaceName(), false, loader);
41: } catch (ClassNotFoundException e) {
42: throw new ResolveAnnotationException(
43: "annotation interface ["
44: + annotation.getInterfaceName()
45: + "] could not be found");
46: }
47: final InvocationHandler handler = new JavaDocAnnotationInvocationHander(
48: interfaceClass, annotation, loader);
49: final Object annotationProxy = Proxy.newProxyInstance(loader,
50: new Class[] { Annotation.class, interfaceClass },
51: handler);
52: return (Annotation) annotationProxy;
53: }
54: }
|