aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7026509 aoqi@0: * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import javax.lang.model.element.*; aoqi@0: import javax.tools.*; aoqi@0: import com.sun.source.tree.*; aoqi@0: import com.sun.source.util.*; aoqi@0: aoqi@0: public class TestJavacTask_ParseAttrGen { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new TestJavacTask_ParseAttrGen().run(); aoqi@0: } aoqi@0: aoqi@0: JavaCompiler comp; aoqi@0: StandardJavaFileManager fm; aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: comp = ToolProvider.getSystemJavaCompiler(); aoqi@0: fm = comp.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: final boolean[] booleanValues = { false, true }; aoqi@0: for (boolean pk: booleanValues) { aoqi@0: for (boolean ak: booleanValues) { aoqi@0: for (boolean gk: booleanValues) { aoqi@0: test(pk, ak, gk); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void test(boolean pk, boolean ak, boolean gk) throws Exception { aoqi@0: if (!pk && !ak && !gk) // nothing to do aoqi@0: return; aoqi@0: aoqi@0: System.err.println("test: pk:" + pk + ", ak:" + ak + ", gk: " + gk); aoqi@0: File testSrc = new File(System.getProperty("test.src")); aoqi@0: String thisClassName = TestJavacTask_ParseAttrGen.class.getName(); aoqi@0: Iterable files = aoqi@0: fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java")); aoqi@0: File tmpDir = new File((pk ? "p" : "") + (ak ? "a" : "") + (gk ? "g" : "")); aoqi@0: tmpDir.mkdirs(); aoqi@0: fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir)); aoqi@0: JavacTask t = (JavacTask) comp.getTask(null, fm, null, null, null, files); aoqi@0: //t.setTaskListener(createTaskListener()); aoqi@0: aoqi@0: try { aoqi@0: if (pk) { aoqi@0: Iterable trees = t.parse(); aoqi@0: System.err.println(count(trees) + " trees parsed"); aoqi@0: } aoqi@0: aoqi@0: if (ak) { aoqi@0: Iterable elems = t.analyze(); aoqi@0: System.err.println(count(elems) + " elements analyzed"); aoqi@0: } aoqi@0: aoqi@0: if (gk) { aoqi@0: Iterable classfiles = t.generate(); aoqi@0: System.err.println(count(classfiles) + " class files generated"); aoqi@0: } aoqi@0: } catch (IOException e) { aoqi@0: error("unexpected exception caught: " + e); aoqi@0: } aoqi@0: aoqi@0: File[] genFiles = tmpDir.listFiles(); aoqi@0: int expect = (gk ? 2 : 0); // main class and anon class for TaskListener aoqi@0: if (genFiles.length != expect) aoqi@0: error("unexpected number of files generated: " + genFiles.length aoqi@0: + ", expected: " + expect); aoqi@0: aoqi@0: System.err.println(); aoqi@0: } aoqi@0: aoqi@0: TaskListener createTaskListener() { aoqi@0: return new TaskListener() { aoqi@0: public void started(TaskEvent e) { aoqi@0: System.err.println(e + " started"); aoqi@0: } aoqi@0: aoqi@0: public void finished(TaskEvent e) { aoqi@0: System.err.println(e + " finished"); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: int count(Iterable items) { aoqi@0: int count = 0; aoqi@0: for (T item: items) aoqi@0: count++; aoqi@0: return count; aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: }