001/*
002 * Created on 03-May-2004
003 * 
004 * (c) 2003-2005 ThoughtWorks Ltd
005 *
006 * See license.txt for license details
007 */
008package com.thoughtworks.proxy.factory;
009
010import java.lang.reflect.InvocationHandler;
011import java.lang.reflect.Proxy;
012
013import com.thoughtworks.proxy.Invoker;
014
015/**
016 * A {@link com.thoughtworks.proxy.ProxyFactory} based on a JDK.
017 *
018 * @author Aslak Hellesøy
019 * @since 0.1
020 * @see com.thoughtworks.proxy.factory
021 */
022public class StandardProxyFactory extends AbstractProxyFactory {
023
024    private static final long serialVersionUID = 4430360631813383235L;
025
026    /**
027     * The native InvocationHandler implementation.
028     *
029     * @since 0.1
030     */
031    static class StandardInvocationHandlerAdapter extends CoincidentalInvocationHandlerAdapter implements
032            InvocationHandler {
033        private static final long serialVersionUID = 141954540221604284L;
034
035        /**
036         * Construct a StandardInvocationHandlerAdapter.
037         *
038         * @param invoker the wrapping invoker instance
039         * @since 0.1
040         */
041        public StandardInvocationHandlerAdapter(Invoker invoker) {
042            super(invoker);
043        }
044    }
045
046    public <T> T createProxy(final Invoker invoker, final Class<?>... types) {
047        final Class<?>[] interfaces = new Class[types.length + 1];
048        System.arraycopy(types, 0, interfaces, 0, types.length);
049        interfaces[types.length] = InvokerReference.class;
050        @SuppressWarnings("unchecked")
051        final T proxyInstance = (T)Proxy.newProxyInstance(getClass().getClassLoader(), interfaces,
052                new StandardInvocationHandlerAdapter(invoker));
053        return proxyInstance;
054    }
055
056    public boolean canProxy(final Class<?> type) {
057        return type.isInterface();
058    }
059
060    public boolean isProxyClass(final Class<?> type) {
061        return Proxy.isProxyClass(type);
062    }
063
064}