langtools/test/tools/javac/api/TestJavacTask_ParseAttrGen.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27319 030080f03e4f
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
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
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     2
 * Copyright (c) 2011, 2015, 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
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
 * @modules jdk.compiler
8844
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
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    31
import java.io.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    32
import java.util.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    33
import javax.lang.model.element.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    34
import javax.tools.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    35
import com.sun.source.tree.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    36
import com.sun.source.util.*;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    37
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    38
public class TestJavacTask_ParseAttrGen {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    39
    public static void main(String... args) throws Exception {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    40
        new TestJavacTask_ParseAttrGen().run();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    41
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    42
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    43
    JavaCompiler comp;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    44
    StandardJavaFileManager fm;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    45
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    46
    void run() throws Exception {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    47
        comp = ToolProvider.getSystemJavaCompiler();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    48
        fm = comp.getStandardFileManager(null, null, null);
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    49
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    50
            final boolean[] booleanValues = { false, true };
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    51
            for (boolean pk: booleanValues) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    52
                for (boolean ak: booleanValues) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    53
                    for (boolean gk: booleanValues) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    54
                        test(pk, ak, gk);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    55
                    }
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    56
                }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    57
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    58
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 9744
diff changeset
    59
            fm.close();
8844
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
    }
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
    void test(boolean pk, boolean ak, boolean gk) throws Exception {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    64
        if (!pk && !ak && !gk)  // nothing to do
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    65
            return;
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
        System.err.println("test: pk:" + pk + ", ak:" + ak + ", gk: " + gk);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    68
        File testSrc = new File(System.getProperty("test.src"));
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    69
        String thisClassName = TestJavacTask_ParseAttrGen.class.getName();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    70
        Iterable<? extends JavaFileObject> files =
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    71
                fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    72
        File tmpDir = new File((pk ? "p" : "") + (ak ? "a" : "") + (gk ? "g" : ""));
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    73
        tmpDir.mkdirs();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    74
        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    75
        JavacTask t = (JavacTask) comp.getTask(null, fm, null, null, null, files);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    76
        //t.setTaskListener(createTaskListener());
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    77
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    78
        try {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    79
            if (pk) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    80
                Iterable<? extends CompilationUnitTree> trees = t.parse();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    81
                System.err.println(count(trees) + " trees parsed");
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
            if (ak) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    85
                Iterable<? extends Element> elems = t.analyze();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    86
                System.err.println(count(elems) + " elements analyzed");
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    87
            }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    88
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    89
            if (gk) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    90
                Iterable<? extends JavaFileObject> classfiles = t.generate();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    91
                System.err.println(count(classfiles) + " class files generated");
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    92
            }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    93
        } catch (IOException e) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    94
            error("unexpected exception caught: " + e);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    95
        }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    96
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    97
        File[] genFiles = tmpDir.listFiles();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    98
        int expect = (gk ? 2 : 0); // main class and anon class for TaskListener
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
    99
        if (genFiles.length != expect)
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   100
            error("unexpected number of files generated: " + genFiles.length
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   101
                    + ", expected: " + expect);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   102
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   103
        System.err.println();
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   104
    }
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
    TaskListener createTaskListener() {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   107
        return new TaskListener() {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   108
            public void started(TaskEvent e) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   109
                System.err.println(e + " started");
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   110
            }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   111
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   112
            public void finished(TaskEvent e) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   113
                System.err.println(e + " finished");
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   114
            }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   115
        };
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   116
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   117
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   118
    <T> int count(Iterable<T> items) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   119
        int count = 0;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   120
        for (T item: items)
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   121
            count++;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   122
        return count;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   123
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   124
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   125
    void error(String msg) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   126
        System.err.println("Error: " + msg);
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   127
        errors++;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   128
    }
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   129
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   130
    int errors;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
diff changeset
   131
}