Search in sources :

Example 1 with MethodProxy

use of net.sf.cglib.proxy.MethodProxy in project dubbo by alibaba.

the class ProxyTest method testCglibProxy.

@Test
public void testCglibProxy() throws Exception {
    ITest test = (ITest) Proxy.getProxy(ITest.class).newInstance(new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println(method.getName());
            return null;
        }
    });
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(test.getClass());
    enhancer.setCallback(new MethodInterceptor() {

        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
            return null;
        }
    });
    try {
        enhancer.create();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) MethodProxy(net.sf.cglib.proxy.MethodProxy) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 2 with MethodProxy

use of net.sf.cglib.proxy.MethodProxy in project tomee by apache.

the class SeiFactoryImpl method initialize.

void initialize(Object serviceImpl, ClassLoader classLoader) throws ClassNotFoundException {
    this.serviceImpl = serviceImpl;
    Class serviceEndpointBaseClass = classLoader.loadClass(serviceEndpointClassName);
    serviceEndpointClass = enhanceServiceEndpointInterface(serviceEndpointBaseClass, classLoader);
    Class[] constructorTypes = new Class[] { classLoader.loadClass(GenericServiceEndpoint.class.getName()) };
    this.constructor = FastClass.create(serviceEndpointClass).getConstructor(constructorTypes);
    this.handlerInfoChainFactory = new HandlerInfoChainFactory(handlerInfos);
    sortedOperationInfos = new OperationInfo[FastClass.create(serviceEndpointClass).getMaxIndex() + 1];
    String encodingStyle = "";
    for (int i = 0; i < operationInfos.length; i++) {
        OperationInfo operationInfo = operationInfos[i];
        Signature signature = operationInfo.getSignature();
        MethodProxy methodProxy = MethodProxy.find(serviceEndpointClass, signature);
        if (methodProxy == null) {
            throw new ServerRuntimeException("No method proxy for operationInfo " + signature);
        }
        int index = methodProxy.getSuperIndex();
        sortedOperationInfos[index] = operationInfo;
        if (operationInfo.getOperationDesc().getUse() == Use.ENCODED) {
            encodingStyle = org.apache.axis.Constants.URI_SOAP11_ENC;
        }
    }
    //register our type descriptors
    Service service = ((ServiceImpl) serviceImpl).getService();
    AxisEngine axisEngine = service.getEngine();
    TypeMappingRegistry typeMappingRegistry = axisEngine.getTypeMappingRegistry();
    TypeMapping typeMapping = typeMappingRegistry.getOrMakeTypeMapping(encodingStyle);
    typeMapping.register(BigInteger.class, Constants.XSD_UNSIGNEDLONG, new SimpleSerializerFactory(BigInteger.class, Constants.XSD_UNSIGNEDLONG), new SimpleDeserializerFactory(BigInteger.class, Constants.XSD_UNSIGNEDLONG));
    typeMapping.register(URI.class, Constants.XSD_ANYURI, new SimpleSerializerFactory(URI.class, Constants.XSD_ANYURI), new SimpleDeserializerFactory(URI.class, Constants.XSD_ANYURI));
    //It is essential that the types be registered before the typeInfos create the serializer/deserializers.
    for (Iterator iter = typeInfo.iterator(); iter.hasNext(); ) {
        TypeInfo info = (TypeInfo) iter.next();
        TypeDesc.registerTypeDescForClass(info.getClazz(), info.buildTypeDesc());
    }
    TypeInfo.register(typeInfo, typeMapping);
}
Also used : SimpleSerializerFactory(org.apache.axis.encoding.ser.SimpleSerializerFactory) HandlerInfoChainFactory(org.apache.axis.handlers.HandlerInfoChainFactory) Service(org.apache.axis.client.Service) URI(java.net.URI) SimpleDeserializerFactory(org.apache.axis.encoding.ser.SimpleDeserializerFactory) Signature(net.sf.cglib.core.Signature) MethodProxy(net.sf.cglib.proxy.MethodProxy) Iterator(java.util.Iterator) TypeMappingRegistry(org.apache.axis.encoding.TypeMappingRegistry) TypeMapping(org.apache.axis.encoding.TypeMapping) BigInteger(java.math.BigInteger) FastClass(net.sf.cglib.reflect.FastClass) ServerRuntimeException(org.apache.openejb.server.ServerRuntimeException) AxisEngine(org.apache.axis.AxisEngine)

Example 3 with MethodProxy

use of net.sf.cglib.proxy.MethodProxy in project simplejpa by appoxy.

the class LazyInterceptor method handleSetMethod.

