test/tools/javac/tree/T6993305.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/tree/T6993305.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6993305
    1.30 + * @summary starting position of a method without modifiers and with type parameters is incorrect
    1.31 + */
    1.32 +
    1.33 +import java.io.File;
    1.34 +import javax.tools.JavaFileObject;
    1.35 +import javax.tools.StandardJavaFileManager;
    1.36 +
    1.37 +import com.sun.source.tree.CompilationUnitTree;
    1.38 +import com.sun.source.tree.MethodTree;
    1.39 +import com.sun.source.util.JavacTask;
    1.40 +import com.sun.source.util.SourcePositions;
    1.41 +import com.sun.source.util.TreeScanner;
    1.42 +import com.sun.source.util.Trees;
    1.43 +import com.sun.tools.javac.api.JavacTool;
    1.44 +import java.io.IOException;
    1.45 +
    1.46 +/*
    1.47 + * Test verifies the starting position of all methods by computing the start position
    1.48 + * of each method as the first non-white character on the first line containing
    1.49 + * (" " + methodName + "("), and then comparing this value against the reported
    1.50 + * value in the SourcePositions table.
    1.51 + */
    1.52 +public class T6993305 {
    1.53 +    <T> void test1(T t) { }  // this is the primary case to be tested
    1.54 +    public <T> void test2(T t) { }
    1.55 +    @Deprecated <T> void test3(T t) { }
    1.56 +
    1.57 +    public static void main(String... args) throws Exception {
    1.58 +        new T6993305().run();
    1.59 +    }
    1.60 +
    1.61 +    void run() throws Exception {
    1.62 +        File testSrc = new File(System.getProperty("test.src"));
    1.63 +
    1.64 +        JavacTool tool = JavacTool.create();
    1.65 +        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    1.66 +
    1.67 +        File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
    1.68 +        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
    1.69 +        JavacTask task = tool.getTask(null, fm, null, null, null, fos);
    1.70 +        Iterable<? extends CompilationUnitTree> cus = task.parse();
    1.71 +
    1.72 +        TestScanner s = new TestScanner();
    1.73 +        s.scan(cus, task);
    1.74 +
    1.75 +        if (errors > 0)
    1.76 +            throw new Exception(errors + " errors occurred");
    1.77 +    }
    1.78 +
    1.79 +    void error(String msg) {
    1.80 +        System.err.println("Error: " + msg);
    1.81 +        errors++;
    1.82 +    }
    1.83 +
    1.84 +    int errors;
    1.85 +
    1.86 +    class TestScanner extends TreeScanner<Void, JavacTask> {
    1.87 +        CompilationUnitTree cu;
    1.88 +        SourcePositions sourcePositions;
    1.89 +        String source;
    1.90 +
    1.91 +        void show(String label, int pos) {
    1.92 +            System.err.println(label + ": " +
    1.93 +                    source.substring(pos, Math.min(source.length(), pos + 10)));
    1.94 +        }
    1.95 +
    1.96 +        @Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    1.97 +            cu = tree;
    1.98 +            Trees trees = Trees.instance(task);
    1.99 +            sourcePositions = trees.getSourcePositions();
   1.100 +            try {
   1.101 +                source = String.valueOf(tree.getSourceFile().getCharContent(true));
   1.102 +            } catch (IOException e) {
   1.103 +                throw new Error(e);
   1.104 +            }
   1.105 +            return super.visitCompilationUnit(tree, task);
   1.106 +        }
   1.107 +
   1.108 +        // this is the core of the test
   1.109 +        @Override public Void visitMethod(MethodTree tree, JavacTask task) {
   1.110 +            String name = String.valueOf(tree.getName());
   1.111 +            int pos = source.indexOf(" " + name + "(");
   1.112 +            while (source.charAt(pos - 1) != '\n') pos--;
   1.113 +            while (source.charAt(pos) == ' ') pos++;
   1.114 +            int expectedStart = pos;
   1.115 +            int reportedStart = (int) sourcePositions.getStartPosition(cu, tree);
   1.116 +            System.err.println("Method " + name
   1.117 +                    + " expectedStart:" + expectedStart
   1.118 +                    + " reportedStart:" + reportedStart);
   1.119 +            if (expectedStart != reportedStart) {
   1.120 +                error("Unexpected value for " + name);
   1.121 +                show("expected", expectedStart);
   1.122 +                show("reported", reportedStart);
   1.123 +            }
   1.124 +            return super.visitMethod(tree, task);
   1.125 +        }
   1.126 +    }
   1.127 +}

mercurial