All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nakedobjects.plugins.headless.viewer.internal.CgLibProxy Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package org.nakedobjects.plugins.headless.viewer.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.Factory;

import org.nakedobjects.bytecode.cglib.persistence.objectfactory.internal.CglibEnhanced;
import org.nakedobjects.plugins.headless.applib.ViewObject;




public class CgLibProxy {

    private final DelegatingInvocationHandler handler;

    public CgLibProxy(final DelegatingInvocationHandler handler) {
        this.handler = handler;
    }

    @SuppressWarnings("unchecked")
    public T proxy() {

    	final T toProxy = handler.getDelegate();
    	
    	// handle if already proxied using cglib.
    	if (CglibEnhanced.class.isAssignableFrom(toProxy.getClass())) {
    		
    		handler.setResolveObjectChangedEnabled(true);
    		
    		Class enhancedClass = toProxy.getClass();
    		Class origSuperclass = toProxy.getClass().getSuperclass();
    		
    		List interfaces = new ArrayList();
			interfaces.addAll(Arrays.asList(enhancedClass.getInterfaces()));
			interfaces.remove(Factory.class); // if there.
			interfaces.add(ViewObject.class);
    		
			return (T) Enhancer.create(
					origSuperclass,
					interfaces.toArray(new Class[]{}),
					new InvocationHandlerMethodInterceptor(handler));
    	}
    	
        final Class clazz = (Class) toProxy.getClass();
        
        T proxy = null;
        try {
            final IProxyFactory proxyFactory = 
            	clazz.isInterface()?
					new JavaProxyFactory():
					new CgLibClassProxyFactory();
            proxy = proxyFactory.createProxy(clazz, handler);
        } catch (final RuntimeExceptionWrapper e) {
            throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
        }
        return proxy;
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy