langtools/test/tools/javac/tree/T6993305.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27319 030080f03e4f
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     4
 *
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     8
 *
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    13
 * accompanied this code).
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    14
 *
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    18
 *
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    21
 * questions.
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    22
 */
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    23
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    24
/*
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    25
 * @test
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    26
 * @bug 6993305
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    27
 * @summary starting position of a method without modifiers and with type parameters is incorrect
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.file
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    30
 */
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    31
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    32
import java.io.File;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    33
import javax.tools.JavaFileObject;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    34
import javax.tools.StandardJavaFileManager;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    35
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    36
import com.sun.source.tree.CompilationUnitTree;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    37
import com.sun.source.tree.MethodTree;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    38
import com.sun.source.util.JavacTask;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    39
import com.sun.source.util.SourcePositions;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    40
import com.sun.source.util.TreeScanner;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    41
import com.sun.source.util.Trees;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    42
import com.sun.tools.javac.api.JavacTool;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    43
import java.io.IOException;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    44
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    45
/*
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    46
 * Test verifies the starting position of all methods by computing the start position
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    47
 * of each method as the first non-white character on the first line containing
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    48
 * (" " + methodName + "("), and then comparing this value against the reported
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    49
 * value in the SourcePositions table.
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    50
 */
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    51
public class T6993305 {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    52
    <T> void test1(T t) { }  // this is the primary case to be tested
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    53
    public <T> void test2(T t) { }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    54
    @Deprecated <T> void test3(T t) { }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    55
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    56
    public static void main(String... args) throws Exception {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    57
        new T6993305().run();
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    58
    }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    59
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    60
    void run() throws Exception {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    61
        File testSrc = new File(System.getProperty("test.src"));
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    62
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    63
        JavacTool tool = JavacTool.create();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    64
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    65
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    66
            File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    67
            Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    68
            JavacTask task = tool.getTask(null, fm, null, null, null, fos);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    69
            Iterable<? extends CompilationUnitTree> cus = task.parse();
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    70
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    71
            TestScanner s = new TestScanner();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    72
            s.scan(cus, task);
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    73
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    74
            if (errors > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    75
                throw new Exception(errors + " errors occurred");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 8033
diff changeset
    76
        }
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    77
    }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    78
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    79
    void error(String msg) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    80
        System.err.println("Error: " + msg);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    81
        errors++;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    82
    }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    83
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    84
    int errors;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    85
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    86
    class TestScanner extends TreeScanner<Void, JavacTask> {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    87
        CompilationUnitTree cu;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    88
        SourcePositions sourcePositions;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    89
        String source;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    90
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    91
        void show(String label, int pos) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    92
            System.err.println(label + ": " +
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    93
                    source.substring(pos, Math.min(source.length(), pos + 10)));
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    94
        }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    95
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    96
        @Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    97
            cu = tree;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    98
            Trees trees = Trees.instance(task);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
    99
            sourcePositions = trees.getSourcePositions();
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   100
            try {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   101
                source = String.valueOf(tree.getSourceFile().getCharContent(true));
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   102
            } catch (IOException e) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   103
                throw new Error(e);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   104
            }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   105
            return super.visitCompilationUnit(tree, task);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   106
        }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   107
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   108
        // this is the core of the test
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   109
        @Override public Void visitMethod(MethodTree tree, JavacTask task) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   110
            String name = String.valueOf(tree.getName());
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   111
            int pos = source.indexOf(" " + name + "(");
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   112
            while (source.charAt(pos - 1) != '\n') pos--;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   113
            while (source.charAt(pos) == ' ') pos++;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   114
            int expectedStart = pos;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   115
            int reportedStart = (int) sourcePositions.getStartPosition(cu, tree);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   116
            System.err.println("Method " + name
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   117
                    + " expectedStart:" + expectedStart
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   118
                    + " reportedStart:" + reportedStart);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   119
            if (expectedStart != reportedStart) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   120
                error("Unexpected value for " + name);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   121
                show("expected", expectedStart);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   122
                show("reported", reportedStart);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   123
            }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   124
            return super.visitMethod(tree, task);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   125
        }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   126
    }
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents:
diff changeset
   127
}