6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.misc;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.DataOutputStream;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.lang.reflect.Array;
34 import java.lang.reflect.Method;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.ListIterator;
43 import java.util.Map;
44 import sun.security.action.GetBooleanAction;
45
46 /**
47 * ProxyGenerator contains the code to generate a dynamic proxy class
48 * for the java.lang.reflect.Proxy API.
49 *
50 * The external interfaces to ProxyGenerator is the static
51 * "generateProxyClass" method.
52 *
53 * @author Peter Jones
54 * @since 1.3
55 */
56 public class ProxyGenerator {
57 /*
58 * In the comments below, "JVMS" refers to The Java Virtual Machine
59 * Specification Second Edition and "JLS" refers to the original
60 * version of The Java Language Specification, unless otherwise
61 * specified.
62 */
63
64 /* generate 1.5-era class file version */
65 private static final int CLASSFILE_MAJOR_VERSION = 49;
66 private static final int CLASSFILE_MINOR_VERSION = 0;
67
68 /*
69 * beginning of constants copied from
70 * sun.tools.java.RuntimeConstants (which no longer exists):
71 */
72
73 /* constant pool tags */
74 private static final int CONSTANT_UTF8 = 1;
75 private static final int CONSTANT_UNICODE = 2;
76 private static final int CONSTANT_INTEGER = 3;
297 // private static final int opc_monitorexit = 195;
298 private static final int opc_wide = 196;
299 // private static final int opc_multianewarray = 197;
300 // private static final int opc_ifnull = 198;
301 // private static final int opc_ifnonnull = 199;
302 // private static final int opc_goto_w = 200;
303 // private static final int opc_jsr_w = 201;
304
305 // end of constants copied from sun.tools.java.RuntimeConstants
306
307 /** name of the superclass of proxy classes */
308 private static final String superclassName = "java/lang/reflect/Proxy";
309
310 /** name of field for storing a proxy instance's invocation handler */
311 private static final String handlerFieldName = "h";
312
313 /** debugging flag for saving generated class files */
314 private static final boolean saveGeneratedFiles =
315 java.security.AccessController.doPrivileged(
316 new GetBooleanAction(
317 "sun.misc.ProxyGenerator.saveGeneratedFiles")).booleanValue();
318
319 /**
320 * Generate a public proxy class given a name and a list of proxy interfaces.
321 */
322 public static byte[] generateProxyClass(final String name,
323 Class<?>[] interfaces) {
324 return generateProxyClass(name, interfaces, (ACC_PUBLIC | ACC_FINAL | ACC_SUPER));
325 }
326
327 /**
328 * Generate a proxy class given a name and a list of proxy interfaces.
329 *
330 * @param name the class name of the proxy class
331 * @param interfaces proxy interfaces
332 * @param accessFlags access flags of the proxy class
333 */
334 public static byte[] generateProxyClass(final String name,
335 Class<?>[] interfaces,
336 int accessFlags)
337 {
338 ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);
339 final byte[] classFile = gen.generateClassFile();
340
341 if (saveGeneratedFiles) {
342 java.security.AccessController.doPrivileged(
343 new java.security.PrivilegedAction<Void>() {
344 public Void run() {
345 try {
346 int i = name.lastIndexOf('.');
347 Path path;
348 if (i > 0) {
349 Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar));
350 Files.createDirectories(dir);
351 path = dir.resolve(name.substring(i+1, name.length()) + ".class");
352 } else {
353 path = Paths.get(name + ".class");
354 }
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.reflect;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.DataOutputStream;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.lang.reflect.Array;
34 import java.lang.reflect.Method;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.ListIterator;
43 import java.util.Map;
44 import sun.security.action.GetBooleanAction;
45
46 /**
47 * ProxyGenerator contains the code to generate a dynamic proxy class
48 * for the java.lang.reflect.Proxy API.
49 *
50 * The external interfaces to ProxyGenerator is the static
51 * "generateProxyClass" method.
52 *
53 * @author Peter Jones
54 * @since 1.3
55 */
56 class ProxyGenerator {
57 /*
58 * In the comments below, "JVMS" refers to The Java Virtual Machine
59 * Specification Second Edition and "JLS" refers to the original
60 * version of The Java Language Specification, unless otherwise
61 * specified.
62 */
63
64 /* generate 1.5-era class file version */
65 private static final int CLASSFILE_MAJOR_VERSION = 49;
66 private static final int CLASSFILE_MINOR_VERSION = 0;
67
68 /*
69 * beginning of constants copied from
70 * sun.tools.java.RuntimeConstants (which no longer exists):
71 */
72
73 /* constant pool tags */
74 private static final int CONSTANT_UTF8 = 1;
75 private static final int CONSTANT_UNICODE = 2;
76 private static final int CONSTANT_INTEGER = 3;
297 // private static final int opc_monitorexit = 195;
298 private static final int opc_wide = 196;
299 // private static final int opc_multianewarray = 197;
300 // private static final int opc_ifnull = 198;
301 // private static final int opc_ifnonnull = 199;
302 // private static final int opc_goto_w = 200;
303 // private static final int opc_jsr_w = 201;
304
305 // end of constants copied from sun.tools.java.RuntimeConstants
306
307 /** name of the superclass of proxy classes */
308 private static final String superclassName = "java/lang/reflect/Proxy";
309
310 /** name of field for storing a proxy instance's invocation handler */
311 private static final String handlerFieldName = "h";
312
313 /** debugging flag for saving generated class files */
314 private static final boolean saveGeneratedFiles =
315 java.security.AccessController.doPrivileged(
316 new GetBooleanAction(
317 "java.lang.reflect.ProxyGenerator.saveGeneratedFiles")).booleanValue();
318
319 /**
320 * Generate a public proxy class given a name and a list of proxy interfaces.
321 */
322 static byte[] generateProxyClass(final String name,
323 Class<?>[] interfaces) {
324 return generateProxyClass(name, interfaces, (ACC_PUBLIC | ACC_FINAL | ACC_SUPER));
325 }
326
327 /**
328 * Generate a proxy class given a name and a list of proxy interfaces.
329 *
330 * @param name the class name of the proxy class
331 * @param interfaces proxy interfaces
332 * @param accessFlags access flags of the proxy class
333 */
334 static byte[] generateProxyClass(final String name,
335 Class<?>[] interfaces,
336 int accessFlags)
337 {
338 ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);
339 final byte[] classFile = gen.generateClassFile();
340
341 if (saveGeneratedFiles) {
342 java.security.AccessController.doPrivileged(
343 new java.security.PrivilegedAction<Void>() {
344 public Void run() {
345 try {
346 int i = name.lastIndexOf('.');
347 Path path;
348 if (i > 0) {
349 Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar));
350 Files.createDirectories(dir);
351 path = dir.resolve(name.substring(i+1, name.length()) + ".class");
352 } else {
353 path = Paths.get(name + ".class");
354 }
|