langtools/test/tools/javac/api/TestEvalExpression.java
changeset 10 06bc494ca11e
child 5520 86e4b9a9da40
equal deleted inserted replaced
0:fd16c54261b3 10:06bc494ca11e
       
     1 /*
       
     2  * Copyright 2005-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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug     4164450
       
    27  * @summary JSR 199: Standard interface for Java compilers
       
    28  * @author  Peter von der Ah\u00e9
       
    29  * @compile TestEvalExpression.java evalexpr/ByteArrayClassLoader.java  evalexpr/CompileFromString.java  evalexpr/MemoryFileManager.java
       
    30  * @run main TestEvalExpression
       
    31  */
       
    32 
       
    33 import java.lang.reflect.Method;
       
    34 import java.util.*;
       
    35 import javax.swing.JOptionPane;
       
    36 import javax.tools.*;
       
    37 import static evalexpr.CompileFromString.*;
       
    38 
       
    39 public class TestEvalExpression {
       
    40     static int errorCount = 0;
       
    41     static class Listener implements DiagnosticListener<JavaFileObject> {
       
    42         public void report(Diagnostic<? extends JavaFileObject> message) {
       
    43             if (message.getKind() == Diagnostic.Kind.ERROR)
       
    44                 errorCount++;
       
    45             System.err.println(message);
       
    46         }
       
    47     }
       
    48 
       
    49     public static void main(String[] args) {
       
    50         // Get a compiler tool
       
    51         final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
    52         final List<String> compilerFlags = new ArrayList();
       
    53         compilerFlags.add("-Xlint:all"); // report all warnings
       
    54         compilerFlags.add("-g:none"); // don't generate debug info
       
    55         final DiagnosticListener<JavaFileObject> listener = new Listener();
       
    56         String expression = "System.getProperty(\"java.vendor\")";
       
    57         Object result = null;
       
    58         try {
       
    59             result = evalExpression(compiler, listener, compilerFlags, expression);
       
    60         } catch (Exception e) {
       
    61             throw new AssertionError(e);
       
    62         }
       
    63         if (result == ERROR)
       
    64             throw new AssertionError(result);
       
    65         System.out.format("%s => %s%n", expression, result);
       
    66         if (!System.getProperty("java.vendor").equals(result))
       
    67             throw new AssertionError(result);
       
    68         if (errorCount != 0)
       
    69             throw new AssertionError(errorCount);
       
    70         try {
       
    71             result = evalExpression(compiler, listener, compilerFlags, "fisk hest");
       
    72         } catch (Exception e) {
       
    73             throw new AssertionError(e);
       
    74         }
       
    75         if (errorCount == 0)
       
    76             throw new AssertionError(errorCount);
       
    77     }
       
    78 }