KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > proxy > Proxy


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.proxy;
17
18 import java.io.Serializable JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Member JavaDoc;
21 import net.sf.cglib.core.CodeGenerationException;
22
23 /**
24  * This class is meant to be used as replacement for
25  * <code>java.lang.reflect.Proxy</code> under JDK 1.2. There are some known
26  * subtle differences:
27  * <ul>
28  * <li>The exceptions returned by invoking <code>getExceptionTypes</code>
29  * on the <code>Method</code> passed to the <code>invoke</code> method
30  * <b>are</b> the exact set that can be thrown without resulting in an
31  * <code>UndeclaredThrowableException</code> being thrown.
32  * <li>{@link UndeclaredThrowableException} is used instead
33  * of <code>java.lang.reflect.UndeclaredThrowableException</code>.
34  * </ul>
35  * <p>
36  * @version $Id: Proxy.java,v 1.6 2004/06/24 21:15:19 herbyderby Exp $
37  */

38 public class Proxy implements Serializable JavaDoc {
39     protected InvocationHandler h;
40
41     private static final CallbackFilter BAD_OBJECT_METHOD_FILTER = new CallbackFilter() {
42         public int accept(Method JavaDoc method) {
43             if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
44                 String JavaDoc name = method.getName();
45                 if (!(name.equals("hashCode") ||
46                       name.equals("equals") ||
47                       name.equals("toString"))) {
48                     return 1;
49                 }
50             }
51             return 0;
52         }
53     };
54
55     protected Proxy(InvocationHandler h) {
56         Enhancer.registerCallbacks(getClass(), new Callback[]{ h, null });
57         this.h = h;
58     }
59
60     // private for security of isProxyClass
61
private static class ProxyImpl extends Proxy {
62         protected ProxyImpl(InvocationHandler h) {
63             super(h);
64         }
65     }
66
67     public static InvocationHandler getInvocationHandler(Object JavaDoc proxy) {
68         if (!(proxy instanceof ProxyImpl)) {
69             throw new IllegalArgumentException JavaDoc("Object is not a proxy");
70         }
71         return ((Proxy)proxy).h;
72     }
73
74     public static Class JavaDoc getProxyClass(ClassLoader JavaDoc loader, Class JavaDoc[] interfaces) {
75         Enhancer e = new Enhancer();
76         e.setSuperclass(ProxyImpl.class);
77         e.setInterfaces(interfaces);
78         e.setCallbackTypes(new Class JavaDoc[]{
79             InvocationHandler.class,
80             NoOp.class,
81         });
82         e.setCallbackFilter(BAD_OBJECT_METHOD_FILTER);
83         e.setUseFactory(false);
84         return e.createClass();
85     }
86
87     public static boolean isProxyClass(Class JavaDoc cl) {
88         return cl.getSuperclass().equals(ProxyImpl.class);
89     }
90
91     public static Object JavaDoc newProxyInstance(ClassLoader JavaDoc loader, Class JavaDoc[] interfaces, InvocationHandler h) {
92         try {
93             Class JavaDoc clazz = getProxyClass(loader, interfaces);
94             return clazz.getConstructor(new Class JavaDoc[]{ InvocationHandler.class }).newInstance(new Object JavaDoc[]{ h });
95         } catch (RuntimeException JavaDoc e) {
96             throw e;
97         } catch (Exception JavaDoc e) {
98             throw new CodeGenerationException(e);
99         }
100     }
101 }
102
Popular Tags