Skip to content

Commit 1825f1c

Browse files
jbachorikclaude
andcommitted
fix: avoid NPE from Auxiliary.class.getPackage() on JDK 8
On JDK 8, Class.getPackage() returns null for classes dynamically defined on the bootstrap classloader because the VM package table is only populated for file-loaded classes. Derive the package path from Class.getName() instead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 672ddc2 commit 1825f1c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

btrace-instr/src/test/java/org/openjdk/btrace/instr/InstrumentorTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void visit(
202202
String[] interfaces) {
203203
int idx = name.lastIndexOf('/');
204204
name =
205-
Auxiliary.class.getPackage().getName().replace('.', '/')
205+
Auxiliary.class.getName().substring(0, Auxiliary.class.getName().lastIndexOf('.')).replace('.', '/')
206206
+ '/'
207207
+ name.substring(idx + 1);
208208
super.visit(version, access, name, signature, superName, interfaces);

btrace-runtime/src/main/java/org/openjdk/btrace/runtime/BTraceRuntimeAccessImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
4343
public final class BTraceRuntimeAccessImpl implements BTraceRuntimeAccess.Delegate {
4444
private static final BTraceRuntimeAccessImpl INSTANCE = new BTraceRuntimeAccessImpl();
4545

46+
// Auxiliary.class.getPackage() returns null on JDK 8 when the class is dynamically defined
47+
// on the bootstrap classloader (the VM package table is only populated for file-loaded classes)
48+
private static final String AUXILIARY_PKG;
49+
50+
static {
51+
String name = Auxiliary.class.getName();
52+
AUXILIARY_PKG = name.substring(0, name.lastIndexOf('.')).replace('.', '/');
53+
}
54+
4655
static final class RTWrapper {
4756
private BTraceRuntime.Impl rt = null;
4857

@@ -140,11 +149,11 @@ static String getClientNameInternal(String forClassName) {
140149
int idx = forClassName.lastIndexOf('/');
141150
if (idx > -1) {
142151
forClassName =
143-
Auxiliary.class.getPackage().getName().replace('.', '/')
152+
AUXILIARY_PKG
144153
+ "/"
145154
+ forClassName.substring(idx + 1);
146155
} else {
147-
forClassName = Auxiliary.class.getPackage().getName().replace('.', '/') + "/" + forClassName;
156+
forClassName = AUXILIARY_PKG + "/" + forClassName;
148157
}
149158

150159
if (!BTraceRuntimeAccess.isUniqueClientClassNames()) {

0 commit comments

Comments
 (0)