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();
}
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();
}
}
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();
}
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;
}
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();
}
Aggregations