KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > backport175 > proxy > ProxyFactory


1 /*******************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://backport175.codehaus.org *
4  * --------------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of Apache License Version 2.0 *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  *******************************************************************************************/

8 package com.tc.backport175.proxy;
9
10 import com.tc.backport175.Annotation;
11 import com.tc.backport175.bytecode.AnnotationElement;
12
13 import java.lang.reflect.InvocationHandler JavaDoc;
14 import java.lang.reflect.Proxy JavaDoc;
15
16 /**
17  * Creates a proxy instance (Java dynamic proxy) for a given reader.
18  *
19  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
20  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
21  */

22 public class ProxyFactory {
23
24     /**
25      * Creates a new proxy for the annotation specified.
26      *
27      * @param annotation the annotation data structure abstraction
28      * @param loader the class loader for the target class
29      * @return the proxy for the annotation
30      */

31     public static Annotation newAnnotationProxy(final AnnotationElement.Annotation annotation,
32                                                  ClassLoader JavaDoc loader) {
33         ClassLoader JavaDoc backportLoader = Annotation.class.getClassLoader();
34         if (loader != backportLoader) {
35           loader = backportLoader;
36         }
37         final Class JavaDoc interfaceClass;
38         try {
39             interfaceClass = Class.forName(annotation.getInterfaceName(), false, loader);
40         } catch (ClassNotFoundException JavaDoc e) {
41             throw new ResolveAnnotationException("annotation interface [" + annotation.getInterfaceName() + "] could not be found");
42         }
43         final InvocationHandler JavaDoc handler = new JavaDocAnnotationInvocationHander(interfaceClass, annotation, loader);
44         final Object JavaDoc annotationProxy = Proxy.newProxyInstance(
45                   loader,
46                   new Class JavaDoc[]{Annotation.class, interfaceClass},
47                   handler
48           );
49         return (Annotation)annotationProxy;
50     }
51 }
52
Popular Tags