aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8012929 aoqi@0: * @summary Trees.getElement should work not only for declaration trees, but also for use-trees aoqi@0: * @build TestGetElementReference aoqi@0: * @run main TestGetElementReference aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.tree.CompilationUnitTree; aoqi@0: import com.sun.source.tree.Tree; aoqi@0: import com.sun.source.util.*; aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.regex.Matcher; aoqi@0: import java.util.regex.Pattern; aoqi@0: import javax.lang.model.element.Element; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.DiagnosticCollector; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: public class TestGetElementReference { aoqi@0: aoqi@0: public static void main(String... args) throws IOException { aoqi@0: File source = new File(System.getProperty("test.src", "."), "TestGetElementReferenceData.java").getAbsoluteFile(); aoqi@0: StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null); aoqi@0: DiagnosticCollector diagnostics = new DiagnosticCollector<>(); aoqi@0: JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov", "-source", "1.8"), null, fm.getJavaFileObjects(source)); aoqi@0: Trees trees = Trees.instance(ct); aoqi@0: CompilationUnitTree cut = ct.parse().iterator().next(); aoqi@0: aoqi@0: ct.analyze(); aoqi@0: aoqi@0: for (Diagnostic d : diagnostics.getDiagnostics()) { aoqi@0: if (d.getKind() == Diagnostic.Kind.ERROR) { aoqi@0: throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/"); aoqi@0: Matcher m = p.matcher(cut.getSourceFile().getCharContent(false)); aoqi@0: aoqi@0: while (m.find()) { aoqi@0: TreePath tp = pathFor(trees, cut, m.start() - 1); aoqi@0: Element found = trees.getElement(tp); aoqi@0: String expected = m.group(1); aoqi@0: String actual = found != null ? found.getKind() + ":" + symbolToString(found) : ""; aoqi@0: aoqi@0: if (!expected.equals(actual)) { aoqi@0: throw new IllegalStateException("expected=" + expected + "; actual=" + actual); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static TreePath pathFor(final Trees trees, final CompilationUnitTree cut, final int pos) { aoqi@0: final TreePath[] result = new TreePath[1]; aoqi@0: aoqi@0: new TreePathScanner() { aoqi@0: @Override public Void scan(Tree node, Void p) { aoqi@0: if ( node != null aoqi@0: && trees.getSourcePositions().getStartPosition(cut, node) <= pos aoqi@0: && pos <= trees.getSourcePositions().getEndPosition(cut, node)) { aoqi@0: result[0] = new TreePath(getCurrentPath(), node); aoqi@0: return super.scan(node, p); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }.scan(cut, null); aoqi@0: aoqi@0: return result[0]; aoqi@0: } aoqi@0: aoqi@0: private static String symbolToString(Element el) { aoqi@0: switch (el.getKind()) { aoqi@0: case METHOD: return symbolToString(el.getEnclosingElement()) + "." + el.toString(); aoqi@0: case CONSTRUCTOR: return symbolToString(el.getEnclosingElement().getEnclosingElement()) + "." + el.toString(); aoqi@0: default: aoqi@0: return el.toString(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class TestFileObject extends SimpleJavaFileObject { aoqi@0: private final String text; aoqi@0: public TestFileObject(String text) { aoqi@0: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); aoqi@0: this.text = text; aoqi@0: } aoqi@0: @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return text; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }