Why does JDK dynamic Proxy only work with Interfaces? - java

The JDK Proxy class only accepts interfaces in the factory method newProxyInstance().
Is there a workaround available, or alternative implementations?
The use cases are limited if I have to extract methods to an interface in order to enable them for use with a proxy. I would like to wrap them to apply annotation based actions during runtime.
public static <T> T getProxy(T obj) {
InvocationHandler ih = new InjectProxy( obj );
ClassLoader classLoader = InjectProxy.class.getClassLoader();
return (T) Proxy.newProxyInstance( classLoader, obj.getClass().getInterfaces(), ih );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

You can use cglib like this:
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class AbstractFactory {
#SuppressWarnings("unchecked")
public static <A> A createDefaultImplementation(Class<A> abstractClass) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(abstractClass);
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
if (!Modifier.isAbstract(method.getModifiers())) {
return methodProxy.invokeSuper(proxy, args);
} else {
Class type = method.getReturnType();
if (type.isPrimitive() && !void.class.equals(type)) {
return Array.get(Array.newInstance(type, 1), 0);
} else {
return null;
}
}
}
});
return (A) enhancer.create();
}
#SuppressWarnings("unchecked")
public static <A> A createDefaultImplementation(String className) throws ClassNotFoundException{
return (A) createDefaultImplementation(Class.forName(className));
}
}
This for example lets you build abstract classes with a default implementation method. But you can change the enhancer to what ever you want.

Is there a workaround available..?
Yeah. There is. Extract interface from existing classes.
upd
If you need it for some specific classes, you can write smt like
//interface that already exists
public interface IDomain {
String foo();
}
//your class
public class Domain implements IDomain{
public String foo(){
return "domain foo";
}
//method that doesn't placed in IDomain
public String bar(){
return "domain bar";
}
}
//So you need create new interface with bar()
//it can extend IDomain
public interface ExtendedIDomain extends IDomain {
public String bar();
}
//than your wrapper factory will be like this
public class Proxifier {
public static ExtendedIDomain getProxy(Domain obj) {
InvocationHandler ih = new InjectProxy( obj );
ClassLoader classLoader = InjectProxy.class.getClassLoader();
return (ExtendedIDomain) Proxy.newProxyInstance( classLoader, new Class[]{ExtendedIDomain.class}, ih );
}
static class InjectProxy implements InvocationHandler {
private final Domain domain;
private InjectProxy(Domain domain){
this.domain = domain;
}
public String invoke(Object proxy, Method method, Object[] args) throws Throwable{
for(Method m : domain.getClass().getMethods()){
//TODO: check signature(name, args etc) or implement some logic with annotations
if(m.getName().equals(method.getName())){
return "wrapped " + m.invoke(domain, args);
}
}
throw new IllegalArgumentException();
}
}
}
//test
public static void main(String[] args) {
ExtendedIDomain d = Proxifier.getProxy(new Domain());
System.out.println(d.foo());
System.out.println(d.bar());
}
If you need some "universal" stuff you should use AOP as #Peter Lawrey allready said.

or alternative implementations
You can use cglib.
Similar posts on alternatives to cglib : Are there alternatives to cglib?

Related

How to get 'proxied' object from an existing dynamic proxy

Is there an some API like Proxy.getProxiedObject(), that will return the original object of a dynamic proxy? I would like, for example, to invoke equals on the proxied objects, rather than on the dynamic proxies themselves, like in the following example:
public class ProxyTest implements InvocationHandler {
public static Object createProxy(Object target) {
Class<? extends Object> clazz = target.getClass();
return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ProxyTest());
}
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
// PROXIED OBJECTS COMPARISON - DESIRED
// return Proxy.getProxiedObject(proxy).equals(Proxy.getProxiedObject(args[0]));
// DYNAMIC PROXIES COMPARISON - UNDESIRED
// return proxy.equals(args[0]);
return null;
}
public static void main(String[] args) {
Object proxied = createProxy(new Object());
System.out.println(proxied.equals(proxied));
}
}
I don't think there's any API available for this; but I built a workaround this using the API that retrieves the InvocationHandler from any Proxy object, and the one that tests where or not a Class is a Proxy class:
InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException
boolean isProxyClass(Class<?> cl)
Using these I created an abstract extension of InvocationHandler to keep a reference to the object being proxied, with a static utility to retrieve the proxied object out of any potential Proxy object, and a factory utility for creating Proxys using the target object:
public abstract class ProxiedSavedInvocationHandler implements InvocationHandler {
public static Object getProxied(Object proxy) {
if (!Proxy.isProxyClass(proxy.getClass()))
return null;
InvocationHandler handler = Proxy.getInvocationHandler(proxy);
return (handler instanceof ProxiedSavedInvocationHandler) ?
((ProxiedSavedInvocationHandler)handler).proxied : null;
}
protected final Object proxied;
public ProxiedSavedInvocationHandler(Object proxied) {
this.proxied = proxied;
}
public Object getProxied() {
return proxied;
}
public Object createProxy() {
Class<? extends Object> clazz = proxied.getClass();
return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
}
}
Then I just used the newly created class like this:
class MyProxiedSavedInvocationHandler extends ProxiedSavedInvocationHandler {
...
}
ProxiedSavedInvocationHandler handler = new MyProxiedSavedInvocationHandler(target);
Object proxy = handler.createProxy();
// DESIRED API THROUGH STATIC UTILIY
Object proxied1 = ProxiedSavedInvocationHandler.getProxied(proxy);
// DESIRED API THROUGH INSTANCE UTILIY
Object proxied2 = handler.getProxied();
The only dependency on this solution is having the ProxiedSavedInvocationHandler utility class where all the logic and new APIs are located. This class can be extended even to include APIs to delegate behavior to other InvocationHandlers transparently; but the minimum required is there.
The following is a full working example application of this solution:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyTest {
static class MyProxiedSavedInvocationHandler extends ProxiedSavedInvocationHandler {
public MyProxiedSavedInvocationHandler(Object proxied) {
super(proxied);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
if (!method.getName().equals("equals"))
return method.invoke(proxied, args);
Object other = ProxiedSavedInvocationHandler.getProxied(args[0]);
System.out.println("====");
System.out.println("\tRunning 'equals' inside proxy with:");
System.out.println("\tthis: " + proxied);
System.out.println("\tother: " + other);
System.out.println("====");
return proxied.equals(other);
}
}
static abstract class ProxiedSavedInvocationHandler implements InvocationHandler {
public static Object getProxied(Object proxy) {
if (!Proxy.isProxyClass(proxy.getClass()))
return null;
InvocationHandler handler = Proxy.getInvocationHandler(proxy);
return (handler instanceof ProxiedSavedInvocationHandler) ?
((ProxiedSavedInvocationHandler)handler).proxied : null;
}
protected final Object proxied;
public ProxiedSavedInvocationHandler(Object proxied) {
this.proxied = proxied;
}
public Object getProxied() {
return proxied;
}
public Object createProxy() {
Class<? extends Object> clazz = proxied.getClass();
return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
}
}
// TO TEST EDGE SCENARIONS
private static Object createProxy(Class<? extends Object> clazz, InvocationHandler handler) {
return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), handler);
}
// MAIN
public static void main(String[] args) {
// EDGE SCENARIOS
Object proxiedFromNotEnhancedProxy =
ProxiedSavedInvocationHandler.getProxied(createProxy(Object.class, (p, m, a) -> null));
Object proxiedFromNotAProxy =
ProxiedSavedInvocationHandler.getProxied(new Object());
System.out.println("proxied from NOT ENHANCED PROXY: " + proxiedFromNotEnhancedProxy);
System.out.println("proxied from NOT A PROXY: " + proxiedFromNotAProxy);
System.out.println();
// FUNCTIONALITY DESIRED
Object target = new Object();
ProxiedSavedInvocationHandler handler = new MyProxiedSavedInvocationHandler(target);
Object proxy = handler.createProxy();
Object proxied1 = ProxiedSavedInvocationHandler.getProxied(proxy);
Object proxied2 = handler.getProxied();
System.out.println("target: " + target);
System.out.println("proxied1: " + proxied1);
System.out.println("target == proxied1: " + (target == proxied1));
System.out.println("proxy.equals(proxy): " + proxy.equals(proxy));
}
}
Complete code on GitHub

What is the idiomatic way to write common code for a group of classes with identical methods, but not implementing the same interface?

I'm using an external library that provides tightly related classes (generated from some template), but unfortunately without a shared interface, e.g.
public class A {
public UUID id();
public Long version();
public String foo();
public String bar();
}
public class B {
public UUID id();
public Long version();
public String foo();
public String bar();
}
public class C {
public UUID id();
public Long version();
public String foo();
public String bar();
}
// ... and more: D, E, F, etc.
Given I have no influence over the external library, what's the idiomatic way to write logic common to a group of classes that share the same method signatures (at least, for the methods being used by the common logic)?
Currently I do one of three things, on a case-by-case basis:
I write helper methods that take the primitive results from each object, e.g.
private static void myHelper(UUID id, Long version, String foo, String bar) {
...
}
This way I can "unpack" an object regardless of its type:
myHelper(whatever.id(), whatever.version(), whatever.foo(), whatever.bar());
But that can get very wordy, especially when I need to work with many members.
In the scenario where I'm only working with getters (i.e. only need to access current values of the objects), I've found a way to use mapping libraries like Dozer or ModelMapper to map A or B or C to my own common class, e.g.
public class CommonABC {
UUID id;
Long version;
String foo;
String bar;
}
By playing with configuration, you can get these libraries to map all members, whether method or field, public or private, to your class, e.g.
modelMapper.getConfiguration()
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);
But this was kind of a "broadsword" approach, a hack that IMO isn't clearly justified merely to factor out duplicate code.
Finally, in certain other scenarios it was most succinct to simply do
private static void myHelper(Object extLibEntity) {
if (extLibEntity instanceof A) {
...
} else if (extLibEntity instanceof B) {
...
} else if (extLibEntity instanceof C) {
...
} else {
throw new RuntimeException(...);
}
}
It's obvious why this is bad.
In enterprise situations where you have to live with a library that is this way, what would you do?
I'm leaning toward writing a very explicit, if verbose, mapper (not using a generic mapper library) that translates these entities from the start. But, I wonder if there's a better way. (Like, is there a way to "cast" an object as implementing a new interface, in runtime?)
An option that is (under the hood) likely similar to the second approach, but comparatively lean and flexible, is to use Dynamic Proxy Classes. With only a few lines of code, you can let any object "appear" to implement a certain interface, as long as it has the required methods. The following is an MCVE that shows the basic approach:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.UUID;
public class DelegatingProxyExample {
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
CommonInterface commonA = wrap(a);
CommonInterface commonB = wrap(b);
CommonInterface commonC = wrap(c);
use(commonA);
use(commonB);
use(commonC);
}
private static void use(CommonInterface commonInterface) {
System.out.println(commonInterface.id());
System.out.println(commonInterface.version());
System.out.println(commonInterface.foo());
System.out.println(commonInterface.bar());
}
private static CommonInterface wrap(Object object) {
CommonInterface commonInterface = (CommonInterface) Proxy.newProxyInstance(
CommonInterface.class.getClassLoader(),
new Class[] { CommonInterface.class }, new Delegator(object));
return commonInterface;
}
}
// Partially based on the example from
// https://docs.oracle.com/javase/8/docs/technotes/guides/reflection/proxy.html
class Delegator implements InvocationHandler {
private static Method hashCodeMethod;
private static Method equalsMethod;
private static Method toStringMethod;
static {
try {
hashCodeMethod = Object.class.getMethod("hashCode", (Class<?>[]) null);
equalsMethod = Object.class.getMethod("equals", new Class[] { Object.class });
toStringMethod = Object.class.getMethod("toString", (Class<?>[]) null);
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
private Object delegate;
public Delegator(Object delegate) {
this.delegate = delegate;
}
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
Class<?> declaringClass = m.getDeclaringClass();
if (declaringClass == Object.class) {
if (m.equals(hashCodeMethod)) {
return proxyHashCode(proxy);
} else if (m.equals(equalsMethod)) {
return proxyEquals(proxy, args[0]);
} else if (m.equals(toStringMethod)) {
return proxyToString(proxy);
} else {
throw new InternalError("unexpected Object method dispatched: " + m);
}
} else {
// TODO Here, the magic happens. Add some sensible error checks here!
Method delegateMethod = delegate.getClass().getDeclaredMethod(
m.getName(), m.getParameterTypes());
return delegateMethod.invoke(delegate, args);
}
}
protected Integer proxyHashCode(Object proxy) {
return new Integer(System.identityHashCode(proxy));
}
protected Boolean proxyEquals(Object proxy, Object other) {
return (proxy == other ? Boolean.TRUE : Boolean.FALSE);
}
protected String proxyToString(Object proxy) {
return proxy.getClass().getName() + '#' + Integer.toHexString(proxy.hashCode());
}
}
interface CommonInterface {
UUID id();
Long version();
String foo();
String bar();
}
class A {
public UUID id() {
return UUID.randomUUID();
}
public Long version() {
return 1L;
}
public String foo() {
return "fooA";
}
public String bar() {
return "barA";
}
}
class B {
public UUID id() {
return UUID.randomUUID();
}
public Long version() {
return 2L;
}
public String foo() {
return "fooB";
}
public String bar() {
return "barB";
}
}
class C {
public UUID id() {
return UUID.randomUUID();
}
public Long version() {
return 3L;
}
public String foo() {
return "fooC";
}
public String bar() {
return "barC";
}
}
Of course, this uses reflection internally, and should only be used when you know what you're doing. Particularly, you should add some sensible error checking, at the place that is marked with TODO: There, the method of the interface is looked up in the given delegate object.
The only technique not tried:
package aplus;
public interface Common {
...
}
public class A extends original.A implements Common {
}
public class B extends original.B implements Common {
}

Mockito to verify all getter like methods of an interface are called [duplicate]

This question already has answers here:
Verify that all getter methods are called
(2 answers)
Closed 6 years ago.
Lets say I have an interface that has many (like 30) getter like methods.
I would like to validate all the methods gets called with out specifying each and every method (that way if the interface gets update we have a failure).
For now I will just write a plain Java proxy handler but I was curious if there was a way to do this in Mockito.
I ended up just using a Proxy. I figured I would post it for others.
It is not elegant but it works.
One could add additional validate methods like sub or super set validation of methods instead of an exact match.
#Test
public void test() {
VerifyAllMethods h = new VerifyAllMethods() {
#Override
public Object runMethod(Method method, Object[] args) throws Throwable {
Class<?> rt = method.getReturnType();
if (Optional.class.equals(rt)) {
return Optional.absent();
}
else if (String.class.equals(rt)) {
return "mock";
}
else if (Integer.class.equals(rt)) {
return 1;
}
else if (List.class.equals(rt)) {
return Lists.newArrayList("1");
}
else {
return rt.newInstance();
}
}
};
SomeInterface mf = h.mock(SomeInterface.class);
// do something with SomeInterface
h.validateEquals(SomeInterface.class);
}
public abstract static class VerifyAllMethods implements InvocationHandler {
private final Set<Method> actualMethods = Sets.newLinkedHashSet();
public VerifyAllMethods() {
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
actualMethods.add(method);
return runMethod(method, args);
}
public abstract Object runMethod(Method method, Object[] args) throws Throwable;
public Set<Method> getActualMethods() {
return actualMethods;
}
public void validateEquals(Class<?> iface) {
Set<Method> methods = Sets.newHashSet(iface.getMethods());
/*
* Asserting Equals on strings is better for IDE as
* most generate a diff.
*/
assertEquals(methodsToString(methods), methodsToString(actualMethods));
}
public String methodsToString(Iterable<Method> methods) {
List<String> names = Lists.newArrayList();
for (Method m : methods) {
names.add(m.toString());
}
Collections.sort(names);
return Joiner.on("\n").join(names);
}
#SuppressWarnings("unchecked")
public <T> T mock(Class<T> iface) {
actualMethods.clear();
Class<?>[] interfaces = {iface};
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces , this);
}
}

