Search in sources :

Example 1 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor in project buck by facebook.

the class TestCellBuilder method build.

public Cell build() throws IOException, InterruptedException {
    ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());
    BuckConfig config = buckConfig == null ? FakeBuckConfig.builder().setFilesystem(filesystem).build() : buckConfig;
    KnownBuildRuleTypesFactory typesFactory = new KnownBuildRuleTypesFactory(executor, androidDirectoryResolver);
    if (parserFactory == null) {
        return CellProvider.createForLocalBuild(filesystem, watchman, config, cellConfig, typesFactory).getCellByPath(filesystem.getRootPath());
    }
    // The constructor for `Cell` is private, and it's in such a central location I don't really
    // want to make it public. Brace yourselves.
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(Cell.class);
    enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
        if ("createBuildFileParserFactory".equals(method.getName())) {
            return parserFactory;
        }
        return proxy.invokeSuper(obj, args);
    });
    return (Cell) enhancer.create();
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) FakeAndroidDirectoryResolver(com.facebook.buck.android.FakeAndroidDirectoryResolver) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) IOException(java.io.IOException) ProjectBuildFileParserFactory(com.facebook.buck.json.ProjectBuildFileParserFactory) Watchman(com.facebook.buck.io.Watchman) NULL_WATCHMAN(com.facebook.buck.io.Watchman.NULL_WATCHMAN) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BuckConfig(com.facebook.buck.cli.BuckConfig) CellConfig(com.facebook.buck.config.CellConfig) TestConsole(com.facebook.buck.testutil.TestConsole) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) AndroidDirectoryResolver(com.facebook.buck.android.AndroidDirectoryResolver) Enhancer(net.sf.cglib.proxy.Enhancer) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) Nullable(javax.annotation.Nullable) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) Enhancer(net.sf.cglib.proxy.Enhancer) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) TestConsole(com.facebook.buck.testutil.TestConsole)

Example 2 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor 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 3 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor in project blade by biezhi.

the class ProxyFactory method getProxyObj.

/*
	 * 获得代理对象
	 */
public static Object getProxyObj(Class<?> clazz) throws Exception {
    Enhancer hancer = new Enhancer();
    // 设置代理对象的父类
    hancer.setSuperclass(clazz);
    // 设置回调对象,即调用代理对象里面的方法时,实际上执行的是回调对象(里的intercept方法)。
    MethodInterceptor[] methodInterceptors = filter(clazz);
    if (null != methodInterceptors && methodInterceptors.length > 0) {
        hancer.setCallbacks(methodInterceptors);
    } else {
        hancer.setCallback(NoOp.INSTANCE);
    }
    // 创建代理对象
    return hancer.create();
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer)

Example 4 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor in project blade by biezhi.

the class ProxyFactory method filter.

private static MethodInterceptor[] filter(Class<?> clazz) {
    if (null != aopInterceptors) {
        Aop aop = clazz.getAnnotation(Aop.class);
        if (null != aop) {
            Class<?> inteceptorType = aop.value();
            List<MethodInterceptor> methodInterceptors = new ArrayList<MethodInterceptor>();
            for (MethodInterceptor methodInterceptor : aopInterceptors) {
                if (inteceptorType.equals(methodInterceptor.getClass())) {
                    methodInterceptors.add(methodInterceptor);
                }
            }
            return methodInterceptors.toArray(new MethodInterceptor[methodInterceptors.size()]);
        }
    }
    return null;
}
Also used : Aop(com.blade.aop.annotation.Aop) MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) ArrayList(java.util.ArrayList)

Example 5 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor in project querydsl by querydsl.

the class AliasFactory method createProxy.

/**
     * Create a proxy instance for the given class and path
     *
     * @param <A>
     * @param cl type of the proxy
     * @param path underlying expression
     * @return proxy instance
     */
@SuppressWarnings("unchecked")
protected <A> A createProxy(Class<A> cl, Expression<?> path) {
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(AliasFactory.class.getClassLoader());
    if (cl.isInterface()) {
        enhancer.setInterfaces(new Class<?>[] { cl, ManagedObject.class });
    } else {
        enhancer.setSuperclass(cl);
        enhancer.setInterfaces(new Class<?>[] { ManagedObject.class });
    }
    // creates one handler per proxy
    MethodInterceptor handler = new PropertyAccessInvocationHandler(path, this, pathFactory, typeSystem);
    enhancer.setCallback(handler);
    return (A) enhancer.create();
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer)

Aggregations

MethodInterceptor (net.sf.cglib.proxy.MethodInterceptor)9 Enhancer (net.sf.cglib.proxy.Enhancer)8 Method (java.lang.reflect.Method)4 MethodProxy (net.sf.cglib.proxy.MethodProxy)4 Aop (com.blade.aop.annotation.Aop)1 AndroidDirectoryResolver (com.facebook.buck.android.AndroidDirectoryResolver)1 FakeAndroidDirectoryResolver (com.facebook.buck.android.FakeAndroidDirectoryResolver)1 BuckConfig (com.facebook.buck.cli.BuckConfig)1 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)1 CellConfig (com.facebook.buck.config.CellConfig)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Watchman (com.facebook.buck.io.Watchman)1 NULL_WATCHMAN (com.facebook.buck.io.Watchman.NULL_WATCHMAN)1 ProjectBuildFileParserFactory (com.facebook.buck.json.ProjectBuildFileParserFactory)1 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)1 TestConsole (com.facebook.buck.testutil.TestConsole)1 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)1 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1