Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes Issue 462 (In Java21, the ConstPool API throws NPE for MethodParameters attributes without parameter names) #463

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/main/javassist/bytecode/MethodParametersAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public MethodParametersAttribute(ConstPool cp, String[] names, int[] flags) {
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);
}

Expand Down Expand Up @@ -61,7 +62,8 @@ public int name(int i) {
* @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);
}

/**
Expand All @@ -87,7 +89,8 @@ public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
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);
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/javassist/JvstTest5.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,6 +12,8 @@
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;
Expand Down Expand Up @@ -593,4 +598,31 @@ public void testTooManyConstPoolItems() throws Exception {
}
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));
}
}
Binary file not shown.
Binary file not shown.