Skip to content

Commit 39109e5

Browse files
committed
8209736: runtime/RedefineTests/ModifyAnonymous.java fails with NullPointerException when running in CDS mode
Add logging of class names in the allLoadedClasses array; throw RuntimeException upon encountering of a null class. Backport-of: 3c810ce
1 parent cc6137c commit 39109e5

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

test/hotspot/jtreg/runtime/RedefineTests/ModifyAnonymous.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,9 @@
3232
* @run main/othervm -javaagent:redefineagent.jar ModifyAnonymous
3333
*/
3434

35+
import java.io.File;
3536
import java.io.FileNotFoundException;
37+
import java.io.FileOutputStream;
3638
import java.io.PrintWriter;
3739
import java.lang.RuntimeException;
3840
import java.lang.instrument.ClassDefinition;
@@ -107,12 +109,39 @@ public void doWork() {
107109
}
108110

109111
static void runTest() {
112+
PrintWriter pw;
113+
String logName = System.getProperty("test.classes") +
114+
File.separator + "loadedClasses.log";
115+
// Create a log file to capture the names of the classes in the
116+
// allLoadedClasses array. The log file is for assisting in debugging
117+
// in case a null class is encountered in the allLoadedClasses array.
118+
try {
119+
pw = new PrintWriter(new FileOutputStream(
120+
new File(logName), true));
121+
} catch (FileNotFoundException e) {
122+
throw new RuntimeException("Could not write loaded classes to log", e);
123+
}
110124
while (!done) {
111125
Class[] allLoadedClasses = inst.getAllLoadedClasses();
112-
for (Class clazz : allLoadedClasses) {
126+
int len = allLoadedClasses.length;
127+
pw.println(" allLoadedClasses length: " + len);
128+
for (int idx = 0; idx < len; idx++) {
129+
Class cls = allLoadedClasses[idx];
130+
pw.println(" " + idx + " " +
131+
((cls != null) ? cls.getName() : "null"));
132+
}
133+
for (int idx = 0; idx < len; idx++) {
134+
Class clazz = allLoadedClasses[idx];
135+
if (clazz == null) {
136+
pw.flush();
137+
pw.close();
138+
throw new RuntimeException("null class encountered");
139+
}
113140
final String name = clazz.getName();
114141
if (name.contains("$$Lambda$") && name.contains("App")) {
115142
if (inst.isModifiableClass(clazz)) {
143+
pw.flush();
144+
pw.close();
116145
throw new RuntimeException ("Class should not be modifiable");
117146
}
118147
// Try to modify them anyway.
@@ -133,12 +162,16 @@ static void runTest() {
133162
System.out.println("PASSED: expecting UnmodifiableClassException");
134163
t.printStackTrace();
135164
} catch(java.lang.ClassNotFoundException e) {
165+
pw.flush();
166+
pw.close();
136167
throw new RuntimeException ("ClassNotFoundException thrown");
137168
}
138169
done = true;
139170
}
140171
}
141172
}
173+
pw.flush();
174+
pw.close();
142175
}
143176

144177
public static void main(String argv[]) throws InterruptedException, RuntimeException {

0 commit comments

Comments
 (0)