1 /*
2 * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package java.lang;
27
28 /**
29 * The {@code Compiler} class is provided to support Java-to-native-code
30 * compilers and related services. By design, the {@code Compiler} class does
31 * nothing; it serves as a placeholder for a JIT compiler implementation.
32 *
33 * <p> When the Java Virtual Machine first starts, it determines if the system
34 * property {@code java.compiler} exists. (System properties are accessible
35 * through {@link System#getProperty(String)} and {@link
36 * System#getProperty(String, String)}. If so, it is assumed to be the name of
37 * a library (with a platform-dependent exact location and type); {@link
38 * System#loadLibrary} is called to load that library. If this loading
39 * succeeds, the function named {@code java_lang_Compiler_start()} in that
40 * library is called.
41 *
42 * <p> If no compiler is available, these methods do nothing.
43 *
44 * @author Frank Yellin
45 * @since JDK1.0
46 */
47 public final class Compiler {
48 private Compiler() {} // don't make instances
49
50 private static native void initialize();
51
52 private static native void registerNatives();
53
54 static {
55 registerNatives();
56 java.security.AccessController.doPrivileged(
57 new java.security.PrivilegedAction<Void>() {
58 public Void run() {
59 boolean loaded = false;
60 String jit = System.getProperty("java.compiler");
61 if ((jit != null) && (!jit.equals("NONE")) &&
62 (!jit.equals("")))
63 {
64 try {
65 System.loadLibrary(jit);
66 initialize();
67 loaded = true;
68 } catch (UnsatisfiedLinkError e) {
69 System.err.println("Warning: JIT compiler \"" +
70 jit + "\" not found. Will use interpreter.");
71 }
72 }
73 String info = System.getProperty("java.vm.info");
74 if (loaded) {
75 System.setProperty("java.vm.info", info + ", " + jit);
76 } else {
77 System.setProperty("java.vm.info", info + ", nojit");
78 }
79 return null;
80 }
81 });
82 }
83
84 /**
85 * Compiles the specified class.
86 *
87 * @param clazz
88 * A class
89 *
90 * @return {@code true} if the compilation succeeded; {@code false} if the
91 * compilation failed or no compiler is available
92 *
93 * @throws NullPointerException
94 * If {@code clazz} is {@code null}
95 */
96 public static native boolean compileClass(Class<?> clazz);
97
98 /**
99 * Compiles all classes whose name matches the specified string.
100 *
101 * @param string
102 * The name of the classes to compile
103 *
104 * @return {@code true} if the compilation succeeded; {@code false} if the
105 * compilation failed or no compiler is available
106 *
107 * @throws NullPointerException
108 * If {@code string} is {@code null}
109 */
110 public static native boolean compileClasses(String string);
111
112 /**
113 * Examines the argument type and its fields and perform some documented
114 * operation. No specific operations are required.
115 *
116 * @param any
117 * An argument
118 *
119 * @return A compiler-specific value, or {@code null} if no compiler is
120 * available
121 *
122 * @throws NullPointerException
123 * If {@code any} is {@code null}
124 */
125 public static native Object command(Object any);
126
127 /**
128 * Cause the Compiler to resume operation.
129 */
130 public static native void enable();
131
132 /**
133 * Cause the Compiler to cease operation.
134 */
135 public static native void disable();
136 }