javassist.util.proxy.MethodHandler Java Examples
The following examples show how to use
javassist.util.proxy.MethodHandler.
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: JacksonClientModule.java From bowman with Apache License 2.0 | 6 votes |
public JacksonClientModule() { setSerializerModifier(new BeanSerializerModifier() { @Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { for (BeanPropertyWriter writer : beanProperties) { if (writer.getAnnotation(LinkedResource.class) != null) { writer.assignSerializer(new LinkedResourceUriSerializer()); } } return beanProperties; } }); setMixInAnnotation(EntityModel.class, ResourceMixin.class); setMixInAnnotation(MethodHandler.class, MethodHandlerMixin.class); }
Example #2
Source File: JavassistProxyFactory.java From ymate-platform-v2 with Apache License 2.0 | 6 votes |
@Override @SuppressWarnings("unchecked") public <T> T createProxy(final Class<?> targetClass, final List<IProxy> proxies) { ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(targetClass); Class<?> clazz = factory.createClass(); try { Object targetObj = clazz.newInstance(); ((ProxyObject) targetObj).setHandler(new MethodHandler() { @Override public Object invoke(final Object self, Method thisMethod, final Method proceed, final Object[] args) throws Throwable { return new AbstractProxyChain(JavassistProxyFactory.this, targetClass, self, thisMethod, args, proxies) { @Override protected Object doInvoke() throws Throwable { return proceed.invoke(getTargetObject(), getMethodParams()); } }.doProxyChain(); } }); return (T) targetObj; } catch (Exception e) { throw new Error(e); } }
Example #3
Source File: ContextClassLoaderInjector.java From scheduling with GNU Affero General Public License v3.0 | 6 votes |
/** * Create an injected object which has been injected with specifying the thread context class loader during all its methods invocation. * * @param superClass The class whose object methods need to be injected * @param contextClassLoader the thread context class loader to inject in all the methods of the class object * @return the created injected object */ public static Object createInjectedObject(Class<?> superClass, ClassLoader contextClassLoader) throws Exception { proxyFactory.setSuperclass(superClass); // The thread context class loader also needs to be sepecified during the object instantiation. Object object = switchContextClassLoader(contextClassLoader, () -> { Class<?> proxyClass = proxyFactory.createClass(); return proxyClass.newInstance(); }); MethodHandler injectClassLoaderHandler = (self, method, proceed, args) -> { logger.debug("Delegating method: " + self.getClass().getSimpleName() + "." + method.getName()); // inject setting of thread context classloader during execution of all the object original methods return switchContextClassLoader(contextClassLoader, () -> proceed.invoke(self, args)); }; ((Proxy) object).setHandler(injectClassLoaderHandler); return object; }
Example #4
Source File: ProxiesTest.java From furnace with Eclipse Public License 1.0 | 6 votes |
@Test public void testIsProxyObjectType() throws Exception { Assert.assertTrue(Proxies.isProxyType(new ProxyObject() { @Override public void setHandler(MethodHandler mi) { } @Override public MethodHandler getHandler() { return null; } }.getClass())); }
Example #5
Source File: ProxyHelper.java From stevia with BSD 3-Clause "New" or "Revised" License | 6 votes |
@SuppressWarnings("rawtypes") public static <T> T create(final Object obj, final ProxyCallback onSuccess, final ProxyCallback onException) throws Exception { ProxyFactory factory = new ProxyFactory(); Class<T> origClass = (Class<T>) obj.getClass(); factory.setSuperclass(origClass); Class clazz = factory.createClass(); MethodHandler handler = new MethodHandler() { @Override public Object invoke(Object self, Method overridden, Method forwarder, Object[] args) throws Throwable { try { Object returnVal = overridden.invoke(obj, args); if (null != onSuccess) onSuccess.execute(); return returnVal; } catch (Throwable tr) { if (null != onException) onException.execute(); throw tr; } } }; Object instance = clazz.newInstance(); ((ProxyObject) instance).setHandler(handler); return (T) instance; }
Example #6
Source File: JacksonClientModuleTest.java From bowman with Apache License 2.0 | 5 votes |
@Test public void handlerOnJavassistProxyIsNotSerialized() throws Exception { Entity proxy = new Entity("x") { public MethodHandler getHandler() { return null; } }; String json = mapper.writeValueAsString(proxy); assertThat(json, not(containsString("\"handler\""))); }
Example #7
Source File: ClassByExtensionBenchmark.java From byte-buddy with Apache License 2.0 | 5 votes |
/** * Performs a benchmark of a class extension using javassist proxies. * * @return The created instance, in order to avoid JIT removal. * @throws java.lang.Exception If the invocation causes an exception. */ @Benchmark public ExampleClass benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); proxyFactory.setSuperclass(baseClass); proxyFactory.setFilter(new MethodFilter() { public boolean isHandled(Method method) { return method.getDeclaringClass() == baseClass; } }); @SuppressWarnings("unchecked") Object instance = proxyFactory.createClass().getDeclaredConstructor().newInstance(); ((javassist.util.proxy.Proxy) instance).setHandler(new MethodHandler() { public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable { return proceed.invoke(self, args); } }); return (ExampleClass) instance; }
Example #8
Source File: ProxiesTest.java From furnace with Eclipse Public License 1.0 | 5 votes |
@Test public void testIsProxyType() throws Exception { Assert.assertTrue(Proxies.isProxyType(new Proxy() { @Override public void setHandler(MethodHandler mi) { } }.getClass())); }
Example #9
Source File: ClassByImplementationBenchmark.java From byte-buddy with Apache License 2.0 | 4 votes |
/** * Performs a benchmark of an interface implementation using javassist proxies. * * @return The created instance, in order to avoid JIT removal. * @throws java.lang.Exception If the reflective invocation causes an exception. */ @Benchmark public ExampleInterface benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); proxyFactory.setSuperclass(Object.class); proxyFactory.setInterfaces(new Class<?>[]{baseClass}); proxyFactory.setFilter(new MethodFilter() { public boolean isHandled(Method method) { return true; } }); @SuppressWarnings("unchecked") Object instance = proxyFactory.createClass().getDeclaredConstructor().newInstance(); ((javassist.util.proxy.Proxy) instance).setHandler(new MethodHandler() { public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable { Class<?> returnType = thisMethod.getReturnType(); if (returnType.isPrimitive()) { if (returnType == boolean.class) { return defaultBooleanValue; } else if (returnType == byte.class) { return defaultByteValue; } else if (returnType == short.class) { return defaultShortValue; } else if (returnType == char.class) { return defaultCharValue; } else if (returnType == int.class) { return defaultIntValue; } else if (returnType == long.class) { return defaultLongValue; } else if (returnType == float.class) { return defaultFloatValue; } else { return defaultDoubleValue; } } else { return defaultReferenceValue; } } }); return (ExampleInterface) instance; }