6963934: JCCompilationUnit.getImports does not report all imports

Mon, 24 Jan 2011 16:17:33 -0800

author
jjg
date
Mon, 24 Jan 2011 16:17:33 -0800
changeset 837
73ab0b128918
parent 832
57e3b9bc7fb8
child 838
22a040cbf0e0

6963934: JCCompilationUnit.getImports does not report all imports
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/tree/JCTree.java file | annotate | diff | comparison | revisions
test/tools/javac/tree/T6963934.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Mon Jan 24 15:45:41 2011 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Mon Jan 24 16:17:33 2011 -0800
     1.3 @@ -465,9 +465,10 @@
     1.4          public List<JCImport> getImports() {
     1.5              ListBuffer<JCImport> imports = new ListBuffer<JCImport>();
     1.6              for (JCTree tree : defs) {
     1.7 -                if (tree.getTag() == IMPORT)
     1.8 +                int tag = tree.getTag();
     1.9 +                if (tag == IMPORT)
    1.10                      imports.append((JCImport)tree);
    1.11 -                else
    1.12 +                else if (tag != SKIP)
    1.13                      break;
    1.14              }
    1.15              return imports.toList();
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/tree/T6963934.java	Mon Jan 24 16:17:33 2011 -0800
     2.3 @@ -0,0 +1,58 @@
     2.4 +/*
     2.5 + * Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug     6963934
    2.30 + * @summary JCCompilationUnit.getImports does not report all imports
    2.31 + */
    2.32 +
    2.33 +import java.io.File;
    2.34 +import javax.tools.JavaCompiler;
    2.35 +import javax.tools.StandardJavaFileManager;
    2.36 +import javax.tools.ToolProvider;; // NOTE: extra semicolon for test
    2.37 +
    2.38 +import com.sun.source.tree.CompilationUnitTree;
    2.39 +import com.sun.source.tree.ImportTree;
    2.40 +import com.sun.source.util.JavacTask;
    2.41 +; // NOTE: extra semicolon for test
    2.42 +
    2.43 +public class T6963934 {
    2.44 +    public static void main(String[] args) throws Exception {
    2.45 +        File testSrc = new File(System.getProperty("test.src"));
    2.46 +        File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
    2.47 +        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    2.48 +        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    2.49 +        JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null,
    2.50 +                fileManager.getJavaFileObjects(thisSrc));
    2.51 +        CompilationUnitTree tree = task.parse().iterator().next();
    2.52 +        int count = 0;
    2.53 +        for (ImportTree importTree : tree.getImports()) {
    2.54 +            System.out.println(importTree);
    2.55 +            count++;
    2.56 +        }
    2.57 +        int expected = 7;
    2.58 +        if (count != expected)
    2.59 +            throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
    2.60 +    }
    2.61 +}

mercurial