001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.test.stub.api;
043:
044: import java.lang.reflect.InvocationHandler;
045: import java.lang.reflect.InvocationTargetException;
046: import java.lang.reflect.Method;
047: import java.lang.reflect.Proxy;
048: import java.util.Arrays;
049: import java.util.Map;
050: import java.util.WeakHashMap;
051: import org.netbeans.test.stub.spi.StubImplementation;
052: import org.openide.util.Lookup;
053:
054: /**
055: *
056: * @author Andrei Badea
057: */
058: public final class Stub {
059:
060: private static StubImplementation IMPL;
061:
062: static {
063: IMPL = (StubImplementation) Lookup.getDefault().lookup(
064: StubImplementation.class);
065: if (IMPL == null) {
066: IMPL = new DefaultStubImplementation();
067: }
068: }
069:
070: public static Object create(Class intf) {
071: return create(new Class[] { intf });
072: }
073:
074: public static Object create(Class intfs[]) {
075: return IMPL.create(intfs);
076: }
077:
078: public static Object create(Class intf, StubDelegate delegate) {
079: return create(new Class[] { intf }, delegate);
080: }
081:
082: public static Object create(Class[] intfs, StubDelegate delegate) {
083: return IMPL.create(intfs, delegate);
084: }
085:
086: public static Object getDelegate(Object stub) {
087: return IMPL.getDelegate(stub);
088: }
089:
090: public static void setProperty(Object stub, Object key, Object value) {
091: IMPL.setProperty(stub, key, value);
092: }
093:
094: private static final class DefaultStubImplementation implements
095: StubImplementation {
096:
097: private static final Map/*<Object,Delegate>*/STUB_TO_DELEGATE = new WeakHashMap();
098:
099: public Object create(Class[] intfs) {
100: return create(intfs, new DefaultInvocationHandler());
101: }
102:
103: public Object create(Class[] intfs, StubDelegate delegate) {
104: Object stub = create(intfs,
105: new MethodDelegatingInvocationHandler(delegate));
106: STUB_TO_DELEGATE.put(stub, delegate);
107: return stub;
108: }
109:
110: private Object create(Class[] intfs, InvocationHandler handler) {
111: return Proxy.newProxyInstance(Stub.class.getClassLoader(),
112: intfs, handler);
113: }
114:
115: public Object getDelegate(Object stub) {
116: StubDelegate delegate = (StubDelegate) STUB_TO_DELEGATE
117: .get(stub);
118: if (delegate == null) {
119: throw new IllegalArgumentException(
120: "No delegate for this stub. Is " + stub
121: + " a stub?");
122: }
123: return delegate;
124: }
125:
126: public void setProperty(Object stub, Object key, Object value) {
127: ((StubDelegate) getDelegate(stub)).setProperty(key, value);
128: }
129: }
130:
131: /**
132: * Invocation handler which delegates to another object (a delegate)'s methods.
133: */
134: private static final class MethodDelegatingInvocationHandler
135: implements InvocationHandler {
136:
137: public Object delegate;
138:
139: public MethodDelegatingInvocationHandler(Object delegate) {
140: this .delegate = delegate;
141: }
142:
143: public Object invoke(Object proxy, Method method, Object[] args)
144: throws Throwable {
145: try {
146: Method delegateMethod = delegate.getClass().getMethod(
147: method.getName(), method.getParameterTypes());
148: return delegateMethod.invoke(delegate, args);
149: } catch (InvocationTargetException e) {
150: throw e.getCause();
151: } catch (NoSuchMethodException e) {
152: // we avoid an UndeclaredThrowableExeception
153: // therefore displaying the caller and its stack trace
154: throw new RuntimeException("No method "
155: + method.getName() + " with params "
156: + Arrays.asList(method.getParameterTypes()));
157: }
158: }
159: }
160:
161: /**
162: * Invocation handler returning sane values for primitive return types.
163: */
164: private static final class DefaultInvocationHandler implements
165: InvocationHandler {
166:
167: public Object invoke(Object proxy, Method method, Object[] args)
168: throws Throwable {
169: String methodName = method.getName();
170: Class[] paramTypes = method.getParameterTypes();
171:
172: if ("hashCode".equals(methodName)) {
173: return new Integer(System.identityHashCode(proxy));
174: } else if ("equals".equals(methodName)
175: && paramTypes.length == 1
176: && paramTypes[0] == Object.class) {
177: return Boolean.valueOf(args[0] == proxy);
178: }
179:
180: Class retClass = method.getReturnType();
181:
182: if (retClass.isPrimitive()) {
183: if (retClass == Byte.TYPE) {
184: return new Byte((byte) 0);
185: } else if (retClass == Short.TYPE) {
186: return new Short((short) 0);
187: } else if (retClass == Integer.TYPE) {
188: return new Integer(0);
189: } else if (retClass == Long.TYPE) {
190: return new Long(0L);
191: } else if (retClass == Float.TYPE) {
192: return new Float(0);
193: } else if (retClass == Double.TYPE) {
194: return new Double(0.0);
195: } else if (retClass == Character.TYPE) {
196: return new Character('\0');
197: } else if (retClass == Boolean.TYPE) {
198: return Boolean.FALSE;
199: }
200: }
201:
202: return null;
203: }
204: }
205: }
|