How to use createTypeVariableMap method of org.easymock.internal.BridgeMethodResolver class

Best Easymock code snippet using org.easymock.internal.BridgeMethodResolver.createTypeVariableMap

Source:BridgeMethodResolver.java Github

copy

Full Screen

...107 * the bridge method108 * @return the bridged method, or <code>null</code> if none found109 */110 private static Method searchCandidates(final List<Method> candidateMethods, final Method bridgeMethod) {111 final Map<TypeVariable<?>, Type> typeParameterMap = createTypeVariableMap(bridgeMethod112 .getDeclaringClass());113 for (int i = 0; i < candidateMethods.size(); i++) {114 final Method candidateMethod = candidateMethods.get(i);115 if (isBridgeMethodFor(bridgeMethod, candidateMethod, typeParameterMap)) {116 return candidateMethod;117 }118 }119 return null;120 }121122 /**123 * Return <code>true</code> if the supplied '<code>candidateMethod</code>'124 * can be consider a validate candidate for the {@link Method} that is125 * {@link Method#isBridge() bridged} by the supplied {@link Method bridge126 * Method}. This method performs inexpensive checks and can be used quickly127 * filter for a set of possible matches.128 */129 private static boolean isBridgedCandidateFor(final Method candidateMethod, final Method bridgeMethod) {130 return (!candidateMethod.isBridge() && !candidateMethod.equals(bridgeMethod)131 && candidateMethod.getName().equals(bridgeMethod.getName()) && candidateMethod132 .getParameterTypes().length == bridgeMethod.getParameterTypes().length);133 }134135 /**136 * Determine whether or not the bridge {@link Method} is the bridge for the137 * supplied candidate {@link Method}.138 */139 private static boolean isBridgeMethodFor(final Method bridgeMethod, final Method candidateMethod,140 final Map<TypeVariable<?>, Type> typeVariableMap) {141 if (isResolvedTypeMatch(candidateMethod, bridgeMethod, typeVariableMap)) {142 return true;143 }144 final Method method = findGenericDeclaration(bridgeMethod);145 return (method != null ? isResolvedTypeMatch(method, candidateMethod, typeVariableMap) : false);146 }147148 /**149 * Search for the generic {@link Method} declaration whose erased signature150 * matches that of the supplied bridge method.151 * 152 * @throws IllegalStateException153 * if the generic declaration cannot be found154 */155 private static Method findGenericDeclaration(final Method bridgeMethod) {156 // Search parent types for method that has same signature as bridge.157 Class<?> superclass = bridgeMethod.getDeclaringClass().getSuperclass();158 while (!Object.class.equals(superclass)) {159 final Method method = searchForMatch(superclass, bridgeMethod);160 if (method != null && !method.isBridge()) {161 return method;162 }163 superclass = superclass.getSuperclass();164 }165166 // Search interfaces.167 final Class<?>[] interfaces = getAllInterfacesForClass(bridgeMethod.getDeclaringClass());168 for (final Class<?> anInterface : interfaces) {169 final Method method = searchForMatch(anInterface, bridgeMethod);170 if (method != null && !method.isBridge()) {171 return method;172 }173 }174175 return null;176 }177178 /**179 * Return <code>true</code> if the {@link Type} signature of both the180 * supplied {@link Method#getGenericParameterTypes() generic Method} and181 * concrete {@link Method} are equal after resolving all182 * {@link TypeVariable TypeVariables} using the supplied183 * {@link #createTypeVariableMap TypeVariable Map}, otherwise returns184 * <code>false</code>.185 */186 private static boolean isResolvedTypeMatch(final Method genericMethod, final Method candidateMethod,187 final Map<TypeVariable<?>, Type> typeVariableMap) {188 final Type[] genericParameters = genericMethod.getGenericParameterTypes();189 final Class<?>[] candidateParameters = candidateMethod.getParameterTypes();190 if (genericParameters.length != candidateParameters.length) {191 return false;192 }193 for (int i = 0; i < genericParameters.length; i++) {194 final Type genericParameter = genericParameters[i];195 final Class<?> candidateParameter = candidateParameters[i];196 if (candidateParameter.isArray()) {197 // An array type: compare the component type.198 final Type rawType = getRawType(genericParameter, typeVariableMap);199 if (rawType instanceof GenericArrayType) {200 if (!candidateParameter.getComponentType().equals(201 getRawType(((GenericArrayType) rawType).getGenericComponentType(),202 typeVariableMap))) {203 return false;204 }205 break;206 }207 }208 // A non-array type: compare the type itself.209 if (!candidateParameter.equals(getRawType(genericParameter, typeVariableMap))) {210 return false;211 }212 }213 return true;214 }215216 /**217 * Determine the raw type for the given generic parameter type.218 */219 private static Type getRawType(final Type genericType, final Map<TypeVariable<?>, Type> typeVariableMap) {220 if (genericType instanceof TypeVariable<?>) {221 final TypeVariable<?> tv = (TypeVariable<?>) genericType;222 final Type result = typeVariableMap.get(tv);223 return (result != null ? result : Object.class);224 } else if (genericType instanceof ParameterizedType) {225 return ((ParameterizedType) genericType).getRawType();226 } else {227 return genericType;228 }229 }230231 /**232 * If the supplied {@link Class} has a declared {@link Method} whose233 * signature matches that of the supplied {@link Method}, then this matching234 * {@link Method} is returned, otherwise <code>null</code> is returned.235 */236 private static Method searchForMatch(final Class<?> type, final Method bridgeMethod) {237 return findMethod(type, bridgeMethod.getName(), bridgeMethod.getParameterTypes());238 }239240 /**241 * Build a mapping of {@link TypeVariable#getName TypeVariable names} to242 * concrete {@link Class} for the specified {@link Class}. Searches all243 * super types, enclosing types and interfaces.244 */245 private static Map<TypeVariable<?>, Type> createTypeVariableMap(final Class<?> cls) {246 final Map<TypeVariable<?>, Type> typeVariableMap = new HashMap<TypeVariable<?>, Type>();247248 // interfaces249 extractTypeVariablesFromGenericInterfaces(cls.getGenericInterfaces(), typeVariableMap);250251 // super class252 Type genericType = cls.getGenericSuperclass();253 Class<?> type = cls.getSuperclass();254 while (!Object.class.equals(type)) {255 if (genericType instanceof ParameterizedType) {256 final ParameterizedType pt = (ParameterizedType) genericType;257 populateTypeMapFromParameterizedType(pt, typeVariableMap);258 }259 extractTypeVariablesFromGenericInterfaces(type.getGenericInterfaces(), typeVariableMap); ...

Full Screen

Full Screen

createTypeVariableMap

Using AI Code Generation

copy

Full Screen

1import java.lang.reflect.Method;2import java.util.Arrays;3import java.util.Map;4import org.easymock.internal.BridgeMethodResolver;5public class Main {6 public static void main(String[] args) throws Exception {7 Method method = A.class.getMethod("method", Object.class, String.class);8 Method bridgeMethod = A.class.getMethod("method", Object.class, Object.class);9 Map<String, Class<?>> typeVariableMap = BridgeMethodResolver.createTypeVariableMap(method, bridgeMethod);10 System.out.println(typeVariableMap);11 }12}13class A {14 public <T> T method(Object a, T b) {15 return null;16 }17}18{T=class java.lang.String}

Full Screen

Full Screen

createTypeVariableMap

Using AI Code Generation

copy

Full Screen

1package org.easymock.internal;2import java.lang.reflect.Method;3import java.lang.reflect.Type;4import java.util.HashMap;5import java.util.Map;6public class BridgeMethodResolver {7 public static Method findBridgedMethod(final Method method) {8 if (method.isBridge()) {9 final Method[] methods = method.getDeclaringClass().getMethods();10 for (final Method candidateMethod : methods) {11 if (isBridgedCandidateFor(candidateMethod, method)) {12 return candidateMethod;13 }14 }15 }16 return method;17 }18 private static boolean isBridgedCandidateFor(final Method candidateMethod,19 final Method bridgeMethod) {20 return candidateMethod.getName().equals(bridgeMethod.getName())21 && candidateMethod.getParameterTypes().length == bridgeMethod22 .getParameterTypes().length23 && candidateMethod.getReturnType().equals(24 bridgeMethod.getReturnType());25 }26 public static Map<String, Type> createTypeVariableMap(final Method method) {27 final Map<String, Type> typeVariableMap = new HashMap<String, Type>();28 final Type[] genericParameterTypes = method.getGenericParameterTypes();29 final TypeVariable<?>[] typeParameters = method.getDeclaringClass()30 .getTypeParameters();31 for (int i = 0; i < genericParameterTypes.length; i++) {32 final Type genericParameterType = genericParameterTypes[i];33 if (genericParameterType instanceof TypeVariable) {34 final TypeVariable<?> typeVariable = (TypeVariable<?>) genericParameterType;35 typeVariableMap.put(typeVariable.getName(),36 typeParameters[i]);37 }38 }39 return typeVariableMap;40 }41}

Full Screen

Full Screen

createTypeVariableMap

Using AI Code Generation

copy

Full Screen

1import java.lang.reflect.TypeVariable;2import java.util.Map;3import org.easymock.internal.BridgeMethodResolver;4public class CreateTypeVariableMapDemo {5 public static void main(String[] args) {6 Map<TypeVariable<?>, Class<?>> map = BridgeMethodResolver.createTypeVariableMap(String.class);7 System.out.println("Type variable map for java.lang.String class: " + map);8 }9}10Type variable map for java.lang.String class: {}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful