javassist icon indicating copy to clipboard operation
javassist copied to clipboard

Support `setGenericSignature` on `ProxyFactory`

Open atdixon opened this issue 5 years ago • 4 comments

For certain use-cases I need proper generic signatures on the class generated by ProxyFactory.

I think this would be a simple matter of implementing ProxyFactory.setGenericSignature to retain the provided string for providing to ClassFile.setGenericSignature when the proxy class is created (ie, during make.)

atdixon avatar Jun 23 '19 15:06 atdixon

Sounds similar to my ticket: https://issues.jboss.org/browse/JASSIST-219

Workaround I did for this can be found here: (very specific to my usecase) https://github.com/Ericsson/proxy/blob/master/src/main/java/com/ericsson/commonlibrary/proxy/Invocation.java#L75

Basically I find the method from the original class and use that one instead.

Elisedlund avatar Jun 24 '19 19:06 Elisedlund

So, can we just call toGenericString() on the original class and attach it to the proxy class by ClassFile.setGenericSignature()? Maybe we must implement a function for obtaining the right signature by calling methods in java.lang.reflect.Executable...

chibash avatar Jun 25 '19 03:06 chibash

For my scenario I need to set the signature explicitly on the ProxyFactory to include specific type parameters.

For example, if there is an abstract class Node<T>, I want to create a proxy (concrete implementation of this class) with T set to Integer. In Java, I would implement MyNode extends Node<Integer>. When creating a proxy instance, I want to specify the proxy class's generic signature.

The end result would mean that I could reflect on the signature at runtime:

ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(Node.class);
factory.setGenericSignature("LNode<Ljava/lang/Integer;>;"); // *** this is my need ***
...
Node proxyInstance = (Node) factory.create(new Class<?>[0], new Object[0], handler);
Type[] x = ((ParameterizedType) proxyInstance.getClass().getGenericSuperclass())
	.getActualTypeArguments();
// x[0] is Integer

atdixon avatar Jun 27 '19 03:06 atdixon

I've fixed this. Please check the latest commit.

chibash avatar Jul 27 '19 08:07 chibash