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

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

Source:BridgeMethodResolver.java Github

copy

Full Screen

...74 }7576 // Gather all methods with matching name and parameter size.77 final List<Method> candidateMethods = new ArrayList<Method>();78 final Method[] methods = getAllDeclaredMethods(bridgeMethod.getDeclaringClass());79 for (final Method candidateMethod : methods) {80 if (isBridgedCandidateFor(candidateMethod, bridgeMethod)) {81 candidateMethods.add(candidateMethod);82 }83 }8485 Method result;86 // Now perform simple quick checks.87 if (candidateMethods.size() == 1) {88 result = candidateMethods.get(0);89 } else {90 result = searchCandidates(candidateMethods, bridgeMethod);91 }9293 if (result == null) {94 throw new IllegalStateException("Unable to locate bridged method for bridge method '"95 + bridgeMethod + "'");96 }9798 return result;99 }100101 /**102 * Search for the bridged method in the given candidates.103 * 104 * @param candidateMethods105 * the List of candidate Methods106 * @param bridgeMethod107 * 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);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 }390 return interfaces.toArray(new Class[interfaces.size()]);391 }392393 /**394 * Attempt to find a {@link Method} on the supplied class with the supplied395 * name and parameter types. Searches all superclasses up to396 * <code>Object</code>.397 * <p>398 * Returns <code>null</code> if no {@link Method} can be found.399 * 400 * @param clazz401 * the class to introspect402 * @param name403 * the name of the method404 * @param paramTypes405 * the parameter types of the method406 * @return the Method object, or <code>null</code> if none found407 */408 private static Method findMethod(final Class<?> clazz, final String name, final Class<?>[] paramTypes) {409 assert clazz != null : "Class must not be null";410 assert name != null : "Method name must not be null";411 Class<?> searchType = clazz;412 while (!Object.class.equals(searchType) && searchType != null) {413 final Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType414 .getDeclaredMethods());415 for (final Method method : methods) {416 if (name.equals(method.getName()) && Arrays.equals(paramTypes, method.getParameterTypes())) {417 return method;418 }419 }420 searchType = searchType.getSuperclass();421 }422 return null;423 }424425 /**426 * Get all declared methods on the leaf class and all superclasses. Leaf427 * class methods are included first.428 */429 private static Method[] getAllDeclaredMethods(Class<?> leafClass) {430 final List<Method> list = new LinkedList<Method>();431432 // Keep backing up the inheritance hierarchy.433 do {434 final Method[] methods = leafClass.getDeclaredMethods();435 for (final Method method : methods) {436 list.add(method);437 }438 leafClass = leafClass.getSuperclass();439 } while (leafClass != null);440441 return list.toArray(new Method[list.size()]);442 }443 // ///CLOVER:ON ...

Full Screen

Full Screen

getAllDeclaredMethods

Using AI Code Generation

copy

Full Screen

1 Method[] methods = BridgeMethodResolver.getAllDeclaredMethods(Object.class);2 for (Method method : methods) {3 System.out.println(method.getName());4 }5 }6}

Full Screen

Full Screen

getAllDeclaredMethods

Using AI Code Generation

copy

Full Screen

1public class BridgeMethodResolverTest {2 public static void main(String[] args) throws Exception {3 Method[] methods = BridgeMethodResolver.getAllDeclaredMethods(BridgeMethodResolverTest.class);4 for (Method method : methods) {5 System.out.println(method.getName());6 }7 }8 public void foo() {9 }10 public void foo(String s) {11 }12 public String foo(String s, int i) {13 return s;14 }15 public String foo(int i, String s) {16 return s;17 }18 public String foo(int i, String s, Object o) {19 return s;20 }

Full Screen

Full Screen

getAllDeclaredMethods

Using AI Code Generation

copy

Full Screen

1import org.easymock.internal.BridgeMethodResolver2import java.lang.reflect.Method3import java.lang.reflect.Modifier4class Test {5 public static void main(String[] args) {6 Method[] methods = BridgeMethodResolver.getAllDeclaredMethods(Test.class);7 for (Method method : methods) {8 int modifiers = method.getModifiers();9 System.out.print(Modifier.toString(modifiers));10 System.out.print(" ");11 Class<?> returnType = method.getReturnType();12 System.out.print(returnType.getName());13 System.out.print(" ");14 String name = method.getName();15 System.out.print(name);16 System.out.print("(");17 Class<?>[] parameterTypes = method.getParameterTypes();18 for (int i = 0; i < parameterTypes.length; i++) {19 System.out.print(parameterTypes[i].getName());20 if (i < parameterTypes.length - 1) {21 System.out.print(",");22 }23 }24 System.out.print(")");25 System.out.print(" {");26 System.out.println("}");27 }28 }29}30public void main(java.lang.String[]) {31}32public void wait(long,int) throws java.lang.InterruptedException {33 super.wait(arg0, arg1);34}35public void wait(long) throws java.lang.InterruptedException {36 super.wait(arg0);37}38public void wait() throws java.lang.InterruptedException {39 super.wait();40}41public boolean equals(java.lang.Object) {42 return super.equals(arg0);43}44public java.lang.String toString() {45 return super.toString();46}47public native int hashCode();48public final native java.lang.Class getClass();49public final void notify() {50 super.notify();51}52public final void notifyAll() {53 super.notifyAll();54}

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