javassist
javassist copied to clipboard
Support `setGenericSignature` on `ProxyFactory`
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
.)
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.
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...
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
I've fixed this. Please check the latest commit.