KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.annotation.factory.duplicate;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * Comment
31  *
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
34  * @version $Revision: 46363 $
35  */

36 public class AnnotationProxy implements InvocationHandler JavaDoc
37 {
38    Map JavaDoc map;
39    Class JavaDoc annotationType;
40
41    public AnnotationProxy(Class JavaDoc annotationType, Map JavaDoc valueMap)
42    {
43       this.annotationType = annotationType;
44       map = valueMap;
45    }
46
47    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
48            throws Throwable JavaDoc
49    {
50       if (method.getName().equals("equals"))
51       {
52          return doEquals(proxy, args[0]);
53       }
54       else if (method.getName().equals("hashCode"))
55       {
56          return doHashCode();
57       }
58       else if (method.getName().equals("toString"))
59       {
60          return map.toString();
61       }
62       else if (method.getName().equals("annotationType"))
63       {
64          return annotationType;
65       }
66       
67       /*
68       Object obj = map.get(method.getName());
69       if (!method.getReturnType().equals(obj.getClass()))
70       {
71          System.err.println("***** " + method.toString() + " has bad return type: " + obj.getClass().getName());
72       }
73       return obj;
74       */

75       return map.get(method.getName());
76    }
77
78    public Object JavaDoc getValue(String JavaDoc name)
79    {
80       return map.get(name);
81    }
82    
83    private Object JavaDoc doEquals(Object JavaDoc proxy, Object JavaDoc obj)
84    {
85       if (obj == proxy) return Boolean.TRUE;
86       if (obj == null) return Boolean.FALSE;
87
88       Class JavaDoc[] intfs = proxy.getClass().getInterfaces();
89       if (!intfs[0].isAssignableFrom(obj.getClass()))
90       {
91          return Boolean.FALSE;
92       }
93       try
94       {
95          Proxy.getInvocationHandler(obj);
96       }
97       catch (Exception JavaDoc ex)
98       {
99          return Boolean.FALSE;
100       }
101       return Boolean.TRUE;
102    }
103
104    private Object JavaDoc doHashCode()
105    {
106       return new Integer JavaDoc(map.hashCode());
107    }
108
109    public static Object JavaDoc createProxy(Map JavaDoc map, Class JavaDoc annotation) throws Exception JavaDoc
110    {
111       AnnotationProxy proxyHandler = new AnnotationProxy(annotation, map);
112       return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class JavaDoc[]{annotation}, proxyHandler);
113    }
114 }
115
Popular Tags