test/tools/javac/api/TestJavacTask_ParseAttrGen.java

Wed, 25 May 2011 13:32:10 -0700

author
katleman
date
Wed, 25 May 2011 13:32:10 -0700
changeset 1013
8eb952f43b11
parent 930
cb119107aeea
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7044486: open jdk repos have files with incorrect copyright headers, which can end up in src bundles
Reviewed-by: ohair, trims

     1 /*
     2  * Copyright (c) 2011, 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  */
    24 /*
    25  * @test
    26  * @bug 7026509
    27  * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
    28  */
    30 import java.io.*;
    31 import java.util.*;
    32 import javax.lang.model.element.*;
    33 import javax.tools.*;
    34 import com.sun.source.tree.*;
    35 import com.sun.source.util.*;
    37 public class TestJavacTask_ParseAttrGen {
    38     public static void main(String... args) throws Exception {
    39         new TestJavacTask_ParseAttrGen().run();
    40     }
    42     JavaCompiler comp;
    43     StandardJavaFileManager fm;
    45     void run() throws Exception {
    46         comp = ToolProvider.getSystemJavaCompiler();
    47         fm = comp.getStandardFileManager(null, null, null);
    49         final boolean[] booleanValues = { false, true };
    50         for (boolean pk: booleanValues) {
    51             for (boolean ak: booleanValues) {
    52                 for (boolean gk: booleanValues) {
    53                     test(pk, ak, gk);
    54                 }
    55             }
    56         }
    57     }
    59     void test(boolean pk, boolean ak, boolean gk) throws Exception {
    60         if (!pk && !ak && !gk)  // nothing to do
    61             return;
    63         System.err.println("test: pk:" + pk + ", ak:" + ak + ", gk: " + gk);
    64         File testSrc = new File(System.getProperty("test.src"));
    65         String thisClassName = TestJavacTask_ParseAttrGen.class.getName();
    66         Iterable<? extends JavaFileObject> files =
    67                 fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
    68         File tmpDir = new File((pk ? "p" : "") + (ak ? "a" : "") + (gk ? "g" : ""));
    69         tmpDir.mkdirs();
    70         fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));
    71         JavacTask t = (JavacTask) comp.getTask(null, fm, null, null, null, files);
    72         //t.setTaskListener(createTaskListener());
    74         try {
    75             if (pk) {
    76                 Iterable<? extends CompilationUnitTree> trees = t.parse();
    77                 System.err.println(count(trees) + " trees parsed");
    78             }
    80             if (ak) {
    81                 Iterable<? extends Element> elems = t.analyze();
    82                 System.err.println(count(elems) + " elements analyzed");
    83             }
    85             if (gk) {
    86                 Iterable<? extends JavaFileObject> classfiles = t.generate();
    87                 System.err.println(count(classfiles) + " class files generated");
    88             }
    89         } catch (IOException e) {
    90             error("unexpected exception caught: " + e);
    91         }
    93         File[] genFiles = tmpDir.listFiles();
    94         int expect = (gk ? 2 : 0); // main class and anon class for TaskListener
    95         if (genFiles.length != expect)
    96             error("unexpected number of files generated: " + genFiles.length
    97                     + ", expected: " + expect);
    99         System.err.println();
   100     }
   102     TaskListener createTaskListener() {
   103         return new TaskListener() {
   104             public void started(TaskEvent e) {
   105                 System.err.println(e + " started");
   106             }
   108             public void finished(TaskEvent e) {
   109                 System.err.println(e + " finished");
   110             }
   111         };
   112     }
   114     <T> int count(Iterable<T> items) {
   115         int count = 0;
   116         for (T item: items)
   117             count++;
   118         return count;
   119     }
   121     void error(String msg) {
   122         System.err.println("Error: " + msg);
   123         errors++;
   124     }
   126     int errors;
   127 }

mercurial