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

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

Source:BridgeMethodResolver.java Github

copy

Full Screen

...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);260 genericType = type.getGenericSuperclass();261 type = type.getSuperclass();262 }263264 // enclosing class265 type = cls;266 while (type.isMemberClass()) {267 genericType = type.getGenericSuperclass();268 if (genericType instanceof ParameterizedType) {269 final ParameterizedType pt = (ParameterizedType) genericType;270 populateTypeMapFromParameterizedType(pt, typeVariableMap);271 }272 type = type.getEnclosingClass();273 }274275 return typeVariableMap;276 }277278 private static void extractTypeVariablesFromGenericInterfaces(final Type[] genericInterfaces,279 final Map<TypeVariable<?>, Type> typeVariableMap) {280 for (final Type genericInterface : genericInterfaces) {281 if (genericInterface instanceof ParameterizedType) {282 final ParameterizedType pt = (ParameterizedType) genericInterface;283 populateTypeMapFromParameterizedType(pt, typeVariableMap);284 if (pt.getRawType() instanceof Class<?>) {285 extractTypeVariablesFromGenericInterfaces(((Class<?>) pt.getRawType())286 .getGenericInterfaces(), typeVariableMap);287 }288 } else if (genericInterface instanceof Class<?>) {289 extractTypeVariablesFromGenericInterfaces(((Class<?>) genericInterface)290 .getGenericInterfaces(), typeVariableMap);291 }292 }293 }294295 /**296 * Read the {@link TypeVariable TypeVariables} from the supplied297 * {@link ParameterizedType} and add mappings corresponding to the298 * {@link TypeVariable#getName TypeVariable name} -> concrete type to the299 * supplied {@link Map}.300 * <p>301 * Consider this case:302 * 303 * <pre class="code>304 * public interface Foo&lt;S, T&gt; {305 * ..306 * }307 * 308 * public class FooImpl implements Foo&lt;String, Integer&gt; {309 * ..310 * }311 * </pre>312 * 313 * For '<code>FooImpl</code>' the following mappings would be added to the314 * {@link Map}: {S=java.lang.String, T=java.lang.Integer}.315 */316 private static void populateTypeMapFromParameterizedType(final ParameterizedType type,317 final Map<TypeVariable<?>, Type> typeVariableMap) {318 if (type.getRawType() instanceof Class<?>) {319 final Type[] actualTypeArguments = type.getActualTypeArguments();320 final TypeVariable<?>[] typeVariables = ((Class<?>) type.getRawType()).getTypeParameters();321 for (int i = 0; i < actualTypeArguments.length; i++) {322 final Type actualTypeArgument = actualTypeArguments[i];323 final TypeVariable<?> variable = typeVariables[i];324 if (actualTypeArgument instanceof Class<?>) {325 typeVariableMap.put(variable, actualTypeArgument);326 } else if (actualTypeArgument instanceof GenericArrayType) {327 typeVariableMap.put(variable, actualTypeArgument);328 } else if (actualTypeArgument instanceof ParameterizedType) {329 typeVariableMap.put(variable, ((ParameterizedType) actualTypeArgument).getRawType());330 } else if (actualTypeArgument instanceof TypeVariable<?>) {331 // We have a type that is parameterized at instantiation time332 // the nearest match on the bridge method will be the bounded type.333 final TypeVariable<?> typeVariableArgument = (TypeVariable<?>) actualTypeArgument;334 Type resolvedType = typeVariableMap.get(typeVariableArgument);335 if (resolvedType == null) {336 resolvedType = extractClassForTypeVariable(typeVariableArgument);337 }338 if (resolvedType != null) {339 typeVariableMap.put(variable, resolvedType);340 }341 }342 }343 }344 }345346 /**347 * Extracts the bound '<code>Class</code>' for a give {@link TypeVariable}.348 */349 private static Class<?> extractClassForTypeVariable(final TypeVariable<?> typeVariable) {350 final Type[] bounds = typeVariable.getBounds();351 Type result = null;352 if (bounds.length > 0) {353 final Type bound = bounds[0];354 if (bound instanceof ParameterizedType) {355 result = ((ParameterizedType) bound).getRawType();356 } else if (bound instanceof Class<?>) {357 result = bound;358 } else if (bound instanceof TypeVariable<?>) {359 result = extractClassForTypeVariable((TypeVariable<?>) bound);360 }361 }362 return (result instanceof Class<?> ? (Class<?>) result : null);363 }364365 /**366 * Return all interfaces that the given class implements as array, including367 * ones implemented by superclasses.368 * <p>369 * If the class itself is an interface, it gets returned as sole interface.370 * 371 * @param clazz372 * the class to analyse for interfaces373 * @return all interfaces that the given object implements as array374 */375 private static Class<?>[] getAllInterfacesForClass(Class<?> clazz) {376 assert clazz != null : "Class must not be null";377 if (clazz.isInterface()) {378 return new Class[] { clazz };379 }380 final List<Class<?>> interfaces = new ArrayList<Class<?>>();381 while (clazz != null) {382 for (int i = 0; i < clazz.getInterfaces().length; i++) {383 final Class<?> ifc = clazz.getInterfaces()[i];384 if (!interfaces.contains(ifc)) {385 interfaces.add(ifc);386 }387 }388 clazz = clazz.getSuperclass();389 } ...

Full Screen

Full Screen

getAllInterfacesForClass

Using AI Code Generation

copy

Full Screen

1import org.easymock.internal.BridgeMethodResolver2import org.easymock.internal.ClassProxyFactory3import org.easymock.internal.ClassProxyFactoryRegistry4import org.easymock.internal.MockBuilder5import org.easymock.internal.MocksControl6import org.easymock.internal.MocksControl.MockType7import org.easymock.internal.MocksControl.State8import org.easymock.internal.MocksControl.State.*9import org.easymock.internal.ObjectMethodsFilter10import org.easymock.internal.ProxyFactory11import org.easymock.internal.ReplayState12import org.easymock.internal.ReplayState.*13import org.easymock.internal.StateVerificationMode14import org.easymock.internal.StateVerificationMode.*15import org.easymock.internal.UnorderedVerifyState16import org.easymock.internal.UnorderedVerifyState.*17import org.easymock.internal.VerifyState18import org.easymock.internal.VerifyState.*19import org.easymock.internal.WeakHashMap20import org.easymock.internal.matchers.*21import org.easymock.internal.matchers.And22import org.easymock.inte

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