diff options
author | chibash <chiba@javassist.org> | 2019-07-27 14:06:22 +0900 |
---|---|---|
committer | chibash <chiba@javassist.org> | 2019-07-27 14:06:22 +0900 |
commit | 8f4788e0907278cec2a094af179451824651a515 (patch) | |
tree | bff600908fc0597d8ada77ed8618d8650f082e1c /src | |
parent | 9076bde17537c059aa679bd6c0324bda78bdb088 (diff) | |
download | javassist-8f4788e0907278cec2a094af179451824651a515.tar.gz javassist-8f4788e0907278cec2a094af179451824651a515.zip |
fixed Issue #263
Diffstat (limited to 'src')
-rw-r--r-- | src/main/javassist/util/proxy/ProxyFactory.java | 37 | ||||
-rw-r--r-- | src/test/Test.java | 8 | ||||
-rw-r--r-- | src/test/javassist/proxyfactory/GenSig.java | 4 | ||||
-rw-r--r-- | src/test/javassist/proxyfactory/ProxyFactoryTest.java | 11 |
4 files changed, 57 insertions, 3 deletions
diff --git a/src/main/javassist/util/proxy/ProxyFactory.java b/src/main/javassist/util/proxy/ProxyFactory.java index 98e352ca..7d88e07a 100644 --- a/src/main/javassist/util/proxy/ProxyFactory.java +++ b/src/main/javassist/util/proxy/ProxyFactory.java @@ -49,6 +49,7 @@ import javassist.bytecode.ExceptionsAttribute; import javassist.bytecode.FieldInfo; import javassist.bytecode.MethodInfo; import javassist.bytecode.Opcode; +import javassist.bytecode.SignatureAttribute; import javassist.bytecode.StackMapTable; /* @@ -186,6 +187,8 @@ public class ProxyFactory { private String basename; private String superName; private Class<?> thisClass; + private String genericSignature; + /** * per factory setting initialised from current setting for useCache but able to be reset before each create call */ @@ -389,6 +392,7 @@ public class ProxyFactory { signatureMethods = null; hasGetHandler = false; thisClass = null; + genericSignature = null; writeDirectory = null; factoryUseCache = useCache; factoryWriteReplace = useWriteReplace; @@ -436,6 +440,34 @@ public class ProxyFactory { } /** + * Sets the generic signature for the proxy class. + * <p>For example, + * + * <pre>class NullPtr<T> { + * T get() { return null; } + * } + * </pre> + * + * <p>The following code makes a proxy for <code>NullPtr<String></code>: + * + * <pre>ProxyFactory factory = new ProxyFactory(); + * factory.setSuperclass(NullPtr.class); + * factory.setGenericSignature("LNullPtr<Ljava/lang/String;>;"); + * NullPtr<?> np = (NullPtr<?>)factory.create(null, null); + *java.lang.reflect.Type[] x = ((java.lang.reflect.ParameterizedType)np.getClass().getGenericSuperclass()) + * .getActualTypeArguments(); + * // x[0] == String.class + * </pre> + * + * @param sig a generic signature. + * @see javassist.CtClass#setGenericSignature(String) + * @since 3.26 + */ + public void setGenericSignature(String sig) { + genericSignature = sig; + } + + /** * Generates a proxy class using the current filter. * The module or package where a proxy class is created * has to be opened to this package or the Javassist module. @@ -885,6 +917,11 @@ public class ProxyFactory { finfo4.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC| AccessFlag.FINAL); cf.addField(finfo4); + if (genericSignature != null) { + SignatureAttribute sa = new SignatureAttribute(pool, genericSignature); + cf.addAttribute(sa); + } + // HashMap allMethods = getMethods(superClass, interfaces); // int size = allMethods.size(); makeConstructors(classname, cf, pool, classname); diff --git a/src/test/Test.java b/src/test/Test.java index 9907406d..45f74c26 100644 --- a/src/test/Test.java +++ b/src/test/Test.java @@ -44,9 +44,11 @@ public class Test { final CtClass ctClass = classPool.get(INVALID_STACK_MAP_FRAME); final CtMethod method = ctClass.getDeclaredMethod("bytecodeVerifyError"); - method.addLocalVariable("test_localVariable", CtClass.intType); - method.insertBefore("{ test_localVariable = 1; }"); - ctClass.debugWriteFile(); + // method.addLocalVariable("test_localVariable", CtClass.intType); + method.insertBefore("{ java.math.BigDecimal d = null; d.divide(d); }"); + //ctClass.debugWriteFile(); + System.out.println("ok"); + ctClass.writeFile(); Class<?> cc = ctClass.toClass(); System.out.println(cc.getName()); InvalidStackMapFrame obj = (InvalidStackMapFrame)cc.getDeclaredConstructor().newInstance(); diff --git a/src/test/javassist/proxyfactory/GenSig.java b/src/test/javassist/proxyfactory/GenSig.java new file mode 100644 index 00000000..018cdb39 --- /dev/null +++ b/src/test/javassist/proxyfactory/GenSig.java @@ -0,0 +1,4 @@ +package javassist.proxyfactory; + +class GenSig<T> { +} diff --git a/src/test/javassist/proxyfactory/ProxyFactoryTest.java b/src/test/javassist/proxyfactory/ProxyFactoryTest.java index c69acc9d..0997620f 100644 --- a/src/test/javassist/proxyfactory/ProxyFactoryTest.java +++ b/src/test/javassist/proxyfactory/ProxyFactoryTest.java @@ -152,4 +152,15 @@ public class ProxyFactoryTest extends TestCase { } }); } + + // Issue #263 + public void testGenericSignature() throws Exception { + ProxyFactory factory = new ProxyFactory(); + factory.setSuperclass(GenSig.class); + factory.setGenericSignature("Ljavassist/proxyfactory/GenSig<Ljava/lang/Integer;>;"); + GenSig gs = (GenSig)factory.create(null, null); + java.lang.reflect.Type[] x = ((java.lang.reflect.ParameterizedType)gs.getClass().getGenericSuperclass()) + .getActualTypeArguments(); + assertEquals(Integer.class, x[0]); + } } |