]> source.dussan.org Git - javassist.git/commitdiff
fixed Issue #263
authorchibash <chiba@javassist.org>
Sat, 27 Jul 2019 05:06:22 +0000 (14:06 +0900)
committerchibash <chiba@javassist.org>
Sat, 27 Jul 2019 05:06:22 +0000 (14:06 +0900)
javassist.jar
src/main/javassist/util/proxy/ProxyFactory.java
src/test/Test.java
src/test/javassist/proxyfactory/GenSig.java [new file with mode: 0644]
src/test/javassist/proxyfactory/ProxyFactoryTest.java

index 5508e2edfcf820fa197415ea36ca7281575fbae3..74e714ee490cedac0657f370b4ca40ed4d747433 100644 (file)
Binary files a/javassist.jar and b/javassist.jar differ
index 98e352ca7c63d747fb5bfeebcf275e7bfa1f7400..7d88e07a5365ed0d544e71968f697459ca0b83c7 100644 (file)
@@ -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;
@@ -435,6 +439,34 @@ public class ProxyFactory {
         signature = null;
     }
 
+    /**
+     * Sets the generic signature for the proxy class.
+     * <p>For example,
+     *
+     * <pre>class NullPtr&lt;T&gt; {
+     *     T get() { return null; }
+     * }
+     * </pre>
+     *
+     * <p>The following code makes a proxy for <code>NullPtr&lt;String&gt;</code>:
+     *
+     * <pre>ProxyFactory factory = new ProxyFactory();
+     * factory.setSuperclass(NullPtr.class);
+     * factory.setGenericSignature("LNullPtr&lt;Ljava/lang/String;&gt;;");
+     * NullPtr&lt;?&gt; np = (NullPtr&lt;?&gt;)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
@@ -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);
index 9907406da0847f351a3d5bcef3da215b88bceb8a..45f74c265965123fa4b0b8321b7ac1dbfd1efbfd 100644 (file)
@@ -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 (file)
index 0000000..018cdb3
--- /dev/null
@@ -0,0 +1,4 @@
+package javassist.proxyfactory;
+
+class GenSig<T> {
+}
index c69acc9dc75f31b8c9ff3aa05fb6311ab5118254..0997620f10d44c0c80cd6988f3d0e2abc0f17916 100644 (file)
@@ -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]);
+    }
 }