langtools/test/tools/javac/tree/ClassTreeTest.java
author jjg
Fri, 27 Aug 2010 17:21:17 -0700
changeset 6580 d3d578d22cc7
child 27319 030080f03e4f
permissions -rw-r--r--
6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6580
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     1
/*
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     4
 *
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     8
 *
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    13
 * accompanied this code).
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    14
 *
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    18
 *
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    21
 * questions.
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    22
 */
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    23
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    24
/*
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    25
 * @test
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    26
 * @bug 6570730
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    27
 * @summary com.sun.source.tree.ModifiersTree.getFlags() should return class type
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    28
 */
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    29
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    30
import java.io.*;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    31
import java.util.*;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    32
import javax.tools.*;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    33
import com.sun.source.tree.*;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    34
import com.sun.source.util.*;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.api.*;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    36
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    37
public class ClassTreeTest {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    38
    public static void main(String... args) throws Exception {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    39
        new ClassTreeTest().run();
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    40
    }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    41
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    42
    void run() throws Exception {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    43
        JavacTool tool = JavacTool.create();
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    44
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    45
        List<String> opts = Collections.<String>emptyList();
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    46
        File testSrc = new File(System.getProperty("test.src"));
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    47
        File thisFile = new File(testSrc, ClassTreeTest.class.getSimpleName() + ".java");
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    48
        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    49
        JavacTask task = tool.getTask(null, fm, null, opts, null, fos);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    50
        for (CompilationUnitTree cu: task.parse()) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    51
            check(cu, "CLASS", Tree.Kind.CLASS);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    52
            check(cu, "INTERFACE", Tree.Kind.INTERFACE);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    53
            check(cu, "ENUM", Tree.Kind.ENUM);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    54
            check(cu, "ANNOTATION_TYPE", Tree.Kind.ANNOTATION_TYPE);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    55
        }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    56
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    57
        int expected = 4;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    58
        if (checks != expected)
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    59
            error("Unexpected number of checks performed; expected: " + expected + ", found: " + checks);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    60
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    61
        if (errors > 0)
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    62
            throw new Exception(errors + " errors found");
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    63
    }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    64
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    65
    void check(CompilationUnitTree cu, String name, Tree.Kind k) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    66
        checks++;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    67
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    68
        TreeScanner<ClassTree,String> s = new TreeScanner<ClassTree,String>() {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    69
            @Override
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    70
            public ClassTree visitClass(ClassTree c, String name) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    71
                if (c.getSimpleName().toString().equals(name))
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    72
                    return c;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    73
                else
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    74
                    return super.visitClass(c, name);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    75
            }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    76
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    77
            @Override
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    78
            public ClassTree reduce(ClassTree t1, ClassTree t2) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    79
                return (t1 != null ? t1 : t2);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    80
            }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    81
        };
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    82
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    83
        ClassTree c = s.scan(cu, name);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    84
        if (c == null)
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    85
            error("Can't find node named " + name);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    86
        else if (c.getKind() != k)
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    87
            error("Unexpected kind for node named " + name + ": expected: " + k + ", found: " + c.getKind());
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    88
    }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    89
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    90
    void error(String msg) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    91
        System.err.println("Error: " + msg);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    92
        errors++;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    93
    }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    94
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    95
    int checks;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    96
    int errors;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    97
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    98
    class CLASS { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    99
    interface INTERFACE { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
   100
    enum ENUM { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
   101
    @interface ANNOTATION_TYPE { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
   102
}