T6963934.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     6963934
27 * @summary JCCompilationUnit.getImports does not report all imports
28 * @modules jdk.compiler
29 */
30
31import java.io.File;
32import javax.tools.JavaCompiler;
33import javax.tools.StandardJavaFileManager;
34import javax.tools.ToolProvider;; // NOTE: extra semicolon for test
35
36import com.sun.source.tree.CompilationUnitTree;
37import com.sun.source.tree.ImportTree;
38import com.sun.source.util.JavacTask;
39; // NOTE: extra semicolon for test
40
41public class T6963934 {
42    public static void main(String[] args) throws Exception {
43        File testSrc = new File(System.getProperty("test.src"));
44        File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
45        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
46        try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
47            JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null,
48                    fileManager.getJavaFileObjects(thisSrc));
49            CompilationUnitTree tree = task.parse().iterator().next();
50            int count = 0;
51            for (ImportTree importTree : tree.getImports()) {
52                System.out.println(importTree);
53                count++;
54            }
55            int expected = 7;
56            if (count != expected)
57                throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
58        }
59    }
60}
61