Java Code Examples for org.springframework.cglib.proxy.Enhancer#setCallback()
The following examples show how to use
org.springframework.cglib.proxy.Enhancer#setCallback() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Recordings.java From syndesis with Apache License 2.0 | 6 votes |
static public <T> T recorder(Object object, Class<T> as) { if (as.isInterface()) { // If it's just an interface, use standard java reflect proxying return as.cast(Proxy.newProxyInstance(as.getClassLoader(), new Class<?>[] { as }, new RecordingInvocationHandler(object))); } // If it's a class then use gclib to implement a subclass to implement proxying RecordingInvocationHandler ih = new RecordingInvocationHandler(object); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(as); enhancer.setInterfaces(new Class<?>[]{RecordingProxy.class}); enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler() { @Override public Object invoke(Object o, Method method, Object[] objects) throws Throwable { return ih.invoke(o, method, objects); } }); return as.cast(enhancer.create()); }
Example 2
Source File: EnhancerUtil.java From specification-arg-resolver with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") static <T> T wrapWithIfaceImplementation(final Class<T> iface, final Specification<Object> targetSpec) { Enhancer enhancer = new Enhancer(); enhancer.setInterfaces(new Class[] { iface }); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if ("toString".equals(method.getName())) { return iface.getSimpleName() + "[" + proxy.invoke(targetSpec, args) + "]"; } return proxy.invoke(targetSpec, args); } }); return (T) enhancer.create(); }
Example 3
Source File: DomMapper.java From mica with GNU Lesser General Public License v3.0 | 5 votes |
/** * 读取 xml 信息为 java Bean * * @param doc xml element * @param clazz bean Class * @param <T> 泛型 * @return 对象 */ @SuppressWarnings("unchecked") public static <T> T readValue(final Element doc, final Class<T> clazz) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(clazz); enhancer.setUseCache(true); enhancer.setCallback(new CssQueryMethodInterceptor(clazz, doc)); return (T) enhancer.create(); }
Example 4
Source File: DubboUtils.java From java-master with Apache License 2.0 | 5 votes |
@SuppressWarnings("ALL") public static <T> T getService(Class<T> dubboServiceClass, String group, String version, String host) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(dubboServiceClass); enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> { ReferenceConfig<GenericService> reference = getReferenceConfig(dubboServiceClass, group, version, host); GenericService genericService = reference.get(); Object result = genericService.$invoke(method.getName(), getMethodParamType(method), args); String resJsonStr = OMUtils.objectMapper().writeValueAsString(result); return OMUtils.objectMapper().readValue(resJsonStr, method.getReturnType()); }); Object service = enhancer.create(); return (T) service; }
Example 5
Source File: ProxyFactory.java From DesignPatterns with Apache License 2.0 | 5 votes |
public Object getProxyInstance(){ //1.工具类 Enhancer en = new Enhancer(); //2.设置父类 en.setSuperclass(target.getClass()); //3.设置回调函数 en.setCallback(this); //4.创建子类(代理对象) return en.create(); }
Example 6
Source File: HttpClientProxyConfig.java From api-layer with Eclipse Public License 2.0 | 5 votes |
@Bean public CloseableHttpClient httpClientProxy() { Enhancer e = new Enhancer(); e.setSuperclass(CloseableHttpClient.class); e.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> { if (method.getName().equals("execute") && objects.length > 0 && objects[0] instanceof HttpRequest) { serviceAuthenticationDecorator.process((HttpRequest) objects[0]); } return method.invoke(clientChooser.chooseClient(), objects); } ); return (CloseableHttpClient) e.create(); }
Example 7
Source File: BladeFallbackFactory.java From blade-tool with GNU Lesser General Public License v3.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public T create(Throwable cause) { final Class<T> targetType = target.type(); final String targetName = target.name(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(targetType); enhancer.setUseCache(true); enhancer.setCallback(new BladeFeignFallback<>(targetType, targetName, cause)); return (T) enhancer.create(); }
Example 8
Source File: AddEnhancerMethodProxyTest.java From HotswapAgent with GNU General Public License v2.0 | 5 votes |
private A createEnhancer(Callback cb) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(AImpl.class); enhancer.setCallback(cb); final A a = (A) enhancer.create(); return a; }
Example 9
Source File: AbstractBaseServiceImpl.java From Roothub with GNU Affero General Public License v3.0 | 4 votes |
public Object getInstance() { Enhancer en = new Enhancer(); en.setSuperclass(queryWrapper.getClass()); en.setCallback(this); return en.create(); }