This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private int registerClientStubBeans(DefaultListableBeanFactory beanFactory) { | |
| addClasspathDependencies(); | |
| List<Bean> clientStubBeans = getClientStubBeans(); | |
| int count = 0; | |
| for (Bean bean : clientStubBeans) { | |
| beanFactory.registerBeanDefinition(bean.beanName, bean.beanDefinition); | |
| count++; | |
| } | |
| return count; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| public class ClientStubPostProcessor implements BeanFactoryPostProcessor { | |
| private Logger logger; | |
| @Override | |
| public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | |
| logger = Logger.getLogger(ClientStubPostProcessor.class); | |
| // Don't register stubs if testing against localhost | |
| if (isTestingLocally()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MyClass myClass = new MyClass(); | |
| MyClass proxy = (MyClass) Enhancer.create( | |
| MyClass.class, | |
| new HelloWorldInvocationHandler(myClass)); | |
| proxy.foo(42); // "Hello World" is printed and then 42 is passed to foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List myList = new ArrayList(); | |
| List proxy = (List) Proxy.newProxyInstance( | |
| this.getClass().getClassLoader(), | |
| new Class[] { List.class }, | |
| new HelloWorldInvocationHandler(myList)); | |
| proxy.add(42); // "Hello World" is printed and then 42 is added to the List |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HelloWorldInvocationHandler implements InvocationHandler { | |
| private final Object proxied; | |
| public HelloWorldInvocationHandler(Object proxied) { | |
| this.proxied = proxied; | |
| } | |
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
| System.out.println("Hello World"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public Object invoke(Object proxy, Method method, Object[] args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MyClass myClass = new MyClass(); | |
| MyClass proxy = ProxyBuilder.forClass(MyClass.class) | |
| .dexCache(getInstrumentation().getTargetContext().getDir("dx", Context.MODE_PRIVATE)) | |
| .handler(new HelloWorldInvocationHandler(myClass)) | |
| .build(); | |
| proxy.foo(42); // "Hello World" is printed and then 42 is passed to foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FooEntity foo = (FooEntity) Enhancer.create( | |
| FooEntity.class, | |
| new LazyLoader() { | |
| public Object loadObject() throws Exception { | |
| return Datastore.load(fooId); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FooEntity foo = (FooEntity) Enhancer.create( | |
| FooEntity.class, | |
| new LazilyLoadedObject() { | |
| protected Object loadObject() { | |
| return Datastore.load(fooId); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class LazilyLoadedObject implements InvocationHandler { | |
| private Object target; | |
| @Override | |
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
| if (target == null) | |
| target = loadObject(); | |
| return method.invoke(target, args); | |
| } |