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