aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShigeru Chiba <chibash@users.noreply.github.com>2023-12-04 18:49:40 +0900
committerGitHub <noreply@github.com>2023-12-04 18:49:40 +0900
commit158294371e39b24f003f15933bd74f2b26bbf3aa (patch)
treecab713641bdaebdf50b2e74428d3ca0a42b3aa2a
parent700be6f6f9546e8af049b1a763ce27f1fde5955d (diff)
parent784a7c1ef9996d166aedcd4aacd1fa12016dde7f (diff)
downloadjavassist-158294371e39b24f003f15933bd74f2b26bbf3aa.tar.gz
javassist-158294371e39b24f003f15933bd74f2b26bbf3aa.zip
Merge pull request #463 from wuwen5/java21-inner-class
fixes Issue 462 (In Java21, the ConstPool API throws NPE for MethodParameters attributes without parameter names)
-rw-r--r--src/main/javassist/bytecode/MethodParametersAttribute.java9
-rw-r--r--src/test/javassist/JvstTest5.java32
-rw-r--r--src/test/resources/Java21InnerClassWithoutParameters$InnerClass.classbin0 -> 495 bytes
-rw-r--r--src/test/resources/Java21InnerClassWithoutParameters.classbin0 -> 358 bytes
4 files changed, 38 insertions, 3 deletions
diff --git a/src/main/javassist/bytecode/MethodParametersAttribute.java b/src/main/javassist/bytecode/MethodParametersAttribute.java
index b9c252a9..f78f6c72 100644
--- a/src/main/javassist/bytecode/MethodParametersAttribute.java
+++ b/src/main/javassist/bytecode/MethodParametersAttribute.java
@@ -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);
}
diff --git a/src/test/javassist/JvstTest5.java b/src/test/javassist/JvstTest5.java
index 4a370632..f1c56837 100644
--- a/src/test/javassist/JvstTest5.java
+++ b/src/test/javassist/JvstTest5.java
@@ -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
index 00000000..c1c6cab5
--- /dev/null
+++ b/src/test/resources/Java21InnerClassWithoutParameters$InnerClass.class
Binary files differ
diff --git a/src/test/resources/Java21InnerClassWithoutParameters.class b/src/test/resources/Java21InnerClassWithoutParameters.class
new file mode 100644
index 00000000..a6208c52
--- /dev/null
+++ b/src/test/resources/Java21InnerClassWithoutParameters.class
Binary files differ