KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > stub > api > Stub


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.stub.api;
21
22 import java.lang.reflect.InvocationHandler JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Proxy JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29 import org.netbeans.test.stub.spi.StubImplementation;
30 import org.openide.util.Lookup;
31
32 /**
33  *
34  * @author Andrei Badea
35  */

36 public final class Stub {
37     
38     private static StubImplementation IMPL;
39     
40     static {
41         IMPL = (StubImplementation)Lookup.getDefault().lookup(StubImplementation.class);
42         if (IMPL == null) {
43             IMPL = new DefaultStubImplementation();
44         }
45     }
46     
47     public static Object JavaDoc create(Class JavaDoc intf) {
48         return create(new Class JavaDoc[] { intf });
49     }
50     
51     public static Object JavaDoc create(Class JavaDoc intfs[]) {
52         return IMPL.create(intfs);
53     }
54     
55     public static Object JavaDoc create(Class JavaDoc intf, StubDelegate delegate) {
56         return create(new Class JavaDoc[] { intf }, delegate);
57     }
58     
59     public static Object JavaDoc create(Class JavaDoc[] intfs, StubDelegate delegate) {
60         return IMPL.create(intfs, delegate);
61     }
62     
63     public static Object JavaDoc getDelegate(Object JavaDoc stub) {
64         return IMPL.getDelegate(stub);
65     }
66     
67     public static void setProperty(Object JavaDoc stub, Object JavaDoc key, Object JavaDoc value) {
68         IMPL.setProperty(stub, key, value);
69     }
70     
71     private static final class DefaultStubImplementation implements StubImplementation {
72     
73         private static final Map JavaDoc/*<Object,Delegate>*/ STUB_TO_DELEGATE = new WeakHashMap JavaDoc();
74         
75         public Object JavaDoc create(Class JavaDoc[] intfs) {
76             return create(intfs, new DefaultInvocationHandler());
77         }
78
79         public Object JavaDoc create(Class JavaDoc[] intfs, StubDelegate delegate) {
80             Object JavaDoc stub = create(intfs, new MethodDelegatingInvocationHandler(delegate));
81             STUB_TO_DELEGATE.put(stub, delegate);
82             return stub;
83         }
84         
85         private Object JavaDoc create(Class JavaDoc[] intfs, InvocationHandler JavaDoc handler) {
86             return Proxy.newProxyInstance(Stub.class.getClassLoader(), intfs, handler);
87         }
88         
89         public Object JavaDoc getDelegate(Object JavaDoc stub) {
90             StubDelegate delegate = (StubDelegate)STUB_TO_DELEGATE.get(stub);
91             if (delegate == null) {
92                 throw new IllegalArgumentException JavaDoc("No delegate for this stub. Is " + stub + " a stub?");
93             }
94             return delegate;
95         }
96
97         public void setProperty(Object JavaDoc stub, Object JavaDoc key, Object JavaDoc value) {
98             ((StubDelegate)getDelegate(stub)).setProperty(key, value);
99         }
100     }
101     
102     /**
103      * Invocation handler which delegates to another object (a delegate)'s methods.
104      */

105     private static final class MethodDelegatingInvocationHandler implements InvocationHandler JavaDoc {
106         
107         public Object JavaDoc delegate;
108         
109         public MethodDelegatingInvocationHandler(Object JavaDoc delegate) {
110             this.delegate = delegate;
111         }
112
113         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
114             try {
115                 Method JavaDoc delegateMethod = delegate.getClass().getMethod(method.getName(), method.getParameterTypes());
116                 return delegateMethod.invoke(delegate, args);
117             } catch (InvocationTargetException JavaDoc e) {
118                 throw e.getCause();
119             } catch (NoSuchMethodException JavaDoc e) {
120                 // we avoid an UndeclaredThrowableExeception
121
// therefore displaying the caller and its stack trace
122
throw new RuntimeException JavaDoc("No method " + method.getName() + " with params " + Arrays.asList(method.getParameterTypes()));
123             }
124         }
125     }
126     
127     /**
128      * Invocation handler returning sane values for primitive return types.
129      */

130     private static final class DefaultInvocationHandler implements InvocationHandler JavaDoc {
131         
132         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
133             String JavaDoc methodName = method.getName();
134             Class JavaDoc[] paramTypes = method.getParameterTypes();
135
136             if ("hashCode".equals(methodName)) {
137                 return new Integer JavaDoc(System.identityHashCode(proxy));
138             } else if ("equals".equals(methodName) && paramTypes.length == 1 && paramTypes[0] == Object JavaDoc.class) {
139                 return Boolean.valueOf(args[0] == proxy);
140             }
141                 
142             Class JavaDoc retClass = method.getReturnType();
143             
144             if (retClass.isPrimitive()) {
145                 if (retClass == Byte.TYPE) {
146                     return new Byte JavaDoc((byte)0);
147                 } else if (retClass == Short.TYPE) {
148                     return new Short JavaDoc((short)0);
149                 } else if (retClass == Integer.TYPE) {
150                     return new Integer JavaDoc(0);
151                 } else if (retClass == Long.TYPE) {
152                     return new Long JavaDoc(0L);
153                 } else if (retClass == Float.TYPE) {
154                     return new Float JavaDoc(0);
155                 } else if (retClass == Double.TYPE) {
156                     return new Double JavaDoc(0.0);
157                 } else if (retClass == Character.TYPE) {
158                     return new Character JavaDoc('\0');
159                 } else if (retClass == Boolean.TYPE) {
160                     return Boolean.FALSE;
161                 }
162             }
163                 
164             return null;
165         }
166     }
167 }
168
Popular Tags