test/tools/javac/api/T6392782.java

Mon, 06 Sep 2010 12:55:09 -0700

author
jjg
date
Mon, 06 Sep 2010 12:55:09 -0700
changeset 672
ea54372637a5
parent 554
9d9f26857129
child 679
fc73f83cd563
permissions
-rw-r--r--

6930507: Symbols for anonymous and local classes made too late for use by java tree API
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2006, 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 6392782
    27  * @summary TreeScanner.visitImport returns null, not result of nested scan
    28  */
    30 import java.io.*;
    31 import java.util.*;
    32 import javax.tools.*;
    33 import com.sun.source.tree.*;
    34 import com.sun.source.util.*;
    35 import com.sun.tools.javac.api.*;
    37 public class T6392782 {
    38     public static void main(String... args) throws IOException {
    39         String testSrc = System.getProperty("test.src", ".");
    40         JavacTool tool = JavacTool.create();
    41         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    42         Iterable<? extends JavaFileObject> files =
    43             fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6392782.class.getName()+".java")));
    44         JavacTask task = tool.getTask(null, fm, null, null, null, files);
    45         Iterable<? extends Tree> trees = task.parse();
    46         TreeScanner<Integer,Void> scanner = new MyScanner();
    47         check(scanner, 6, scanner.scan(trees, null));
    49         CountNodes nodeCounter = new CountNodes();
    50         // 383 nodes with the regular parser; 384 nodes with EndPosParser
    51         // We automatically swith to EndPosParser when calling JavacTask.parse()
    52         check(nodeCounter, 384, nodeCounter.scan(trees, null));
    54         CountIdentifiers idCounter = new CountIdentifiers();
    55         check(idCounter, 106, idCounter.scan(trees, null));
    56     }
    58     private static void check(TreeScanner<?,?> scanner, int expect, int found) {
    59         if (found != expect)
    60             throw new AssertionError(scanner.getClass().getName() + ": expected: " + expect + " found: " + found);
    61     }
    63     static class MyScanner extends TreeScanner<Integer,Void> {
    64         @Override
    65         public Integer visitImport(ImportTree tree, Void ignore) {
    66             //System.err.println(tree);
    67             return 1;
    68         }
    70         @Override
    71         public Integer reduce(Integer i1, Integer i2) {
    72             return (i1 == null ? 0 : i1) + (i2 == null ? 0 : i2);
    73         }
    74     }
    76     // example from TreeScanner javadoc
    77     static class CountNodes extends TreeScanner<Integer,Void> {
    78         @Override
    79         public Integer scan(Tree node, Void p) {
    80             Integer n = super.scan(node, p);
    81             return (n == null ? 0 : n) + 1;
    82         }
    83         @Override
    84         public Integer reduce(Integer r1, Integer r2) {
    85             return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
    86         }
    87     }
    89     // example from TreeScanner javadoc
    90     static class CountIdentifiers extends TreeScanner<Integer,Void> {
    91         @Override
    92         public Integer visitIdentifier(IdentifierTree node, Void p) {
    93             return 1;
    94         }
    95         @Override
    96         public Integer reduce(Integer r1, Integer r2) {
    97             return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
    98         }
    99     }
   100 }

mercurial