private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) { Object object = Proxy.newProxyInstance( interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler); return interfaceType.cast(object); }
private Object createInterfaceProxyForFactoryBean(final Object factoryBean, Class<?> interfaceType, final ConfigurableBeanFactory beanFactory, final String beanName) { return Proxy.newProxyInstance( factoryBean.getClass().getClassLoader(), new Class<?>[] {interfaceType}, (proxy, method, args) -> { if (method.getName().equals("getObject") && args == null) { return beanFactory.getBean(beanName); } return ReflectionUtils.invokeMethod(method, factoryBean, args); }); }
static void visit(Object resource, InvocationHandler visitor) throws IOException { Object visitorProxy = Proxy.newProxyInstance( VIRTUAL_FILE_VISITOR_INTERFACE.getClassLoader(), new Class<?>[] {VIRTUAL_FILE_VISITOR_INTERFACE}, visitor); invokeVfsMethod(VIRTUAL_FILE_METHOD_VISIT, resource, visitorProxy); }
@Override public void afterPropertiesSet() { if (this.serviceLocatorInterface == null) { throw new IllegalArgumentException("Property 'serviceLocatorInterface' is required"); } // Create service locator proxy. this.proxy = Proxy.newProxyInstance( this.serviceLocatorInterface.getClassLoader(), new Class<?>[] {this.serviceLocatorInterface}, new ServiceLocatorInvocationHandler()); }
/** * Wrap the given Connection with a proxy that delegates every method call to it * but suppresses close calls. * @param target the original Connection to wrap * @return the wrapped Connection */ protected Connection getCloseSuppressingConnectionProxy(Connection target) { return (Connection) Proxy.newProxyInstance( ConnectionProxy.class.getClassLoader(), new Class<?>[] {ConnectionProxy.class}, new CloseSuppressingInvocationHandler(target)); }
@Override @SuppressWarnings("unchecked") public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) { return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, new InvokerInvocationHandler(invoker)); }
@Override @SuppressWarnings("unchecked") public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) { return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, new InvokerInvocationHandler(invoker)); }
/** * Create a close-suppressing proxy for the given Hibernate Session. * The proxy also prepares returned Query and Criteria objects. * @param session the Hibernate Session to create a proxy for * @return the Session proxy * @see Session#close() * @see #prepareQuery * @see #prepareCriteria */ protected Session createSessionProxy(Session session) { return (Session) Proxy.newProxyInstance( session.getClass().getClassLoader(), new Class<?>[] {Session.class}, new CloseSuppressingInvocationHandler(session)); }
/** * Wrap the given Connection with a proxy that delegates every method call to it * but suppresses close calls. This is useful for allowing application code to * handle a special framework Connection just like an ordinary Connection from a * CCI ConnectionFactory. * @param target the original Connection to wrap * @return the wrapped Connection */ protected Connection getCloseSuppressingConnectionProxy(Connection target) { return (Connection) Proxy.newProxyInstance( Connection.class.getClassLoader(), new Class<?>[] {Connection.class}, new CloseSuppressingInvocationHandler(target)); }
@Override public void configureTlsExtensions( SSLSocket sslSocket, String hostname, List<Protocol> protocols) { List<String> names = alpnProtocolNames(protocols); try { Object alpnProvider = Proxy.newProxyInstance(Platform.class.getClassLoader(), new Class[] {clientProviderClass, serverProviderClass}, new AlpnProvider(names)); putMethod.invoke(null, sslSocket, alpnProvider); } catch (InvocationTargetException | IllegalAccessException e) { throw new AssertionError("failed to set ALPN", e); } }
private Session getCloseSuppressingSessionProxy(Session target) { List<Class<?>> classes = new ArrayList<>(3); classes.add(SessionProxy.class); if (target instanceof QueueSession) { classes.add(QueueSession.class); } if (target instanceof TopicSession) { classes.add(TopicSession.class); } return (Session) Proxy.newProxyInstance(SessionProxy.class.getClassLoader(), ClassUtils.toClassArray(classes), new CloseSuppressingSessionInvocationHandler(target)); } }
@SuppressWarnings("unchecked") public <T> T createMock(Class<T> toMock) { return (T) Proxy.newProxyInstance(BeanFactoryGenericsTests.class.getClassLoader(), new Class<?>[] {toMock}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { throw new UnsupportedOperationException("mocked!"); } }); } }
@Override public ITestBean getObject() { return (ITestBean) Proxy.newProxyInstance(CustomProxyFactoryBean.class.getClassLoader(), new Class<?>[]{ITestBean.class}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return ReflectionUtils.invokeMethod(method, tb, args); } }); }
@SuppressWarnings("unchecked") public <T> T createMock(Class<T> toMock) { return (T) Proxy.newProxyInstance(AutowiredAnnotationBeanPostProcessorTests.class.getClassLoader(), new Class<?>[] {toMock}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { throw new UnsupportedOperationException("mocked!"); } }); } }
@Override public Object getProxy(@Nullable ClassLoader classLoader) { if (logger.isTraceEnabled()) { logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource()); } Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); }
private Object buildProxy(String referencedBeanName, ReferenceBean referenceBean, Class<?> injectedType) { InvocationHandler handler = buildInvocationHandler(referencedBeanName, referenceBean); Object proxy = Proxy.newProxyInstance(getClassLoader(), new Class[]{injectedType}, handler); return proxy; }
private Object buildProxy(String referencedBeanName, ReferenceBean referenceBean, Class<?> injectedType) { InvocationHandler handler = buildInvocationHandler(referencedBeanName, referenceBean); Object proxy = Proxy.newProxyInstance(getClassLoader(), new Class[]{injectedType}, handler); return proxy; }
private static Object newProxyWithEqualsForInterfaces(Class<?>... interfaces) { return Proxy.newProxyInstance( AbstractInvocationHandlerTest.class.getClassLoader(), interfaces, new DelegatingInvocationHandlerWithEquals("a string")); }
@Test(expected = IllegalArgumentException.class) public void testProxiedUserInterfacesWithNoInterface() { Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[0], (proxy1, method, args) -> null); AopProxyUtils.proxiedUserInterfaces(proxy); }
@Test public void varargsAgainstProxy_SPR16122() { SpelExpressionParser parser = new SpelExpressionParser(); Expression expr = parser.parseExpression("process('a', 'b')"); VarargsReceiver receiver = new VarargsReceiver(); VarargsInterface proxy = (VarargsInterface) Proxy.newProxyInstance( getClass().getClassLoader(), new Class<?>[] {VarargsInterface.class}, (proxy1, method, args) -> method.invoke(receiver, args)); assertEquals("OK", expr.getValue(new StandardEvaluationContext(receiver))); assertEquals("OK", expr.getValue(new StandardEvaluationContext(proxy))); }