use of net.sf.cglib.proxy.Enhancer in project sharding-jdbc by dangdangdotcom.
the class VisitorLogProxy method enhance.
/**
* 打印SQL解析调用树.
*
* @param target 待增强类
* @param <T> 泛型
* @return 增强后的新类的对象
*/
@SuppressWarnings("unchecked")
public static <T> T enhance(final Class<T> target) {
if (log.isTraceEnabled()) {
Enhancer result = new Enhancer();
result.setSuperclass(target);
result.setCallback(new VisitorHandler());
return (T) result.create();
} else {
try {
return target.newInstance();
} catch (final InstantiationException | IllegalAccessException ex) {
log.error("create Visitor exception: {}", ex);
throw new ShardingJdbcException(ex);
}
}
}
use of net.sf.cglib.proxy.Enhancer 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.Enhancer 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.Enhancer in project mybatis-3 by mybatis.
the class CglibProxyFactory method crateProxy.
static Object crateProxy(Class<?> type, Callback callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
Enhancer enhancer = new Enhancer();
enhancer.setCallback(callback);
enhancer.setSuperclass(type);
try {
type.getDeclaredMethod(WRITE_REPLACE_METHOD);
// ObjectOutputStream will call writeReplace of objects returned by writeReplace
if (log.isDebugEnabled()) {
log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this");
}
} catch (NoSuchMethodException e) {
enhancer.setInterfaces(new Class[] { WriteReplaceInterface.class });
} catch (SecurityException e) {
// nothing to do here
}
Object enhanced;
if (constructorArgTypes.isEmpty()) {
enhanced = enhancer.create();
} else {
Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
enhanced = enhancer.create(typesArray, valuesArray);
}
return enhanced;
}
use of net.sf.cglib.proxy.Enhancer in project byte-buddy by raphw.
the class TrivialClassCreationBenchmark method benchmarkCglib.
/**
* Performs a benchmark for a trivial class creation using cglib.
*
* @return The created instance, in order to avoid JIT removal.
*/
@Benchmark
public Class<?> benchmarkCglib() {
Enhancer enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setClassLoader(newClassLoader());
enhancer.setSuperclass(baseClass);
enhancer.setCallbackType(NoOp.class);
return enhancer.createClass();
}
Aggregations