T6993305.java revision 2687:56f8be952a5c
1/*
2 * Copyright (c) 2011, 2014, 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 */
23
24/*
25 * @test
26 * @bug 6993305
27 * @summary starting position of a method without modifiers and with type parameters is incorrect
28 */
29
30import java.io.File;
31import javax.tools.JavaFileObject;
32import javax.tools.StandardJavaFileManager;
33
34import com.sun.source.tree.CompilationUnitTree;
35import com.sun.source.tree.MethodTree;
36import com.sun.source.util.JavacTask;
37import com.sun.source.util.SourcePositions;
38import com.sun.source.util.TreeScanner;
39import com.sun.source.util.Trees;
40import com.sun.tools.javac.api.JavacTool;
41import java.io.IOException;
42
43/*
44 * Test verifies the starting position of all methods by computing the start position
45 * of each method as the first non-white character on the first line containing
46 * (" " + methodName + "("), and then comparing this value against the reported
47 * value in the SourcePositions table.
48 */
49public class T6993305 {
50    <T> void test1(T t) { }  // this is the primary case to be tested
51    public <T> void test2(T t) { }
52    @Deprecated <T> void test3(T t) { }
53
54    public static void main(String... args) throws Exception {
55        new T6993305().run();
56    }
57
58    void run() throws Exception {
59        File testSrc = new File(System.getProperty("test.src"));
60
61        JavacTool tool = JavacTool.create();
62        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
63
64            File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
65            Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
66            JavacTask task = tool.getTask(null, fm, null, null, null, fos);
67            Iterable<? extends CompilationUnitTree> cus = task.parse();
68
69            TestScanner s = new TestScanner();
70            s.scan(cus, task);
71
72            if (errors > 0)
73                throw new Exception(errors + " errors occurred");
74        }
75    }
76
77    void error(String msg) {
78        System.err.println("Error: " + msg);
79        errors++;
80    }
81
82    int errors;
83
84    class TestScanner extends TreeScanner<Void, JavacTask> {
85        CompilationUnitTree cu;
86        SourcePositions sourcePositions;
87        String source;
88
89        void show(String label, int pos) {
90            System.err.println(label + ": " +
91                    source.substring(pos, Math.min(source.length(), pos + 10)));
92        }
93
94        @Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
95            cu = tree;
96            Trees trees = Trees.instance(task);
97            sourcePositions = trees.getSourcePositions();
98            try {
99                source = String.valueOf(tree.getSourceFile().getCharContent(true));
100            } catch (IOException e) {
101                throw new Error(e);
102            }
103            return super.visitCompilationUnit(tree, task);
104        }
105
106        // this is the core of the test
107        @Override public Void visitMethod(MethodTree tree, JavacTask task) {
108            String name = String.valueOf(tree.getName());
109            int pos = source.indexOf(" " + name + "(");
110            while (source.charAt(pos - 1) != '\n') pos--;
111            while (source.charAt(pos) == ' ') pos++;
112            int expectedStart = pos;
113            int reportedStart = (int) sourcePositions.getStartPosition(cu, tree);
114            System.err.println("Method " + name
115                    + " expectedStart:" + expectedStart
116                    + " reportedStart:" + reportedStart);
117            if (expectedStart != reportedStart) {
118                error("Unexpected value for " + name);
119                show("expected", expectedStart);
120                show("reported", reportedStart);
121            }
122            return super.visitMethod(tree, task);
123        }
124    }
125}
126