1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
32
32
* @run main/othervm -javaagent:redefineagent.jar ModifyAnonymous
33
33
*/
34
34
35
+ import java .io .File ;
35
36
import java .io .FileNotFoundException ;
37
+ import java .io .FileOutputStream ;
36
38
import java .io .PrintWriter ;
37
39
import java .lang .RuntimeException ;
38
40
import java .lang .instrument .ClassDefinition ;
@@ -107,12 +109,39 @@ public void doWork() {
107
109
}
108
110
109
111
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
+ }
110
124
while (!done ) {
111
125
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
+ }
113
140
final String name = clazz .getName ();
114
141
if (name .contains ("$$Lambda$" ) && name .contains ("App" )) {
115
142
if (inst .isModifiableClass (clazz )) {
143
+ pw .flush ();
144
+ pw .close ();
116
145
throw new RuntimeException ("Class should not be modifiable" );
117
146
}
118
147
// Try to modify them anyway.
@@ -133,12 +162,16 @@ static void runTest() {
133
162
System .out .println ("PASSED: expecting UnmodifiableClassException" );
134
163
t .printStackTrace ();
135
164
} catch (java .lang .ClassNotFoundException e ) {
165
+ pw .flush ();
166
+ pw .close ();
136
167
throw new RuntimeException ("ClassNotFoundException thrown" );
137
168
}
138
169
done = true ;
139
170
}
140
171
}
141
172
}
173
+ pw .flush ();
174
+ pw .close ();
142
175
}
143
176
144
177
public static void main (String argv []) throws InterruptedException , RuntimeException {
0 commit comments