test/tools/javac/api/TestGetElementReference.java

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 0
959103a6100f
permissions
-rw-r--r--

Merge

     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  */
    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  */
    32 import com.sun.source.tree.CompilationUnitTree;
    33 import com.sun.source.tree.Tree;
    34 import com.sun.source.util.*;
    35 import java.io.File;
    36 import java.io.IOException;
    37 import java.net.URI;
    38 import java.util.Arrays;
    39 import java.util.regex.Matcher;
    40 import java.util.regex.Pattern;
    41 import javax.lang.model.element.Element;
    42 import javax.tools.Diagnostic;
    43 import javax.tools.DiagnosticCollector;
    44 import javax.tools.JavaFileObject;
    45 import javax.tools.SimpleJavaFileObject;
    46 import javax.tools.StandardJavaFileManager;
    47 import javax.tools.ToolProvider;
    49 public class TestGetElementReference {
    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();
    59         ct.analyze();
    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         }
    67         Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
    68         Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));
    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>";
    76             if (!expected.equals(actual)) {
    77                 throw new IllegalStateException("expected=" + expected + "; actual=" + actual);
    78             }
    79         }
    80     }
    82     private static TreePath pathFor(final Trees trees, final CompilationUnitTree cut, final int pos) {
    83         final TreePath[] result = new TreePath[1];
    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);
    97         return result[0];
    98     }
   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     }
   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     }
   120 }

mercurial