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