langtools/test/tools/javac/api/TestJavacTask_Lock.java
author avstepan
Tue, 19 May 2015 16:04:14 +0400
changeset 30655 d83f50188ca9
parent 27319 030080f03e4f
child 30730 d3ce7619db2c
permissions -rw-r--r--
8080422: some docs cleanup for core libs Summary: some docs cleanup Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     1
/*
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 9744
diff changeset
     2
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     4
 *
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     8
 *
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    13
 * accompanied this code).
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    14
 *
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    18
 *
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    21
 * questions.
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    22
 */
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    23
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    24
/*
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    25
 * @test
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    26
 * @bug 7026509
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    27
 * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    28
 */
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    29
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    30
import java.io.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    31
import java.util.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    32
import javax.tools.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    33
import javax.tools.JavaCompiler.CompilationTask;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    34
import com.sun.source.util.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    35
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    36
public class TestJavacTask_Lock {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    37
    public static void main(String... args) throws Exception {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    38
        new TestJavacTask_Lock().run();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    39
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    40
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    41
    enum MethodKind {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    42
        CALL {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    43
            int test(CompilationTask t) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    44
                boolean ok = t.call();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    45
                if (!ok)
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    46
                    throw new Error("compilation failed");
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    47
                return 1;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    48
            }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    49
        },
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    50
        PARSE {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    51
            int test(CompilationTask t) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    52
                try {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    53
                    ((JavacTask) t).parse();
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 9744
diff changeset
    54
                    return 1;
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    55
                } catch (IOException ex) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    56
                    throw new Error(ex);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    57
                }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    58
            }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    59
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    60
        };
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    61
        abstract int test(CompilationTask t);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    62
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    63
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    64
    JavaCompiler comp;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    65
    StandardJavaFileManager fm;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    66
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    67
    void run() throws Exception {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    68
        comp = ToolProvider.getSystemJavaCompiler();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    69
        fm = comp.getStandardFileManager(null, null, null);
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    70
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    71
            for (MethodKind first: MethodKind.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    72
                for (MethodKind second: MethodKind.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    73
                    test(first, second);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    74
                }
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    75
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    76
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    77
            if (errors > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    78
                throw new Exception(errors + " errors found");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    79
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    80
            fm.close();
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    81
        }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    82
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    83
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    84
    void test(MethodKind first, MethodKind second) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    85
        System.err.println("test: " + first + ", " + second);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    86
        File testSrc = new File(System.getProperty("test.src"));
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    87
        String thisClassName = TestJavacTask_Lock.class.getName();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    88
        Iterable<? extends JavaFileObject> files =
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    89
                fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    90
        File tmpDir = new File(first + "_" + second);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    91
        tmpDir.mkdirs();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    92
        List<String> options = Arrays.asList( "-d", tmpDir.getPath() );
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    93
        CompilationTask t = comp.getTask(null, fm, null, options, null, files);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    94
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    95
        try {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    96
            first.test(t);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    97
            second.test(t);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    98
            error("No exception thrown");
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    99
        } catch (IllegalStateException e) {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 9744
diff changeset
   100
            e.printStackTrace();
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   101
            System.err.println("Expected exception caught: " + e);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   102
        } catch (Exception e) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   103
            error("Unexpected exception caught: " + e);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   104
            e.printStackTrace(System.err);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   105
        }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   106
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   107
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   108
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   109
    void error(String msg) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   110
        System.err.println("Error: " + msg);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   111
        errors++;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   112
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   113
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   114
    int errors;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   115
}