langtools/test/tools/javac/tree/ClassTreeTest.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 6580 d3d578d22cc7
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
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
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
6580
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();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    44
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    45
            List<String> opts = Collections.<String>emptyList();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    46
            File testSrc = new File(System.getProperty("test.src"));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    47
            File thisFile = new File(testSrc, ClassTreeTest.class.getSimpleName() + ".java");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    48
            Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    49
            JavacTask task = tool.getTask(null, fm, null, opts, null, fos);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    50
            for (CompilationUnitTree cu: task.parse()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    51
                check(cu, "CLASS", Tree.Kind.CLASS);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    52
                check(cu, "INTERFACE", Tree.Kind.INTERFACE);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    53
                check(cu, "ENUM", Tree.Kind.ENUM);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    54
                check(cu, "ANNOTATION_TYPE", Tree.Kind.ANNOTATION_TYPE);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    55
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    56
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    57
            int expected = 4;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    58
            if (checks != expected)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    59
                error("Unexpected number of checks performed; expected: " + expected + ", found: " + checks);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    60
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    61
            if (errors > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 6580
diff changeset
    62
                throw new Exception(errors + " errors found");
6580
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
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    66
    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
    67
        checks++;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    68
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    69
        TreeScanner<ClassTree,String> s = new TreeScanner<ClassTree,String>() {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    70
            @Override
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    71
            public ClassTree visitClass(ClassTree c, String name) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    72
                if (c.getSimpleName().toString().equals(name))
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    73
                    return c;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    74
                else
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    75
                    return super.visitClass(c, name);
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
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    78
            @Override
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    79
            public ClassTree reduce(ClassTree t1, ClassTree t2) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    80
                return (t1 != null ? t1 : t2);
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
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    84
        ClassTree c = s.scan(cu, name);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    85
        if (c == null)
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    86
            error("Can't find node named " + name);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    87
        else if (c.getKind() != k)
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    88
            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
    89
    }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    90
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    91
    void error(String msg) {
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    92
        System.err.println("Error: " + msg);
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    93
        errors++;
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
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    96
    int checks;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    97
    int errors;
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    98
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
    99
    class CLASS { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
   100
    interface INTERFACE { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
   101
    enum ENUM { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
   102
    @interface ANNOTATION_TYPE { }
d3d578d22cc7 6570730: com.sun.source.tree.ModifiersTree.getFlags() should return class type
jjg
parents:
diff changeset
   103
}