/** * Determine a TargetSource for the given target (or TargetSource). * @param target target. If this is an implementation of TargetSource it is * used as our TargetSource; otherwise it is wrapped in a SingletonTargetSource. * @return a TargetSource for this object */ protected TargetSource createTargetSource(Object target) { if (target instanceof TargetSource) { return (TargetSource) target; } else { return new SingletonTargetSource(target); } }
/** * Set the given object as target. * Will create a SingletonTargetSource for the object. * @see #setTargetSource * @see org.springframework.aop.target.SingletonTargetSource */ public void setTarget(Object target) { setTargetSource(new SingletonTargetSource(target)); }
/** * Return a TargetSource to use when creating a proxy. If the target was not * specified at the end of the interceptorNames list, the TargetSource will be * this class's TargetSource member. Otherwise, we get the target bean and wrap * it in a TargetSource if necessary. */ private TargetSource freshTargetSource() { if (this.targetName == null) { if (logger.isTraceEnabled()) { logger.trace("Not refreshing target: Bean name not specified in 'interceptorNames'."); } return this.targetSource; } else { if (this.beanFactory == null) { throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " + "- cannot resolve target with name '" + this.targetName + "'"); } if (logger.isDebugEnabled()) { logger.debug("Refreshing target with name '" + this.targetName + "'"); } Object target = this.beanFactory.getBean(this.targetName); return (target instanceof TargetSource ? (TargetSource) target : new SingletonTargetSource(target)); } }
@Override public ITestBean getObject() { return ProxyFactory.getProxy(ITestBean.class, new SingletonTargetSource(tb)); }
/** * Wrap the given bean if necessary, i.e. if it is eligible for being proxied. * @param bean the raw bean instance * @param beanName the name of the bean * @param cacheKey the cache key for metadata access * @return a proxy wrapping the bean, or the raw bean instance as-is */ protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) { return bean; } if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) { return bean; } if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) { this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; } // Create proxy if we have advice. Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); if (specificInterceptors != DO_NOT_PROXY) { this.advisedBeans.put(cacheKey, Boolean.TRUE); Object proxy = createProxy( bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); this.proxyTypes.put(cacheKey, proxy.getClass()); return proxy; } this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; }
/** * Replaces this object with a SingletonTargetSource on serialization. * Protected as otherwise it won't be invoked for subclasses. * (The {@code writeReplace()} method must be visible to the class * being serialized.) * <p>With this implementation of this method, there is no need to mark * non-serializable fields in this class or subclasses as transient. */ protected Object writeReplace() throws ObjectStreamException { if (logger.isDebugEnabled()) { logger.debug("Disconnecting TargetSource [" + this + "]"); } try { // Create disconnected SingletonTargetSource/EmptyTargetSource. Object target = getTarget(); return (target != null ? new SingletonTargetSource(target) : EmptyTargetSource.forClass(getTargetClass())); } catch (Exception ex) { String msg = "Cannot get target for disconnecting TargetSource [" + this + "]"; logger.error(msg, ex); throw new NotSerializableException(msg + ": " + ex); } }
public Object getObject ( final String command ) throws Exception { final Object templates = Gadgets.createTemplatesImpl(command); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(new SingletonTargetSource(templates)); final Type typeTemplatesProxy = Gadgets.createProxy( (InvocationHandler) Reflections.getFirstCtor("org.springframework.aop.framework.JdkDynamicAopProxy").newInstance(as), Type.class, Templates.class); final Object typeProviderProxy = Gadgets.createMemoitizedProxy( Gadgets.createMap("getType", typeTemplatesProxy), forName("org.springframework.core.SerializableTypeWrapper$TypeProvider")); Object mitp = Reflections.createWithoutConstructor(forName("org.springframework.core.SerializableTypeWrapper$MethodInvokeTypeProvider")); Reflections.setFieldValue(mitp, "provider", typeProviderProxy); Reflections.setFieldValue(mitp, "methodName", "newTransformer"); return mitp; }
@Test // SPR-11225 public void resolveArgumentTypeVariableWithNonGenericConverter() throws Exception { Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class); HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method); MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "{\"name\" : \"Jad\"}"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List<HttpMessageConverter<?>> converters = new ArrayList<>(); HttpMessageConverter<Object> target = new MappingJackson2HttpMessageConverter(); HttpMessageConverter<?> proxy = ProxyFactory.getProxy(HttpMessageConverter.class, new SingletonTargetSource(target)); converters.add(proxy); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); SimpleBean result = (SimpleBean) processor.resolveArgument(methodParam, container, request, factory); assertNotNull(result); assertEquals("Jad", result.getName()); }
@Test public void testCanUnwrapAopProxy() { EntityManager em = entityManagerFactory.createEntityManager(); EntityManager proxy = ProxyFactory.getProxy(EntityManager.class, new SingletonTargetSource(em)); assertTrue(em instanceof org.hibernate.jpa.HibernateEntityManager); assertFalse(proxy instanceof org.hibernate.jpa.HibernateEntityManager); assertTrue(proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class) != null); assertSame(em, proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class)); assertSame(em.getDelegate(), proxy.getDelegate()); }
SingletonTargetSource sts = new SingletonTargetSource(tb2); advised.setTargetSource(sts); assertEquals(tb2.getName(), proxy.getName());
protected TargetSource createTargetSource(Object target) { if (target instanceof TargetSource) return (TargetSource) target; else return new SingletonTargetSource(target); }
/** * Set the given object as target. * Will create a SingletonTargetSource for the object. * @see #setTargetSource * @see org.springframework.aop.target.SingletonTargetSource */ public void setTarget(Object target) { setTargetSource(new SingletonTargetSource(target)); }
/** * Set the given object as target. * Will create a SingletonTargetSource for the object. * @see #setTargetSource * @see org.springframework.aop.target.SingletonTargetSource */ public void setTarget(Object target) { setTargetSource(new SingletonTargetSource(target)); }
/** * Set the given object as target. * Will create a SingletonTargetSource for the object. * @see #setTargetSource * @see org.springframework.aop.target.SingletonTargetSource */ public void setTarget(Object target) { setTargetSource(new SingletonTargetSource(target)); }
protected <T> T wrapHandler(Class<T> interfaceClass, T handler) { ProxyFactory proxyFactory = new ProxyFactory(interfaceClass, new SingletonTargetSource(handler)); thriftConfigurer.configureProxyFactory(proxyFactory); //TODO remove from here? proxyFactory.setFrozen(true); return (T) proxyFactory.getProxy(); } }
private Object wrapHandler(Class<?> ifaceClass, Object handler, String path, String type) { ProxyFactory proxyFactory = new ProxyFactory(ifaceClass, new SingletonTargetSource(handler)); thriftConfigurer.configureProxyFactory(proxyFactory, path, type); //TODO remove from here? proxyFactory.setFrozen(true); return proxyFactory.getProxy(); }
public void testMethodACL() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testMethod", new Class[] {}); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_METHOD.andy", "ACL_METHOD.BANANA"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] {}); }
public void testAllowNullStore() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testOneStoreRef", new Class[] { StoreRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { null }); }
public void testAllowNullParent() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testOneChildAssociationRef", new Class[] { ChildAssociationRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_PARENT.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { null }); }
public void testAllowNullParentOnRealChildAssoc() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testOneChildAssociationRef", new Class[] { ChildAssociationRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_PARENT.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef) }); }