How to use BridgeMethodResolver class of org.easymock.internal package

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

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/**
2 * Copyright 2001-2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock.internal;
17
18import java.io.IOException;
19import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationHandler;
21import java.lang.reflect.Method;
22import java.lang.reflect.Modifier;
23import java.util.Arrays;
24import java.util.HashSet;
25import java.util.Set;
26
27import org.easymock.ConstructorArgs;
28
29import com.google.dexmaker.stock.ProxyBuilder;
30
31// ///CLOVER:OFF (sadly not possible to test android with clover)
32/**
33 * Mocks concrete classes for Android's runtime by generating dex files.
34 */
35public final class AndroidClassProxyFactory implements IProxyFactory {
36    public <T> T createProxy(Class<T> toMock, InvocationHandler handler,
37            Method[] mockedMethods, ConstructorArgs constructorArgs) {
38        final MockHandler interceptor = new MockHandler(handler, mockedMethods);
39        try {
40            ProxyBuilder<T> builder = ProxyBuilder.forClass(toMock)
41                    .handler(interceptor);
42            if (constructorArgs != null) {
43                builder.constructorArgTypes(constructorArgs.getConstructor().getParameterTypes())
44                        .constructorArgValues(constructorArgs.getInitArgs());
45            } else {
46                try {
47                    DefaultClassInstantiator instantiator = new DefaultClassInstantiator();
48                    Constructor<?> constructor = instantiator.getConstructorToUse(toMock);
49                    Object[] params = instantiator.getArgsForTypes(constructor.getParameterTypes());
50                    builder.constructorArgTypes(constructor.getParameterTypes())
51                            .constructorArgValues(params);
52                } catch (InstantiationException e) {
53                    throw new RuntimeException("Fail to instantiate mock for " + toMock);
54                }
55            }
56            return builder.build();
57        } catch (IOException e) {
58            throw new RuntimeException("Failed to mock " + toMock, e);
59        }
60    }
61
62    public InvocationHandler getInvocationHandler(Object mock) {
63        MockHandler mockHandler = (MockHandler) ProxyBuilder.getInvocationHandler(mock);
64        return mockHandler.delegate;
65    }
66
67    private static class MockHandler implements InvocationHandler {
68        private final InvocationHandler delegate;
69        private final Set<Method> mockedMethods;
70
71        public MockHandler(InvocationHandler delegate, Method... mockedMethods) {
72            this.delegate = delegate;
73            this.mockedMethods = (mockedMethods != null)
74                    ? new HashSet<Method>(Arrays.asList(mockedMethods))
75                    : null;
76        }
77
78        public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
79            if (method.isBridge()) {
80                method = BridgeMethodResolver.findBridgedMethod(method);
81            }
82
83            // Never intercept EasyMock's own calls to fillInStackTrace
84            boolean internalFillInStackTraceCall = obj instanceof Throwable
85                    && method.getName().equals("fillInStackTrace")
86                    && ClassProxyFactory.isCallerMockInvocationHandlerInvoke(new Throwable());
87
88            if (internalFillInStackTraceCall
89                    || isMocked(method) && !Modifier.isAbstract(method.getModifiers())) {
90                return ProxyBuilder.callSuper(obj, method, args);
91            }
92
93            return delegate.invoke(obj, method, args);
94        }
95
96        private boolean isMocked(Method method) {
97            return mockedMethods != null && !mockedMethods.contains(method);
98        }
99    }
100}
101// ///CLOVER:ON
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on BridgeMethodResolver

    No relevant questions found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)