test/tools/javac/api/TestJavacTask_ParseAttrGen.java

changeset 930
cb119107aeea
child 1013
8eb952f43b11
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/TestJavacTask_ParseAttrGen.java	Mon Mar 14 11:48:41 2011 -0700
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 7026509
    1.30 + * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.util.*;
    1.35 +import javax.lang.model.element.*;
    1.36 +import javax.tools.*;
    1.37 +import com.sun.source.tree.*;
    1.38 +import com.sun.source.util.*;
    1.39 +
    1.40 +public class TestJavacTask_ParseAttrGen {
    1.41 +    public static void main(String... args) throws Exception {
    1.42 +        new TestJavacTask_ParseAttrGen().run();
    1.43 +    }
    1.44 +
    1.45 +    JavaCompiler comp;
    1.46 +    StandardJavaFileManager fm;
    1.47 +
    1.48 +    void run() throws Exception {
    1.49 +        comp = ToolProvider.getSystemJavaCompiler();
    1.50 +        fm = comp.getStandardFileManager(null, null, null);
    1.51 +
    1.52 +        final boolean[] booleanValues = { false, true };
    1.53 +        for (boolean pk: booleanValues) {
    1.54 +            for (boolean ak: booleanValues) {
    1.55 +                for (boolean gk: booleanValues) {
    1.56 +                    test(pk, ak, gk);
    1.57 +                }
    1.58 +            }
    1.59 +        }
    1.60 +    }
    1.61 +
    1.62 +    void test(boolean pk, boolean ak, boolean gk) throws Exception {
    1.63 +        if (!pk && !ak && !gk)  // nothing to do
    1.64 +            return;
    1.65 +
    1.66 +        System.err.println("test: pk:" + pk + ", ak:" + ak + ", gk: " + gk);
    1.67 +        File testSrc = new File(System.getProperty("test.src"));
    1.68 +        String thisClassName = TestJavacTask_ParseAttrGen.class.getName();
    1.69 +        Iterable<? extends JavaFileObject> files =
    1.70 +                fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
    1.71 +        File tmpDir = new File((pk ? "p" : "") + (ak ? "a" : "") + (gk ? "g" : ""));
    1.72 +        tmpDir.mkdirs();
    1.73 +        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));
    1.74 +        JavacTask t = (JavacTask) comp.getTask(null, fm, null, null, null, files);
    1.75 +        //t.setTaskListener(createTaskListener());
    1.76 +
    1.77 +        try {
    1.78 +            if (pk) {
    1.79 +                Iterable<? extends CompilationUnitTree> trees = t.parse();
    1.80 +                System.err.println(count(trees) + " trees parsed");
    1.81 +            }
    1.82 +
    1.83 +            if (ak) {
    1.84 +                Iterable<? extends Element> elems = t.analyze();
    1.85 +                System.err.println(count(elems) + " elements analyzed");
    1.86 +            }
    1.87 +
    1.88 +            if (gk) {
    1.89 +                Iterable<? extends JavaFileObject> classfiles = t.generate();
    1.90 +                System.err.println(count(classfiles) + " class files generated");
    1.91 +            }
    1.92 +        } catch (IOException e) {
    1.93 +            error("unexpected exception caught: " + e);
    1.94 +        }
    1.95 +
    1.96 +        File[] genFiles = tmpDir.listFiles();
    1.97 +        int expect = (gk ? 2 : 0); // main class and anon class for TaskListener
    1.98 +        if (genFiles.length != expect)
    1.99 +            error("unexpected number of files generated: " + genFiles.length
   1.100 +                    + ", expected: " + expect);
   1.101 +
   1.102 +        System.err.println();
   1.103 +    }
   1.104 +
   1.105 +    TaskListener createTaskListener() {
   1.106 +        return new TaskListener() {
   1.107 +            public void started(TaskEvent e) {
   1.108 +                System.err.println(e + " started");
   1.109 +            }
   1.110 +
   1.111 +            public void finished(TaskEvent e) {
   1.112 +                System.err.println(e + " finished");
   1.113 +            }
   1.114 +        };
   1.115 +    }
   1.116 +
   1.117 +    <T> int count(Iterable<T> items) {
   1.118 +        int count = 0;
   1.119 +        for (T item: items)
   1.120 +            count++;
   1.121 +        return count;
   1.122 +    }
   1.123 +
   1.124 +    void error(String msg) {
   1.125 +        System.err.println("Error: " + msg);
   1.126 +        errors++;
   1.127 +    }
   1.128 +
   1.129 +    int errors;
   1.130 +}

mercurial