How to modify/decorator an object returned from 3rd party API using javassist/CGLib

I have an 3rd party API call which returns the following object:
public class A {
protected void common() {
System.out.println("common is called in A");
}
public void test1() {
common();
System.out.println("test1 is called in A");
}
public void test2() {
common();
System.out.println("test2 is called in A");
}
}
But I'd like to modify its behavior like the following ModifiedA shows:
public class ModifiedA extends A {
#Override
protected void common() {
super.common();
System.out.println("common is called in ModifiedA");
}
}
So what I am trying to do is:
A a = 3rdPartyAPI_call();
//
// Now I'd like to get a ModifiedA which has changed common() behavior.
//
How to use javassist/CGLIB to accomplish this ?
One easy way may be to like this:
public class ModifiedA extends A {
private A a;
public ModifiedA(final A a) {
this.a = a;
}
//
// Override every public method in A
//
#Override
protected void common() {
super.common();
System.out.println("common is called in ModifiedA");
}
}
But since A's definition comes from 3rd party and is very complex and may change, so I'd like to use a proxy to do this?
Thanks for your comments in adavance.
You can use CGLib to implement a delegator pattern without having to override all the methods. There are a few different approaches to implement this depending on style but here is one similar to your example.
You can wrap the instance using a cglib Enhancer:
public static <T> T wrapInstance(final T original) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(original.getClass());
enhancer.setCallback(new MethodInterceptor() {
#Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Object returnValue = proxy.invoke(original, args);
if (method.getName().equals("common")) {
System.out.println("common is called");
}
return returnValue;
}
});
return (T) enhancer.create();
}
eclps post will fullfill your requirement and it works.I want to add some more code to eclps code.
Adding filter which give index zero for common method and rest all method to One. MethodInterceptor callback will intercept only common method and rest all method use NoOp intercetor(which will call super class apis).This way filtering is not happening for every method call.
public static <T> T wrapInstance(final T original) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(original.getClass());
enhancer.setCallbackFilter(new CallbackFilter() {
#Override
public int accept(Method method) {
if (method.getName().equals("common")) {
return 0;
}
return 1;
}
});
enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
#Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// only common method will intercept this call back.
return proxy.invoke(this, args);
}
}, NoOp.INSTANCE});
return (T) enhancer.create();
}

