KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > AnnotationProxy


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.aop.annotation;
8
9 import java.lang.reflect.InvocationHandler JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.lang.reflect.Proxy JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  * Comment
16  *
17  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
18  * @version $Revision: 1.4 $
19  */

20 public class AnnotationProxy implements InvocationHandler JavaDoc
21 {
22    Map JavaDoc map;
23
24    public AnnotationProxy(Map JavaDoc valueMap)
25    {
26       map = valueMap;
27    }
28
29    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
30            throws Throwable JavaDoc
31    {
32       if (method.getName().equals("equals"))
33       {
34          return doEquals(proxy, args[0]);
35       }
36       else if (method.getName().equals("hashCode"))
37       {
38          return doHashCode();
39       }
40       else if (method.getName().equals("toString"))
41       {
42          return map.toString();
43       }
44       /*
45       Object obj = map.get(method.getName());
46       if (!method.getReturnType().equals(obj.getClass()))
47       {
48          System.err.println("***** " + method.toString() + " has bad return type: " + obj.getClass().getName());
49       }
50       return obj;
51       */

52       return map.get(method.getName());
53    }
54
55    private Object JavaDoc doEquals(Object JavaDoc proxy, Object JavaDoc obj)
56    {
57       if (obj == proxy) return Boolean.TRUE;
58       if (obj == null) return Boolean.FALSE;
59
60       Class JavaDoc[] intfs = proxy.getClass().getInterfaces();
61       if (!intfs[0].isAssignableFrom(obj.getClass()))
62       {
63          return Boolean.FALSE;
64       }
65       try
66       {
67          Proxy.getInvocationHandler(obj);
68       }
69       catch (Exception JavaDoc ex)
70       {
71          return Boolean.FALSE;
72       }
73       return Boolean.TRUE;
74    }
75
76    private Object JavaDoc doHashCode()
77    {
78       return new Integer JavaDoc(map.hashCode());
79    }
80
81    public static Object JavaDoc createProxy(javassist.bytecode.annotation.Annotation info) throws Exception JavaDoc
82    {
83       Class JavaDoc annotation = Thread.currentThread().getContextClassLoader().loadClass(info.getTypeName());
84       return createProxy(info, annotation);
85    }
86
87    public static Object JavaDoc createProxy(javassist.bytecode.annotation.Annotation info, Class JavaDoc annotation) throws Exception JavaDoc
88    {
89       Map JavaDoc map = ProxyMapCreator.createProxyMap(annotation, info);
90       AnnotationProxy proxyHandler = new AnnotationProxy(map);
91       return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class JavaDoc[]{annotation}, proxyHandler);
92    }
93
94    public static Object JavaDoc createProxy(Map JavaDoc map, Class JavaDoc annotation) throws Exception JavaDoc
95    {
96       AnnotationProxy proxyHandler = new AnnotationProxy(map);
97       return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class JavaDoc[]{annotation}, proxyHandler);
98    }
99 }
100
Popular Tags