TestGetElementReference.java revision 1733:8dd528992c15
1/*
2 * Copyright (c) 2013, 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 8012929
27 * @summary Trees.getElement should work not only for declaration trees, but also for use-trees
28 * @build TestGetElementReference
29 * @run main TestGetElementReference
30 */
31
32import com.sun.source.tree.CompilationUnitTree;
33import com.sun.source.tree.Tree;
34import com.sun.source.util.*;
35import java.io.File;
36import java.io.IOException;
37import java.net.URI;
38import java.util.Arrays;
39import java.util.regex.Matcher;
40import java.util.regex.Pattern;
41import javax.lang.model.element.Element;
42import javax.tools.Diagnostic;
43import javax.tools.DiagnosticCollector;
44import javax.tools.JavaFileObject;
45import javax.tools.SimpleJavaFileObject;
46import javax.tools.StandardJavaFileManager;
47import javax.tools.ToolProvider;
48
49public class TestGetElementReference {
50
51    public static void main(String... args) throws IOException {
52        File source = new File(System.getProperty("test.src", "."), "TestGetElementReferenceData.java").getAbsoluteFile();
53        StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
54        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
55        JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov", "-source", "1.8"), null, fm.getJavaFileObjects(source));
56        Trees trees = Trees.instance(ct);
57        CompilationUnitTree cut = ct.parse().iterator().next();
58
59        ct.analyze();
60
61        for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
62            if (d.getKind() == Diagnostic.Kind.ERROR) {
63                throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
64            }
65        }
66
67        Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
68        Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));
69
70        while (m.find()) {
71            TreePath tp = pathFor(trees, cut, m.start() - 1);
72            Element found = trees.getElement(tp);
73            String expected = m.group(1);
74            String actual = found != null ? found.getKind() + ":" + symbolToString(found) : "<null>";
75
76            if (!expected.equals(actual)) {
77                throw new IllegalStateException("expected=" + expected + "; actual=" + actual);
78            }
79        }
80    }
81
82    private static TreePath pathFor(final Trees trees, final CompilationUnitTree cut, final int pos) {
83        final TreePath[] result = new TreePath[1];
84
85        new TreePathScanner<Void, Void>() {
86            @Override public Void scan(Tree node, Void p) {
87                if (   node != null
88                    && trees.getSourcePositions().getStartPosition(cut, node) <= pos
89                    && pos <= trees.getSourcePositions().getEndPosition(cut, node)) {
90                    result[0] = new TreePath(getCurrentPath(), node);
91                    return super.scan(node, p);
92                }
93                return null;
94            }
95        }.scan(cut, null);
96
97        return result[0];
98    }
99
100    private static String symbolToString(Element el) {
101        switch (el.getKind()) {
102            case METHOD: return symbolToString(el.getEnclosingElement()) + "." + el.toString();
103            case CONSTRUCTOR: return symbolToString(el.getEnclosingElement().getEnclosingElement()) + "." + el.toString();
104            default:
105                return el.toString();
106        }
107    }
108
109    static class TestFileObject extends SimpleJavaFileObject {
110        private final String text;
111        public TestFileObject(String text) {
112            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
113            this.text = text;
114        }
115        @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) {
116            return text;
117        }
118    }
119
120}
121