AspectJ constructor force factory pattern

I want to change the object return from call to a constuctor
FROM
public class A {
public A(){
}
public String sayHello() {
return "hello";
}
public String foo() {
return "foo";
}
}
TO
public class AWrapped extends A {
private A wrapped;
public AWrapped() {
super();
}
public AWrapped(A pWrapped) {
wrapped=pWrapped;
}
public String foo() {
return wrapped.foo();
}
public String sayHello {
return "gday mate";
}
}
What i want to do is to change the object that is returned from a call
A a = new A();
a.sayHello() returns "gday mate"
a is an instaceof AWrapped
I understand that this would usually be done with a factory pattern but I dont have access to the code of A or the code that makes new A's. And there are 1000s of places that A can be created.
It seems that Aspectj might do the trick, but i dont know much about it, If AspectJ would do the trick how to I get around the infinite wrapping i need to know that its being consturcted from within and aspect so it doesnt wrapp it again.
Thanks for the help
Jon
If I understand you right you could do the following:
I've created three packages:
aspectj for the aspect and AWrapped.java
unknown for A.java (could also be Bytecode but then you have to use Load Time Weaving)
main to test A a = new A();
MyAspect to return the AWrapped object if a new() call is made on class A:
package aspectj;
import unknown.A;
#Aspect
public class MyAspect {
#Pointcut("call(unknown.A.new(..)) && !within(aspectj..*)")
public static void init(ProceedingJoinPoint pjp) {
}
#Around("init(pjp)")
public Object initAdvice(ProceedingJoinPoint pjp) throws Throwable{
Object ret = pjp.proceed();
return new AWrapped((A) ret);
}
}
For testing:
package main;
import unknown.A;
public class Main {
public static void main(String[] args) {
A a = new A();
System.out.println(a.sayHello());
}
}
This outputs:
gday mate

Categories

Resources