private void handleSetMethod(Object obj, Method method, Object[] args) throws Throwable {
    // we basically want to mark this object as dirty if this is called and to only delete attributes if it's dirty
    dirty = true;
    String attributeName = NamingHelper.attributeName(method);
    if (args != null && args.length == 1) {
        Object valueToSet = args[0];
        if (valueToSet == null) {
            // FIXME support direct field accessors better here
            PersistentMethod persistentMethod = (PersistentMethod) (PersistentMethod) em.getFactory().getAnnotationManager().getAnnotationInfo(obj).getPersistentProperty(attributeName);
            Method getter = persistentMethod.getGetter();
            MethodProxy getterProxy = MethodProxy.find(obj.getClass(), new Signature(persistentMethod.getGetter().getName(), Type.getType(getter.getReturnType()), new Type[] {}));
            Object ret = getterProxy.invokeSuper(obj, null);
            if (ret != null) {
                nulledFields.put(attributeName, ret);
                logger.fine("field " + attributeName + " is being nulled. Old value = " + ret);
            }
        }
    }
}
Also used : Type(net.sf.cglib.asm.Type) Signature(net.sf.cglib.core.Signature) MethodProxy(net.sf.cglib.proxy.MethodProxy) Method(java.lang.reflect.Method)

Example 4 with MethodProxy

use of net.sf.cglib.proxy.MethodProxy in project yyl_example by Relucent.

the class EnhancerTest method main.

public static void main(String[] args) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(null);
    enhancer.setInterfaces(new Class[] { I.class });
    enhancer.setCallback(new MethodInterceptor() {

        @Override
        public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
            if (Object.class.equals(method.getDeclaringClass())) {
                return methodProxy.invokeSuper(o, args);
            }
            System.out.println("->" + method.getName());
            return null;
        }
    });
    I a = I.class.cast(enhancer.create());
    a.method();
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) MethodProxy(net.sf.cglib.proxy.MethodProxy) Method(java.lang.reflect.Method)

Example 5 with MethodProxy

use of net.sf.cglib.proxy.MethodProxy in project cloudstack by apache.

the class AsyncCallbackDispatcher method getTarget.

@SuppressWarnings("unchecked")
public T getTarget() {
    Class<?> clz = _targetObject.getClass();
    String clzName = clz.getName();
    if (clzName.contains("EnhancerByCloudStack"))
        clz = clz.getSuperclass();
    Enhancer en = null;
    synchronized (enMap) {
        en = enMap.get(clz);
        if (en == null) {
            en = new Enhancer();
            en.setSuperclass(clz);
            en.setCallback(new MethodInterceptor() {

                @Override
                public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
                    return null;
                }
            });
            enMap.put(clz, en);
        }
    }
    try {
        T t = (T) en.create();
        Factory factory = (Factory) t;
        factory.setCallback(0, new MethodInterceptor() {

            @Override
            public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
                if (arg1.getParameterTypes().length == 0 && arg1.getName().equals("finalize")) {
                    return null;
                } else {
                    _callbackMethod = arg1;
                    _callbackMethod.setAccessible(true);
                    return null;
                }
            }
        });
        return t;
    } catch (Throwable e) {
        s_logger.error("Unexpected exception", e);
    }
    return null;
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) MethodProxy(net.sf.cglib.proxy.MethodProxy) Factory(net.sf.cglib.proxy.Factory) Method(java.lang.reflect.Method)

Aggregations

MethodProxy (net.sf.cglib.proxy.MethodProxy)6 Method (java.lang.reflect.Method)5 Enhancer (net.sf.cglib.proxy.Enhancer)4 MethodInterceptor (net.sf.cglib.proxy.MethodInterceptor)4 Signature (net.sf.cglib.core.Signature)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 BigInteger (java.math.BigInteger)1 URI (java.net.URI)1 Iterator (java.util.Iterator)1 Type (net.sf.cglib.asm.Type)1 CallbackFilter (net.sf.cglib.proxy.CallbackFilter)1 Factory (net.sf.cglib.proxy.Factory)1 FastClass (net.sf.cglib.reflect.FastClass)1 AxisEngine (org.apache.axis.AxisEngine)1 Service (org.apache.axis.client.Service)1 TypeMapping (org.apache.axis.encoding.TypeMapping)1 TypeMappingRegistry (org.apache.axis.encoding.TypeMappingRegistry)1 SimpleDeserializerFactory (org.apache.axis.encoding.ser.SimpleDeserializerFactory)1 SimpleSerializerFactory (org.apache.axis.encoding.ser.SimpleSerializerFactory)1 HandlerInfoChainFactory (org.apache.axis.handlers.HandlerInfoChainFactory)1