test/tools/javac/api/TestJavacTask_Multiple.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1013
8eb952f43b11
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 7026509
    27  * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
    28  */
    30 import java.io.*;
    31 import java.util.*;
    32 import javax.tools.*;
    33 import javax.tools.JavaCompiler.CompilationTask;
    34 import com.sun.source.util.*;
    36 public class TestJavacTask_Multiple {
    37     public static void main(String... args) throws Exception {
    38         new TestJavacTask_Multiple().run();
    39     }
    41     final int MAX_TASKS = 3;
    43     enum TestKind {
    44         CALL {
    45             int test(CompilationTask t) {
    46                 boolean ok = t.call();
    47                 if (!ok)
    48                     throw new Error("compilation failed");
    49                 return 1;
    50             }
    51         },
    52         PARSE {
    53             int test(CompilationTask t) {
    54                 try {
    55                     ((JavacTask) t).parse();
    56                 return 1;
    57                 } catch (IOException ex) {
    58                     throw new Error(ex);
    59                 }
    60             }
    62         };
    63         abstract int test(CompilationTask t);
    64     }
    66     int count;
    68     void run() throws Exception {
    69         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    70         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    71         for (TestKind tk: TestKind.values()) {
    72             test(comp, fm, tk);
    73         }
    75         int expect = TestKind.values().length * MAX_TASKS;
    76         if (count != expect) {
    77             throw new Exception("Unexpected number of tests completed: " + count
    78                     + ", expected: " + expect);
    79         }
    81     }
    83     void test(JavaCompiler comp, StandardJavaFileManager fm, TestKind tk) {
    84         System.err.println("test " + tk);
    85         File testSrc = new File(System.getProperty("test.src"));
    86         String thisClassName = TestJavacTask_Multiple.class.getName();
    87         Iterable<? extends JavaFileObject> files =
    88                 fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
    90         List<CompilationTask> tasks = new ArrayList<CompilationTask>();
    91         for (int i = 1; i <= MAX_TASKS; i++) {
    92             File tmpDir = new File(tk + "_" + i);
    93             tmpDir.mkdirs();
    94             List<String> options = Arrays.asList( "-d", tmpDir.getPath() );
    95             CompilationTask t = comp.getTask(null, fm, null, options, null, files);
    96             ((JavacTask) t).setTaskListener(createTaskListener(tk, i));
    97             tasks.add(t);
    98         }
   100         for (CompilationTask t: tasks)
   101             count += tk.test(t);
   103         System.err.println();
   104     }
   106     TaskListener createTaskListener(final TestKind tk, final int i) {
   107         return new TaskListener() {
   109             public void started(TaskEvent e) {
   110                 System.err.println(tk + "." + i + ": " + e + " started");
   111             }
   113             public void finished(TaskEvent e) {
   114                 System.err.println(tk + "." + i + ": " + e + " finished");
   115             }
   116         };
   117     }
   118 }

mercurial