|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2012, 2023, 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
|
|
34 | 34 | import sun.invoke.util.VerifyAccess;
|
35 | 35 | import sun.invoke.util.VerifyType;
|
36 | 36 | import sun.invoke.util.Wrapper;
|
37 |
| -import sun.reflect.misc.ReflectUtil; |
38 | 37 |
|
39 |
| -import java.io.File; |
40 |
| -import java.io.FileOutputStream; |
41 |
| -import java.io.IOException; |
42 | 38 | import java.lang.reflect.Modifier;
|
43 | 39 | import java.util.ArrayList;
|
44 | 40 | import java.util.Arrays;
|
@@ -125,7 +121,7 @@ private InvokerBytecodeGenerator(LambdaForm lambdaForm, int localsMapSize,
|
125 | 121 | name = invokerName.substring(0, p);
|
126 | 122 | invokerName = invokerName.substring(p + 1);
|
127 | 123 | }
|
128 |
| - if (DUMP_CLASS_FILES) { |
| 124 | + if (dumper().isEnabled()) { |
129 | 125 | name = makeDumpableClassName(name);
|
130 | 126 | }
|
131 | 127 | this.name = name;
|
@@ -173,58 +169,8 @@ private InvokerBytecodeGenerator(String name, LambdaForm form, MethodType invoke
|
173 | 169 | }
|
174 | 170 |
|
175 | 171 | /** instance counters for dumped classes */
|
176 |
| - private static final HashMap<String,Integer> DUMP_CLASS_FILES_COUNTERS; |
177 |
| - /** debugging flag for saving generated class files */ |
178 |
| - private static final File DUMP_CLASS_FILES_DIR; |
179 |
| - |
180 |
| - static { |
181 |
| - if (DUMP_CLASS_FILES) { |
182 |
| - DUMP_CLASS_FILES_COUNTERS = new HashMap<>(); |
183 |
| - try { |
184 |
| - File dumpDir = new File("DUMP_CLASS_FILES"); |
185 |
| - if (!dumpDir.exists()) { |
186 |
| - dumpDir.mkdirs(); |
187 |
| - } |
188 |
| - DUMP_CLASS_FILES_DIR = dumpDir; |
189 |
| - System.out.println("Dumping class files to "+DUMP_CLASS_FILES_DIR+"/..."); |
190 |
| - } catch (Exception e) { |
191 |
| - throw newInternalError(e); |
192 |
| - } |
193 |
| - } else { |
194 |
| - DUMP_CLASS_FILES_COUNTERS = null; |
195 |
| - DUMP_CLASS_FILES_DIR = null; |
196 |
| - } |
197 |
| - } |
198 |
| - |
199 |
| - private void maybeDump(final byte[] classFile) { |
200 |
| - if (DUMP_CLASS_FILES) { |
201 |
| - maybeDump(className, classFile); |
202 |
| - } |
203 |
| - } |
204 |
| - |
205 |
| - // Also used from BoundMethodHandle |
206 |
| - @SuppressWarnings("removal") |
207 |
| - static void maybeDump(final String className, final byte[] classFile) { |
208 |
| - if (DUMP_CLASS_FILES) { |
209 |
| - java.security.AccessController.doPrivileged( |
210 |
| - new java.security.PrivilegedAction<>() { |
211 |
| - public Void run() { |
212 |
| - try { |
213 |
| - String dumpName = className.replace('.','/'); |
214 |
| - File dumpFile = new File(DUMP_CLASS_FILES_DIR, dumpName+".class"); |
215 |
| - System.out.println("dump: " + dumpFile); |
216 |
| - dumpFile.getParentFile().mkdirs(); |
217 |
| - FileOutputStream file = new FileOutputStream(dumpFile); |
218 |
| - file.write(classFile); |
219 |
| - file.close(); |
220 |
| - return null; |
221 |
| - } catch (IOException ex) { |
222 |
| - throw newInternalError(ex); |
223 |
| - } |
224 |
| - } |
225 |
| - }); |
226 |
| - } |
227 |
| - } |
| 172 | + private static final HashMap<String,Integer> DUMP_CLASS_FILES_COUNTERS = |
| 173 | + dumper().isEnabled() ? new HashMap<>(): null; |
228 | 174 |
|
229 | 175 | private static String makeDumpableClassName(String className) {
|
230 | 176 | Integer ctr;
|
@@ -271,7 +217,7 @@ String classData(Object arg) {
|
271 | 217 |
|
272 | 218 | // unique static variable name
|
273 | 219 | String name;
|
274 |
| - if (DUMP_CLASS_FILES) { |
| 220 | + if (dumper().isEnabled()) { |
275 | 221 | Class<?> c = arg.getClass();
|
276 | 222 | while (c.isArray()) {
|
277 | 223 | c = c.getComponentType();
|
@@ -299,7 +245,7 @@ private static String debugString(Object arg) {
|
299 | 245 | * Extract the MemberName of a newly-defined method.
|
300 | 246 | */
|
301 | 247 | private MemberName loadMethod(byte[] classFile) {
|
302 |
| - Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(className, classFile, Set.of()) |
| 248 | + Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(className, classFile, Set.of(), dumper()) |
303 | 249 | .defineClass(true, classDataValues());
|
304 | 250 | return resolveInvokerMember(invokerClass, invokerName, invokerType);
|
305 | 251 | }
|
@@ -809,9 +755,7 @@ private byte[] generateCustomizedCodeBytes() {
|
809 | 755 | clinit(cw, className, classData);
|
810 | 756 | bogusMethod(lambdaForm);
|
811 | 757 |
|
812 |
| - final byte[] classFile = toByteArray(); |
813 |
| - maybeDump(classFile); |
814 |
| - return classFile; |
| 758 | + return toByteArray(); |
815 | 759 | }
|
816 | 760 |
|
817 | 761 | void setClassWriter(ClassWriter cw) {
|
@@ -1898,9 +1842,7 @@ private byte[] generateLambdaFormInterpreterEntryPointBytes() {
|
1898 | 1842 | clinit(cw, className, classData);
|
1899 | 1843 | bogusMethod(invokerType);
|
1900 | 1844 |
|
1901 |
| - final byte[] classFile = cw.toByteArray(); |
1902 |
| - maybeDump(classFile); |
1903 |
| - return classFile; |
| 1845 | + return cw.toByteArray(); |
1904 | 1846 | }
|
1905 | 1847 |
|
1906 | 1848 | /**
|
@@ -1967,17 +1909,15 @@ private byte[] generateNamedFunctionInvokerImpl(MethodTypeForm typeForm) {
|
1967 | 1909 | clinit(cw, className, classData);
|
1968 | 1910 | bogusMethod(dstType);
|
1969 | 1911 |
|
1970 |
| - final byte[] classFile = cw.toByteArray(); |
1971 |
| - maybeDump(classFile); |
1972 |
| - return classFile; |
| 1912 | + return cw.toByteArray(); |
1973 | 1913 | }
|
1974 | 1914 |
|
1975 | 1915 | /**
|
1976 | 1916 | * Emit a bogus method that just loads some string constants. This is to get the constants into the constant pool
|
1977 | 1917 | * for debugging purposes.
|
1978 | 1918 | */
|
1979 | 1919 | private void bogusMethod(Object os) {
|
1980 |
| - if (DUMP_CLASS_FILES) { |
| 1920 | + if (dumper().isEnabled()) { |
1981 | 1921 | mv = cw.visitMethod(Opcodes.ACC_STATIC, "dummy", "()V", null, null);
|
1982 | 1922 | mv.visitLdcInsn(os.toString());
|
1983 | 1923 | mv.visitInsn(Opcodes.POP);
|
|
0 commit comments