|
Spring Framework example source code file (BridgeMethodResolverTests.java)
The Spring Framework BridgeMethodResolverTests.java source code/* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.core; import java.io.Serializable; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.TypeVariable; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import junit.framework.TestCase; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional; /** * @author Rob Harrop * @author Juergen Hoeller */ public class BridgeMethodResolverTests extends TestCase { private static TypeVariable findTypeVariable(Class clazz, String name) { TypeVariable[] variables = clazz.getTypeParameters(); for (int i = 0; i < variables.length; i++) { TypeVariable variable = variables[i]; if (variable.getName().equals(name)) { return variable; } } return null; } private static Method findMethodWithReturnType(String name, Class returnType, Class targetType) { Method[] methods = targetType.getMethods(); for (Method m : methods) { if (m.getName().equals(name) && m.getReturnType().equals(returnType)) { return m; } } return null; } public void testFindBridgedMethod() throws Exception { Method unbridged = MyFoo.class.getDeclaredMethod("someMethod", String.class, Object.class); Method bridged = MyFoo.class.getDeclaredMethod("someMethod", Serializable.class, Object.class); assertFalse(unbridged.isBridge()); assertTrue(bridged.isBridge()); assertEquals("Unbridged method not returned directly", unbridged, BridgeMethodResolver.findBridgedMethod(unbridged)); assertEquals("Incorrect bridged method returned", unbridged, BridgeMethodResolver.findBridgedMethod(bridged)); } public void testFindBridgedVarargMethod() throws Exception { Method unbridged = MyFoo.class.getDeclaredMethod("someVarargMethod", String.class, Object[].class); Method bridged = MyFoo.class.getDeclaredMethod("someVarargMethod", Serializable.class, Object[].class); assertFalse(unbridged.isBridge()); assertTrue(bridged.isBridge()); assertEquals("Unbridged method not returned directly", unbridged, BridgeMethodResolver.findBridgedMethod(unbridged)); assertEquals("Incorrect bridged method returned", unbridged, BridgeMethodResolver.findBridgedMethod(bridged)); } public void testFindBridgedMethodInHierarchy() throws Exception { Method unbridged = DateAdder.class.getMethod("add", Date.class); Method bridged = DateAdder.class.getMethod("add", Object.class); assertFalse(unbridged.isBridge()); assertTrue(bridged.isBridge()); assertEquals("Unbridged method not returned directly", unbridged, BridgeMethodResolver.findBridgedMethod(unbridged)); assertEquals("Incorrect bridged method returned", unbridged, BridgeMethodResolver.findBridgedMethod(bridged)); } public void testIsBridgeMethodFor() throws Exception { Map typeParameterMap = GenericTypeResolver.getTypeVariableMap(MyBar.class); Method bridged = MyBar.class.getDeclaredMethod("someMethod", String.class, Object.class); Method other = MyBar.class.getDeclaredMethod("someMethod", Integer.class, Object.class); Method bridge = MyBar.class.getDeclaredMethod("someMethod", Object.class, Object.class); assertTrue("Should be bridge method", BridgeMethodResolver.isBridgeMethodFor(bridge, bridged, typeParameterMap)); assertFalse("Should not be bridge method", BridgeMethodResolver.isBridgeMethodFor(bridge, other, typeParameterMap)); } public void testCreateTypeVariableMap() throws Exception { Map<String, Class> typeVariableMap = GenericTypeResolver.getTypeVariableMap(MyBar.class); TypeVariable barT = findTypeVariable(InterBar.class, "T"); assertEquals(String.class, typeVariableMap.get(barT)); typeVariableMap = GenericTypeResolver.getTypeVariableMap(MyFoo.class); TypeVariable fooT = findTypeVariable(Foo.class, "T"); assertEquals(String.class, typeVariableMap.get(fooT)); typeVariableMap = GenericTypeResolver.getTypeVariableMap(ExtendsEnclosing.ExtendsEnclosed.ExtendsReallyDeepNow.class); TypeVariable r = findTypeVariable(Enclosing.Enclosed.ReallyDeepNow.class, "R"); TypeVariable s = findTypeVariable(Enclosing.Enclosed.class, "S"); TypeVariable t = findTypeVariable(Enclosing.class, "T"); assertEquals(Long.class, typeVariableMap.get(r)); assertEquals(Integer.class, typeVariableMap.get(s)); assertEquals(String.class, typeVariableMap.get(t)); } public void testDoubleParameterization() throws Exception { Method objectBridge = MyBoo.class.getDeclaredMethod("foo", Object.class); Method serializableBridge = MyBoo.class.getDeclaredMethod("foo", Serializable.class); Method stringFoo = MyBoo.class.getDeclaredMethod("foo", String.class); Method integerFoo = MyBoo.class.getDeclaredMethod("foo", Integer.class); assertEquals("foo(String) not resolved.", stringFoo, BridgeMethodResolver.findBridgedMethod(objectBridge)); assertEquals("foo(Integer) not resolved.", integerFoo, BridgeMethodResolver.findBridgedMethod(serializableBridge)); } public void testFindBridgedMethodFromMultipleBridges() throws Exception { Method loadWithObjectReturn = findMethodWithReturnType("load", Object.class, SettingsDaoImpl.class); assertNotNull(loadWithObjectReturn); Method loadWithSettingsReturn = findMethodWithReturnType("load", Settings.class, SettingsDaoImpl.class); assertNotNull(loadWithSettingsReturn); assertNotSame(loadWithObjectReturn, loadWithSettingsReturn); Method method = SettingsDaoImpl.class.getMethod("load"); assertNotNull(method); assertEquals(method, BridgeMethodResolver.findBridgedMethod(loadWithObjectReturn)); assertEquals(method, BridgeMethodResolver.findBridgedMethod(loadWithSettingsReturn)); } public void testFindBridgedMethodFromParent() throws Exception { Method loadFromParentBridge = SettingsDaoImpl.class.getMethod("loadFromParent"); assertNotNull(loadFromParentBridge); assertTrue(loadFromParentBridge.isBridge()); Method loadFromParent = AbstractDaoImpl.class.getMethod("loadFromParent"); assertNotNull(loadFromParent); assertFalse(loadFromParent.isBridge()); assertEquals(loadFromParent, BridgeMethodResolver.findBridgedMethod(loadFromParentBridge)); } public void testWithSingleBoundParameterizedOnInstantiate() throws Exception { Method bridgeMethod = DelayQueue.class.getMethod("add", Object.class); assertTrue(bridgeMethod.isBridge()); Method actualMethod = DelayQueue.class.getMethod("add", Delayed.class); assertFalse(actualMethod.isBridge()); assertEquals(actualMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testWithDoubleBoundParameterizedOnInstantiate() throws Exception { Method bridgeMethod = SerializableBounded.class.getMethod("boundedOperation", Object.class); assertTrue(bridgeMethod.isBridge()); Method actualMethod = SerializableBounded.class.getMethod("boundedOperation", HashMap.class); assertFalse(actualMethod.isBridge()); assertEquals(actualMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testWithGenericParameter() throws Exception { Method[] methods = StringGenericParameter.class.getMethods(); Method bridgeMethod = null; Method bridgedMethod = null; for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if ("getFor".equals(method.getName()) && !method.getParameterTypes()[0].equals(Integer.class)) { if (method.getReturnType().equals(Object.class)) { bridgeMethod = method; } else { bridgedMethod = method; } } } assertNotNull("bridgedMethod should not be null", bridgedMethod); assertNotNull("bridgeMethod should not be null", bridgeMethod); assertTrue(bridgeMethod.isBridge()); assertFalse(bridgedMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testOnAllMethods() throws Exception { Method[] methods = StringList.class.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; assertNotNull(BridgeMethodResolver.findBridgedMethod(method)); } } public void testSPR2583() throws Exception { Method bridgedMethod = MessageBroadcasterImpl.class.getMethod("receive", MessageEvent.class); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = MessageBroadcasterImpl.class.getMethod("receive", Event.class); assertTrue(bridgeMethod.isBridge()); Method otherMethod = MessageBroadcasterImpl.class.getMethod("receive", NewMessageEvent.class); assertFalse(otherMethod.isBridge()); Map typeVariableMap = GenericTypeResolver.getTypeVariableMap(MessageBroadcasterImpl.class); assertFalse("Match identified incorrectly", BridgeMethodResolver.isBridgeMethodFor(bridgeMethod, otherMethod, typeVariableMap)); assertTrue("Match not found correctly", BridgeMethodResolver.isBridgeMethodFor(bridgeMethod, bridgedMethod, typeVariableMap)); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR2454() throws Exception { Map typeVariableMap = GenericTypeResolver.getTypeVariableMap(YourHomer.class); TypeVariable variable = findTypeVariable(MyHomer.class, "L"); assertEquals(AbstractBounded.class, ((ParameterizedType) typeVariableMap.get(variable)).getRawType()); } public void testSPR2603() throws Exception { Method objectBridge = YourHomer.class.getDeclaredMethod("foo", Bounded.class); Method abstractBoundedFoo = YourHomer.class.getDeclaredMethod("foo", AbstractBounded.class); Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(objectBridge); assertEquals("foo(AbstractBounded) not resolved.", abstractBoundedFoo, bridgedMethod); } public void testSPR2648() throws Exception { Method bridgeMethod = GenericSqlMapIntegerDao.class.getDeclaredMethod("saveOrUpdate", Object.class); assertTrue(bridgeMethod.isBridge()); Method bridgedMethod = GenericSqlMapIntegerDao.class.getDeclaredMethod("saveOrUpdate", Integer.class); assertFalse(bridgedMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR2763() throws Exception { Method bridgedMethod = AbstractDao.class.getDeclaredMethod("save", Object.class); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = UserDaoImpl.class.getDeclaredMethod("save", User.class); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR3041() throws Exception { Method bridgedMethod = BusinessDao.class.getDeclaredMethod("save", Business.class); assertNotNull(bridgedMethod); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = BusinessDao.class.getDeclaredMethod("save", Object.class); assertNotNull(bridgeMethod); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR3173() throws Exception { Method bridgedMethod = UserDaoImpl.class.getDeclaredMethod("saveVararg", User.class, Object[].class); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = UserDaoImpl.class.getDeclaredMethod("saveVararg", Object.class, Object[].class); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR3304() throws Exception { Method bridgedMethod = MegaMessageProducerImpl.class.getDeclaredMethod("receive", MegaMessageEvent.class); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = MegaMessageProducerImpl.class.getDeclaredMethod("receive", MegaEvent.class); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR3324() throws Exception { Method bridgedMethod = BusinessDao.class.getDeclaredMethod("get", Long.class); assertNotNull(bridgedMethod); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = BusinessDao.class.getDeclaredMethod("get", Object.class); assertNotNull(bridgeMethod); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR3357() throws Exception { Method bridgedMethod = ExtendsAbstractImplementsInterface.class.getDeclaredMethod( "doSomething", DomainObjectExtendsSuper.class, Object.class); assertNotNull(bridgedMethod); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = ExtendsAbstractImplementsInterface.class.getDeclaredMethod( "doSomething", DomainObjectSuper.class, Object.class); assertNotNull(bridgeMethod); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR3485() throws Exception { Method bridgedMethod = DomainObject.class.getDeclaredMethod( "method2", ParameterType.class, byte[].class); assertNotNull(bridgedMethod); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = DomainObject.class.getDeclaredMethod( "method2", Serializable.class, Object.class); assertNotNull(bridgeMethod); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public void testSPR3534() throws Exception { Method bridgedMethod = TestEmailProvider.class.getDeclaredMethod( "findBy", EmailSearchConditions.class); assertNotNull(bridgedMethod); assertFalse(bridgedMethod.isBridge()); Method bridgeMethod = TestEmailProvider.class.getDeclaredMethod( "findBy", Object.class); assertNotNull(bridgeMethod); assertTrue(bridgeMethod.isBridge()); assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod)); } public static interface Foo<T extends Serializable> { void someMethod(T theArg, Object otherArg); void someVarargMethod(T theArg, Object... otherArg); } public static class MyFoo implements Foo<String> { public void someMethod(Integer theArg, Object otherArg) { } public void someMethod(String theArg, Object otherArg) { } public void someVarargMethod(String theArg, Object... otherArgs) { } } public static abstract class Bar<T> { void someMethod(Map m, Object otherArg) { } void someMethod(T theArg, Map m) { } abstract void someMethod(T theArg, Object otherArg); } public static abstract class InterBar<T> extends Bar Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework BridgeMethodResolverTests.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.