1/*
2 * Copyright (c) 2006, 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 6357331
27 * @summary NPE from JavacTask.getElements() after calling CompilationTask.run
28 * @modules jdk.compiler
29 */
30import java.io.*;
31import java.util.*;
32import javax.tools.*;
33import com.sun.source.util.*;
34
35public class T6357331
36{
37    public static void main(String... args) throws IOException {
38        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
39        PrintWriter out = new PrintWriter(new StringWriter());
40        List<String> opts = Arrays.asList("-d", ".");
41        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
42            File thisFile = new File(System.getProperty("test.src"), "T6357331.java");
43            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
44            final JavacTask task = (JavacTask) (tool.getTask(out, fm, null, opts, null, files));
45
46            // set a listener to verify that IllegalStateException is not thrown
47            // during the compilation
48            task.setTaskListener(new TaskListener() {
49                    public void started(TaskEvent e) {
50                        task.getElements();
51                        task.getTypes();
52                    }
53                    public void finished(TaskEvent e) { }
54                });
55
56            task.call();
57
58            // now the compilation is over, we expect IllegalStateException (not NPE)
59            try {
60                task.getElements();
61                throw new AssertionError("IllegalStateException not thrown");
62            }
63            catch (IllegalStateException e) {
64                // expected
65            }
66
67            try {
68                task.getTypes();
69                throw new AssertionError("IllegalStateException not thrown");
70            }
71            catch (IllegalStateException e) {
72                // expected
73            }
74        }
75    }
76}
77