langtools/test/tools/javac/api/evalexpr/CompileFromString.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
package evalexpr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
import java.lang.reflect.Method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import javax.swing.JOptionPane;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import static javax.tools.StandardLocation.CLASS_OUTPUT;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 * JSR 199 Demo application: compile from a String.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    35
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
public class CompileFromString {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
     * The name of the class used to evaluate expressions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private final static String CLASS_NAME = "EvalExpression";
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
     * Object used to signal errors from evalExpression.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    public final static Object ERROR = new Object() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        public String toString() { return "error"; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     * Compile and evaluate the specified expression using the
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * given compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * @param compiler a JSR 199 compiler tool used to compile the given expression
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * @param expression a Java Programming Language expression
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * @return the value of the expression; ERROR if any errors occured during compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * @throws java.lang.Exception exceptions are ignored for brevity
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    public static Object evalExpression(JavaCompiler compiler,
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
                                        DiagnosticListener<JavaFileObject> listener,
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
                                        List<String> flags,
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                                        String expression)
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        throws Exception
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        // Use a customized file manager
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        MemoryFileManager mfm =
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            new MemoryFileManager(compiler.getStandardFileManager(listener, null, null));
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        // Create a file object from a string
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        JavaFileObject fileObject = mfm.makeSource(CLASS_NAME,
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            "public class " + CLASS_NAME + " {\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            "    public static Object eval() throws Throwable {\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            "        return " + expression + ";\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            "    }\n}\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        JavaCompiler.CompilationTask task =
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            compiler.getTask(null, mfm, listener, flags, null, Arrays.asList(fileObject));
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        if (task.call()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            // Obtain a class loader for the compiled classes
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            ClassLoader cl = mfm.getClassLoader(CLASS_OUTPUT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            // Load the compiled class
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            Class compiledClass = cl.loadClass(CLASS_NAME);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            // Find the eval method
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            Method eval = compiledClass.getMethod("eval");
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            // Invoke it
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            return eval.invoke(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
            // Report that an error occured
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            return ERROR;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * Main entry point for program; ask user for expressions,
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * compile, evaluate, and print them.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * @param args ignored
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * @throws java.lang.Exception exceptions are ignored for brevity
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    public static void main(String... args) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        // Get a compiler tool
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        final List<String> compilerFlags = new ArrayList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        compilerFlags.add("-Xlint:all"); // report all warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        compilerFlags.add("-g:none"); // don't generate debug info
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        String expression = "System.getProperty(\"java.vendor\")";
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            expression = JOptionPane.showInputDialog("Please enter a Java expression",
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                                                     expression);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            if (expression == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                return; // end program on "cancel"
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            long time = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            Object result = evalExpression(compiler, null, compilerFlags, expression);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            time = System.currentTimeMillis() - time;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            System.out.format("Elapsed time %dms %n", time);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            if (result == ERROR)
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                System.out.format("Error compiling \"%s\"%n", expression);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                System.out.format("%s => %s%n", expression, result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
}