test/tools/javac/api/TestGetElementReference.java

Fri, 10 May 2013 15:15:50 +0200

author
jlahoda
date
Fri, 10 May 2013 15:15:50 +0200
changeset 1734
8dd528992c15
parent 0
959103a6100f
permissions
-rw-r--r--

8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
Reviewed-by: jjg
Contributed-by: Dusan Balek <dbalek@netbeans.org>, Jan Lahoda <jlahoda@netbeans.org>

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8012929
aoqi@0 27 * @summary Trees.getElement should work not only for declaration trees, but also for use-trees
aoqi@0 28 * @build TestGetElementReference
aoqi@0 29 * @run main TestGetElementReference
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import com.sun.source.tree.CompilationUnitTree;
aoqi@0 33 import com.sun.source.tree.Tree;
aoqi@0 34 import com.sun.source.util.*;
aoqi@0 35 import java.io.File;
aoqi@0 36 import java.io.IOException;
aoqi@0 37 import java.net.URI;
aoqi@0 38 import java.util.Arrays;
aoqi@0 39 import java.util.regex.Matcher;
aoqi@0 40 import java.util.regex.Pattern;
aoqi@0 41 import javax.lang.model.element.Element;
aoqi@0 42 import javax.tools.Diagnostic;
aoqi@0 43 import javax.tools.DiagnosticCollector;
aoqi@0 44 import javax.tools.JavaFileObject;
aoqi@0 45 import javax.tools.SimpleJavaFileObject;
aoqi@0 46 import javax.tools.StandardJavaFileManager;
aoqi@0 47 import javax.tools.ToolProvider;
aoqi@0 48
aoqi@0 49 public class TestGetElementReference {
aoqi@0 50
aoqi@0 51 public static void main(String... args) throws IOException {
aoqi@0 52 File source = new File(System.getProperty("test.src", "."), "TestGetElementReferenceData.java").getAbsoluteFile();
aoqi@0 53 StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
aoqi@0 54 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
aoqi@0 55 JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov", "-source", "1.8"), null, fm.getJavaFileObjects(source));
aoqi@0 56 Trees trees = Trees.instance(ct);
aoqi@0 57 CompilationUnitTree cut = ct.parse().iterator().next();
aoqi@0 58
aoqi@0 59 ct.analyze();
aoqi@0 60
aoqi@0 61 for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
aoqi@0 62 if (d.getKind() == Diagnostic.Kind.ERROR) {
aoqi@0 63 throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
aoqi@0 64 }
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
aoqi@0 68 Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));
aoqi@0 69
aoqi@0 70 while (m.find()) {
aoqi@0 71 TreePath tp = pathFor(trees, cut, m.start() - 1);
aoqi@0 72 Element found = trees.getElement(tp);
aoqi@0 73 String expected = m.group(1);
aoqi@0 74 String actual = found != null ? found.getKind() + ":" + symbolToString(found) : "<null>";
aoqi@0 75
aoqi@0 76 if (!expected.equals(actual)) {
aoqi@0 77 throw new IllegalStateException("expected=" + expected + "; actual=" + actual);
aoqi@0 78 }
aoqi@0 79 }
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 private static TreePath pathFor(final Trees trees, final CompilationUnitTree cut, final int pos) {
aoqi@0 83 final TreePath[] result = new TreePath[1];
aoqi@0 84
aoqi@0 85 new TreePathScanner<Void, Void>() {
aoqi@0 86 @Override public Void scan(Tree node, Void p) {
aoqi@0 87 if ( node != null
aoqi@0 88 && trees.getSourcePositions().getStartPosition(cut, node) <= pos
aoqi@0 89 && pos <= trees.getSourcePositions().getEndPosition(cut, node)) {
aoqi@0 90 result[0] = new TreePath(getCurrentPath(), node);
aoqi@0 91 return super.scan(node, p);
aoqi@0 92 }
aoqi@0 93 return null;
aoqi@0 94 }
aoqi@0 95 }.scan(cut, null);
aoqi@0 96
aoqi@0 97 return result[0];
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 private static String symbolToString(Element el) {
aoqi@0 101 switch (el.getKind()) {
aoqi@0 102 case METHOD: return symbolToString(el.getEnclosingElement()) + "." + el.toString();
aoqi@0 103 case CONSTRUCTOR: return symbolToString(el.getEnclosingElement().getEnclosingElement()) + "." + el.toString();
aoqi@0 104 default:
aoqi@0 105 return el.toString();
aoqi@0 106 }
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 static class TestFileObject extends SimpleJavaFileObject {
aoqi@0 110 private final String text;
aoqi@0 111 public TestFileObject(String text) {
aoqi@0 112 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 113 this.text = text;
aoqi@0 114 }
aoqi@0 115 @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 116 return text;
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 }

mercurial