@SuppressWarnings("unchecked") private static <T> T initProxy(Class<?> type, ControllerMethodInvocationInterceptor interceptor) { if (type.isInterface()) { ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE); factory.addInterface(type); factory.addInterface(MethodInvocationInfo.class); factory.addAdvice(interceptor); return (T) factory.getProxy(); } else { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(type); enhancer.setInterfaces(new Class<?>[] {MethodInvocationInfo.class}); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class); Factory factory = (Factory) objenesis.newInstance(enhancer.createClass()); factory.setCallbacks(new Callback[] {interceptor}); return (T) factory; } }
private void initializeProxy() { if (adviceChain.length == 0) { return; } ProxyFactory factory = new ProxyFactory(); for (Advice advice : getAdviceChain()) { factory.addAdvisor(new DefaultPointcutAdvisor(Pointcut.TRUE, advice)); } factory.setProxyTargetClass(false); factory.addInterface(ContainerDelegate.class); factory.setTarget(delegate); proxy = (ContainerDelegate) factory.getProxy(); }
@Test public void invokeListenerInvalidProxy() { Object target = new InvalidProxyTestBean(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(target); proxyFactory.addInterface(SimpleService.class); Object bean = proxyFactory.getProxy(getClass().getClassLoader()); Method method = ReflectionUtils.findMethod(InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class); StaticApplicationListenerMethodAdapter listener = new StaticApplicationListenerMethodAdapter(method, bean); this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage("handleIt2"); listener.onApplicationEvent(createGenericTestEvent("test")); }
public Visitor findVisitorFromClassName(Object instance) { try { Class<Visitor> visitorClass = nameResolvers.peek().findVisitingClassFromClassName(instance); Object target = visitorClass.newInstance(); if (target instanceof AbstractPatternVisitor) { ((AbstractPatternVisitor) target).setVisitorFactory(this); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addInterface(Visitor.class); proxyFactory.addAdvice(advice); proxyFactory.setTarget(target); return (Visitor) proxyFactory.getProxy(this.getClass().getClassLoader()); } catch (Exception ex) { log.info( "Could not find visitor class for element named [{}]", instance.getClass()); // $NON-NLS-1$ return new ExceptionThrowingVisitor(ex); } }
protected Object buildLazyResolutionProxy( final DependencyDescriptor descriptor, final String beanName) { Assert.state( getBeanFactory() instanceof DefaultListableBeanFactory, "BeanFactory needs to be a DefaultListableBeanFactory"); final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory(); TargetSource ts = new TargetSource() { @Override public Class<?> getTargetClass() { return descriptor.getDependencyType(); } @Override public boolean isStatic() { return false; } @Override public Object getTarget() { Object target = beanFactory.doResolveDependency(descriptor, beanName, null, null); if (target == null) { throw new NoSuchBeanDefinitionException( descriptor.getDependencyType(), "Optional dependency not present for lazy injection point"); } return target; } @Override public void releaseTarget(Object target) {} }; ProxyFactory pf = new ProxyFactory(); pf.setTargetSource(ts); Class<?> dependencyType = descriptor.getDependencyType(); if (dependencyType.isInterface()) { pf.addInterface(dependencyType); } return pf.getProxy(beanFactory.getBeanClassLoader()); }
private void initializeProxy() throws Exception { if (proxyFactory == null) { proxyFactory = new ProxyFactory(); TransactionInterceptor advice = new TransactionInterceptor( transactionManager, PropertiesConverter.stringToProperties( "create*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate + "\n*=PROPAGATION_REQUIRED")); if (validateTransactionState) { DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor( new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (TransactionSynchronizationManager.isActualTransactionActive()) { throw new IllegalStateException( "Existing transaction detected in JobRepository. " + "Please fix this and try again (e.g. remove @Transactional annotations from client)."); } return invocation.proceed(); } }); NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); pointcut.addMethodName("create*"); advisor.setPointcut(pointcut); proxyFactory.addAdvisor(advisor); } proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(false); proxyFactory.addInterface(JobRepository.class); proxyFactory.setTarget(getTarget()); } }
public <T> T makeActoid(Object ai) { ProxyFactory pf = new ProxyFactory(ai); pf.addInterface(ActoidRef.class); return makeActoidProxy(ai, pf); }