]> source.dussan.org Git - javassist.git/commitdiff
fixes GitHub Issue 462 (Internal class issues in the Java 21) 463/head
authorwuwen <wuwen.55@aliyun.com>
Sun, 8 Oct 2023 08:31:26 +0000 (16:31 +0800)
committerwuwen <wuwen.55@aliyun.com>
Fri, 3 Nov 2023 07:04:16 +0000 (15:04 +0800)
src/main/javassist/bytecode/MethodParametersAttribute.java
src/test/javassist/JvstTest5.java
src/test/resources/Java21InnerClassWithoutParameters$InnerClass.class [new file with mode: 0644]
src/test/resources/Java21InnerClassWithoutParameters.class [new file with mode: 0644]

index b9c252a9f470cc6ee483d81a30c6b1933ae38bd8..f78f6c723780cbae9c3cd72d0266e8ddc5c21caa 100644 (file)
@@ -32,7 +32,8 @@ public class MethodParametersAttribute extends AttributeInfo {
         byte[] data = new byte[names.length * 4 + 1];
         data[0] = (byte)names.length;
         for (int i = 0; i < names.length; i++) {
-            ByteArray.write16bit(cp.addUtf8Info(names[i]), data, i * 4 + 1);
+            String name = names[i];
+            ByteArray.write16bit(name == null ? 0 : cp.addUtf8Info(name), data, i * 4 + 1);
             ByteArray.write16bit(flags[i], data, i * 4 + 3);
         }
 
@@ -61,7 +62,8 @@ public class MethodParametersAttribute extends AttributeInfo {
      * @param i         the position of the parameter.
      */
     public String parameterName(int i) {
-        return getConstPool().getUtf8Info(name(i));
+        int index = name(i);
+        return index == 0 ? null : getConstPool().getUtf8Info(index);
     }
 
     /**
@@ -87,7 +89,8 @@ public class MethodParametersAttribute extends AttributeInfo {
         String[] names = new String[s];
         int[] flags = new int[s];
         for (int i = 0; i < s; i++) {
-            names[i] = cp.getUtf8Info(name(i));
+            int index = name(i);
+            names[i] = index == 0 ? null : cp.getUtf8Info(index);
             flags[i] = accessFlags(i);
         }
 
index 4a370632664521adca4fa864bb4448704ae65291..f1c5683711c1870df46b4caa2ad057e527d7d43d 100644 (file)
@@ -1,5 +1,8 @@
 package javassist;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.TypeVariable;
 
@@ -9,6 +12,8 @@ import javassist.bytecode.AttributeInfo;
 import javassist.bytecode.ClassFile;
 import javassist.bytecode.ConstPool;
 import javassist.bytecode.InnerClassesAttribute;
+import javassist.bytecode.MethodInfo;
+import javassist.bytecode.MethodParametersAttribute;
 import javassist.bytecode.NestHostAttribute;
 import javassist.bytecode.NestMembersAttribute;
 import javassist.expr.ExprEditor;
@@ -593,4 +598,31 @@ public class JvstTest5 extends JvstTestRoot {
         }
         catch (CannotCompileException e) {}
     }
+
+    public void testGithubIssue462Java21WithoutParameters() throws IOException {
+
+        //This is a class file compiled by Java-21
+        //javac Java21InnerClassWithoutParameters.java
+        //public class Java21InnerClassWithoutParameters {
+        //    class InnerClass implements Runnable {
+        //        public void run() {
+        //        }
+        //    }
+        //}
+        String classFileName = "./Java21InnerClassWithoutParameters$InnerClass.class";
+        ClassLoader classLoader = getClass().getClassLoader();
+        File classFile = new File(classLoader.getResource(classFileName).getFile());
+        
+        CtClass cc = sloader.makeClass(new FileInputStream(classFile));
+        cc.getClassFile().compact();
+
+        ClassFile cf = cc.getClassFile2();
+        ConstPool cp = cf.getConstPool();
+        
+        MethodInfo minfo = cf.getMethod("<init>");
+        MethodParametersAttribute attr
+                = (MethodParametersAttribute)minfo.getAttribute(MethodParametersAttribute.tag);
+        assertEquals(1, attr.size());
+        assertNull(attr.parameterName(0));
+    }
 }
diff --git a/src/test/resources/Java21InnerClassWithoutParameters$InnerClass.class b/src/test/resources/Java21InnerClassWithoutParameters$InnerClass.class
new file mode 100644 (file)
index 0000000..c1c6cab
Binary files /dev/null and b/src/test/resources/Java21InnerClassWithoutParameters$InnerClass.class differ
diff --git a/src/test/resources/Java21InnerClassWithoutParameters.class b/src/test/resources/Java21InnerClassWithoutParameters.class
new file mode 100644 (file)
index 0000000..a6208c5
Binary files /dev/null and b/src/test/resources/Java21InnerClassWithoutParameters.class differ