/** * Check whether the given object is a JDK dynamic proxy. * <p>This method goes beyond the implementation of * {@link Proxy#isProxyClass(Class)} by additionally checking if the * given object is an instance of {@link SpringProxy}. * @param object the object to check * @see java.lang.reflect.Proxy#isProxyClass */ public static boolean isJdkDynamicProxy(@Nullable Object object) { return (object instanceof SpringProxy && Proxy.isProxyClass(object.getClass())); }
private boolean isProxyForSameBshObject(Object other) { if (!Proxy.isProxyClass(other.getClass())) { return false; } InvocationHandler ih = Proxy.getInvocationHandler(other); return (ih instanceof BshObjectInvocationHandler && this.xt.equals(((BshObjectInvocationHandler) ih).xt)); } }
private static boolean isProxyOfSameInterfaces(Object arg, Class<?> proxyClass) { return proxyClass.isInstance(arg) // Equal proxy instances should mostly be instance of proxyClass // Under some edge cases (such as the proxy of JDK types serialized and then deserialized) // the proxy type may not be the same. // We first check isProxyClass() so that the common case of comparing with non-proxy objects // is efficient. || (Proxy.isProxyClass(arg.getClass()) && Arrays.equals(arg.getClass().getInterfaces(), proxyClass.getInterfaces())); } }
/** * Check whether the given object is a JDK dynamic proxy or a CGLIB proxy. * <p>This method additionally checks if the given object is an instance * of {@link SpringProxy}. * @param object the object to check * @see #isJdkDynamicProxy * @see #isCglibProxy */ public static boolean isAopProxy(@Nullable Object object) { return (object instanceof SpringProxy && (Proxy.isProxyClass(object.getClass()) || ClassUtils.isCglibProxyClass(object.getClass()))); }
public boolean hasProxyTarget() { return (this.target != null && Proxy.isProxyClass(this.target.getType())); }
public CacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass, KeyGenerator keyGenerator, CacheResolver cacheResolver) { this.operation = operation; this.method = BridgeMethodResolver.findBridgedMethod(method); this.targetClass = targetClass; this.targetMethod = (!Proxy.isProxyClass(targetClass) ? AopUtils.getMostSpecificMethod(method, targetClass) : this.method); this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass); this.keyGenerator = keyGenerator; this.cacheResolver = cacheResolver; } }
@Override public boolean matches(Method method, Class<?> targetClass) { if (matchesMethod(method)) { return true; } // Proxy classes never have annotations on their redeclared methods. if (Proxy.isProxyClass(targetClass)) { return false; } // The method may be on an interface, so let's check on the target class as well. Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); return (specificMethod != method && matchesMethod(specificMethod)); }
@Test public void testIsDynamicProxyWhenInterfaceSpecified() { ITestBean test1 = (ITestBean) factory.getBean("test1"); assertTrue("test1 is a dynamic proxy", Proxy.isProxyClass(test1.getClass())); }
@Test public void testIsDynamicProxyWhenInterfaceSpecifiedForPrototype() { ITestBean test1 = (ITestBean) factory.getBean("test2"); assertTrue("test2 is a dynamic proxy", Proxy.isProxyClass(test1.getClass())); }
@Test public void testIsDynamicProxyWhenAutodetectingInterfaces() { ITestBean test1 = (ITestBean) factory.getBean("test3"); assertTrue("test3 is a dynamic proxy", Proxy.isProxyClass(test1.getClass())); }
@Test public void testIsDynamicProxyWhenAutodetectingInterfacesForPrototype() { ITestBean test1 = (ITestBean) factory.getBean("test4"); assertTrue("test4 is a dynamic proxy", Proxy.isProxyClass(test1.getClass())); }
@Test @SuppressWarnings("unchecked") public void testMultipleResults() { // Add with JDBC String firstName = "Tony"; insertPerson(firstName); assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass())); Query q = sharedEntityManager.createQuery("select p from Person as p"); List<Person> people = q.getResultList(); assertEquals(1, people.size()); assertEquals(firstName, people.get(0).getFirstName()); }
@Test @SuppressWarnings("unchecked") public void testEntityManagerProxyIsProxy() { assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass())); Query q = sharedEntityManager.createQuery("select p from Person as p"); q.getResultList(); assertTrue("Should be open to start with", sharedEntityManager.isOpen()); sharedEntityManager.close(); assertTrue("Close should have been silently ignored", sharedEntityManager.isOpen()); }
@Test @SuppressWarnings("unchecked") public void testEntityManagerProxyIsProxy() { EntityManager em = entityManagerFactory.createEntityManager(); assertTrue(Proxy.isProxyClass(em.getClass())); Query q = em.createQuery("select p from Person as p"); List<Person> people = q.getResultList(); assertNotNull(people); assertTrue("Should be open to start with", em.isOpen()); em.close(); assertFalse("Close should work on application managed EM", em.isOpen()); }
@Override public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class<?> targetClass = config.getTargetClass(); if (targetClass == null) { throw new AopConfigException("TargetSource cannot determine target class: " + "Either an interface or a target is required for proxy creation."); } if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) { return new JdkDynamicAopProxy(config); } return new ObjenesisCglibAopProxy(config); } else { return new JdkDynamicAopProxy(config); } }
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) { this.beanName = beanName; this.method = BridgeMethodResolver.findBridgedMethod(method); this.targetMethod = (!Proxy.isProxyClass(targetClass) ? AopUtils.getMostSpecificMethod(method, targetClass) : this.method); this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass); EventListener ann = AnnotatedElementUtils.findMergedAnnotation(this.targetMethod, EventListener.class); this.declaredEventTypes = resolveDeclaredEventTypes(method, ann); this.condition = (ann != null ? ann.condition() : null); this.order = resolveOrder(this.targetMethod); }
@Test public void testCircularCollectionBeansStartingWithSet() { this.beanFactory.getBean("circularSet"); TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean"); List list = bean.getSomeList(); assertFalse(Proxy.isProxyClass(list.getClass())); assertEquals(1, list.size()); assertEquals(bean, list.get(0)); Set set = bean.getSomeSet(); assertTrue(Proxy.isProxyClass(set.getClass())); assertEquals(1, set.size()); assertTrue(set.contains(bean)); Map map = bean.getSomeMap(); assertFalse(Proxy.isProxyClass(map.getClass())); assertEquals(1, map.size()); assertEquals(bean, map.get("foo")); }
@Test public void testCircularCollectionBeansStartingWithMap() { this.beanFactory.getBean("circularMap"); TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean"); List list = bean.getSomeList(); assertFalse(Proxy.isProxyClass(list.getClass())); assertEquals(1, list.size()); assertEquals(bean, list.get(0)); Set set = bean.getSomeSet(); assertFalse(Proxy.isProxyClass(set.getClass())); assertEquals(1, set.size()); assertTrue(set.contains(bean)); Map map = bean.getSomeMap(); assertTrue(Proxy.isProxyClass(map.getClass())); assertEquals(1, map.size()); assertEquals(bean, map.get("foo")); }
@Test public void testGetsAreNotTransactionalWithProxyFactory1() { ITestBean testBean = (ITestBean) factory.getBean("proxyFactory1"); assertTrue("testBean is a dynamic proxy", Proxy.isProxyClass(testBean.getClass())); assertFalse(testBean instanceof TransactionalProxy); doTestGetsAreNotTransactional(testBean); }
@Test public void testGetsAreNotTransactionalWithProxyFactory2DynamicProxy() { this.factory.preInstantiateSingletons(); ITestBean testBean = (ITestBean) factory.getBean("proxyFactory2DynamicProxy"); assertTrue("testBean is a dynamic proxy", Proxy.isProxyClass(testBean.getClass())); assertTrue(testBean instanceof TransactionalProxy); doTestGetsAreNotTransactional(testBean); }