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