test/tools/javac/api/TestJavacTask_ParseAttrGen.java

Mon, 14 Mar 2011 11:48:41 -0700

author
jjg
date
Mon, 14 Mar 2011 11:48:41 -0700
changeset 930
cb119107aeea
child 1013
8eb952f43b11
permissions
-rw-r--r--

7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
Reviewed-by: mcimadamore

jjg@930 1 /*
jjg@930 2 * Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved.
jjg@930 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@930 4 *
jjg@930 5 * This code is free software; you can redistribute it and/or modify it
jjg@930 6 * under the terms of the GNU General Public License version 2 only, as
jjg@930 7 * published by the Free Software Foundation.
jjg@930 8 *
jjg@930 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@930 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@930 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@930 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@930 13 * accompanied this code).
jjg@930 14 *
jjg@930 15 * You should have received a copy of the GNU General Public License version
jjg@930 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@930 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@930 18 *
jjg@930 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@930 20 * or visit www.oracle.com if you need additional information or have any
jjg@930 21 * questions.
jjg@930 22 */
jjg@930 23
jjg@930 24 /*
jjg@930 25 * @test
jjg@930 26 * @bug 7026509
jjg@930 27 * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg@930 28 */
jjg@930 29
jjg@930 30 import java.io.*;
jjg@930 31 import java.util.*;
jjg@930 32 import javax.lang.model.element.*;
jjg@930 33 import javax.tools.*;
jjg@930 34 import com.sun.source.tree.*;
jjg@930 35 import com.sun.source.util.*;
jjg@930 36
jjg@930 37 public class TestJavacTask_ParseAttrGen {
jjg@930 38 public static void main(String... args) throws Exception {
jjg@930 39 new TestJavacTask_ParseAttrGen().run();
jjg@930 40 }
jjg@930 41
jjg@930 42 JavaCompiler comp;
jjg@930 43 StandardJavaFileManager fm;
jjg@930 44
jjg@930 45 void run() throws Exception {
jjg@930 46 comp = ToolProvider.getSystemJavaCompiler();
jjg@930 47 fm = comp.getStandardFileManager(null, null, null);
jjg@930 48
jjg@930 49 final boolean[] booleanValues = { false, true };
jjg@930 50 for (boolean pk: booleanValues) {
jjg@930 51 for (boolean ak: booleanValues) {
jjg@930 52 for (boolean gk: booleanValues) {
jjg@930 53 test(pk, ak, gk);
jjg@930 54 }
jjg@930 55 }
jjg@930 56 }
jjg@930 57 }
jjg@930 58
jjg@930 59 void test(boolean pk, boolean ak, boolean gk) throws Exception {
jjg@930 60 if (!pk && !ak && !gk) // nothing to do
jjg@930 61 return;
jjg@930 62
jjg@930 63 System.err.println("test: pk:" + pk + ", ak:" + ak + ", gk: " + gk);
jjg@930 64 File testSrc = new File(System.getProperty("test.src"));
jjg@930 65 String thisClassName = TestJavacTask_ParseAttrGen.class.getName();
jjg@930 66 Iterable<? extends JavaFileObject> files =
jjg@930 67 fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
jjg@930 68 File tmpDir = new File((pk ? "p" : "") + (ak ? "a" : "") + (gk ? "g" : ""));
jjg@930 69 tmpDir.mkdirs();
jjg@930 70 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));
jjg@930 71 JavacTask t = (JavacTask) comp.getTask(null, fm, null, null, null, files);
jjg@930 72 //t.setTaskListener(createTaskListener());
jjg@930 73
jjg@930 74 try {
jjg@930 75 if (pk) {
jjg@930 76 Iterable<? extends CompilationUnitTree> trees = t.parse();
jjg@930 77 System.err.println(count(trees) + " trees parsed");
jjg@930 78 }
jjg@930 79
jjg@930 80 if (ak) {
jjg@930 81 Iterable<? extends Element> elems = t.analyze();
jjg@930 82 System.err.println(count(elems) + " elements analyzed");
jjg@930 83 }
jjg@930 84
jjg@930 85 if (gk) {
jjg@930 86 Iterable<? extends JavaFileObject> classfiles = t.generate();
jjg@930 87 System.err.println(count(classfiles) + " class files generated");
jjg@930 88 }
jjg@930 89 } catch (IOException e) {
jjg@930 90 error("unexpected exception caught: " + e);
jjg@930 91 }
jjg@930 92
jjg@930 93 File[] genFiles = tmpDir.listFiles();
jjg@930 94 int expect = (gk ? 2 : 0); // main class and anon class for TaskListener
jjg@930 95 if (genFiles.length != expect)
jjg@930 96 error("unexpected number of files generated: " + genFiles.length
jjg@930 97 + ", expected: " + expect);
jjg@930 98
jjg@930 99 System.err.println();
jjg@930 100 }
jjg@930 101
jjg@930 102 TaskListener createTaskListener() {
jjg@930 103 return new TaskListener() {
jjg@930 104 public void started(TaskEvent e) {
jjg@930 105 System.err.println(e + " started");
jjg@930 106 }
jjg@930 107
jjg@930 108 public void finished(TaskEvent e) {
jjg@930 109 System.err.println(e + " finished");
jjg@930 110 }
jjg@930 111 };
jjg@930 112 }
jjg@930 113
jjg@930 114 <T> int count(Iterable<T> items) {
jjg@930 115 int count = 0;
jjg@930 116 for (T item: items)
jjg@930 117 count++;
jjg@930 118 return count;
jjg@930 119 }
jjg@930 120
jjg@930 121 void error(String msg) {
jjg@930 122 System.err.println("Error: " + msg);
jjg@930 123 errors++;
jjg@930 124 }
jjg@930 125
jjg@930 126 int errors;
jjg@930 127 }

mercurial