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