test/tools/javac/api/TestGetElementReference.java

changeset 1734
8dd528992c15
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/TestGetElementReference.java	Fri May 10 15:15:50 2013 +0200
     1.3 @@ -0,0 +1,120 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, 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 8012929
    1.30 + * @summary Trees.getElement should work not only for declaration trees, but also for use-trees
    1.31 + * @build TestGetElementReference
    1.32 + * @run main TestGetElementReference
    1.33 + */
    1.34 +
    1.35 +import com.sun.source.tree.CompilationUnitTree;
    1.36 +import com.sun.source.tree.Tree;
    1.37 +import com.sun.source.util.*;
    1.38 +import java.io.File;
    1.39 +import java.io.IOException;
    1.40 +import java.net.URI;
    1.41 +import java.util.Arrays;
    1.42 +import java.util.regex.Matcher;
    1.43 +import java.util.regex.Pattern;
    1.44 +import javax.lang.model.element.Element;
    1.45 +import javax.tools.Diagnostic;
    1.46 +import javax.tools.DiagnosticCollector;
    1.47 +import javax.tools.JavaFileObject;
    1.48 +import javax.tools.SimpleJavaFileObject;
    1.49 +import javax.tools.StandardJavaFileManager;
    1.50 +import javax.tools.ToolProvider;
    1.51 +
    1.52 +public class TestGetElementReference {
    1.53 +
    1.54 +    public static void main(String... args) throws IOException {
    1.55 +        File source = new File(System.getProperty("test.src", "."), "TestGetElementReferenceData.java").getAbsoluteFile();
    1.56 +        StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
    1.57 +        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    1.58 +        JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov", "-source", "1.8"), null, fm.getJavaFileObjects(source));
    1.59 +        Trees trees = Trees.instance(ct);
    1.60 +        CompilationUnitTree cut = ct.parse().iterator().next();
    1.61 +
    1.62 +        ct.analyze();
    1.63 +
    1.64 +        for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
    1.65 +            if (d.getKind() == Diagnostic.Kind.ERROR) {
    1.66 +                throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
    1.67 +            }
    1.68 +        }
    1.69 +
    1.70 +        Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
    1.71 +        Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));
    1.72 +
    1.73 +        while (m.find()) {
    1.74 +            TreePath tp = pathFor(trees, cut, m.start() - 1);
    1.75 +            Element found = trees.getElement(tp);
    1.76 +            String expected = m.group(1);
    1.77 +            String actual = found != null ? found.getKind() + ":" + symbolToString(found) : "<null>";
    1.78 +
    1.79 +            if (!expected.equals(actual)) {
    1.80 +                throw new IllegalStateException("expected=" + expected + "; actual=" + actual);
    1.81 +            }
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    private static TreePath pathFor(final Trees trees, final CompilationUnitTree cut, final int pos) {
    1.86 +        final TreePath[] result = new TreePath[1];
    1.87 +
    1.88 +        new TreePathScanner<Void, Void>() {
    1.89 +            @Override public Void scan(Tree node, Void p) {
    1.90 +                if (   node != null
    1.91 +                    && trees.getSourcePositions().getStartPosition(cut, node) <= pos
    1.92 +                    && pos <= trees.getSourcePositions().getEndPosition(cut, node)) {
    1.93 +                    result[0] = new TreePath(getCurrentPath(), node);
    1.94 +                    return super.scan(node, p);
    1.95 +                }
    1.96 +                return null;
    1.97 +            }
    1.98 +        }.scan(cut, null);
    1.99 +
   1.100 +        return result[0];
   1.101 +    }
   1.102 +
   1.103 +    private static String symbolToString(Element el) {
   1.104 +        switch (el.getKind()) {
   1.105 +            case METHOD: return symbolToString(el.getEnclosingElement()) + "." + el.toString();
   1.106 +            case CONSTRUCTOR: return symbolToString(el.getEnclosingElement().getEnclosingElement()) + "." + el.toString();
   1.107 +            default:
   1.108 +                return el.toString();
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +    static class TestFileObject extends SimpleJavaFileObject {
   1.113 +        private final String text;
   1.114 +        public TestFileObject(String text) {
   1.115 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.116 +            this.text = text;
   1.117 +        }
   1.118 +        @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.119 +            return text;
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +}

mercurial