Skip to content

Instantly share code, notes, and snippets.

View tylertreat's full-sized avatar

Tyler Treat tylertreat

View GitHub Profile
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;
}
@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())
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
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
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");
public Object invoke(Object proxy, Method method, Object[] args)
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
FooEntity foo = (FooEntity) Enhancer.create(
FooEntity.class,
new LazyLoader() {
public Object loadObject() throws Exception {
return Datastore.load(fooId);
}
});
FooEntity foo = (FooEntity) Enhancer.create(
FooEntity.class,
new LazilyLoadedObject() {
protected Object loadObject() {
return Datastore.load(fooId);
}
});
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);
}