langtools/test/tools/javac/api/TestGetElementReference.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 22444 c79ee1e6742f
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
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
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
17557
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();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    53
        try (StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    54
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    55
            JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov"), null, fm.getJavaFileObjects(source));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    56
            Trees trees = Trees.instance(ct);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    57
            CompilationUnitTree cut = ct.parse().iterator().next();
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    58
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    59
            ct.analyze();
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    60
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    61
            for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    62
                if (d.getKind() == Diagnostic.Kind.ERROR) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    63
                    throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    64
                }
17557
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
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    67
            Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    68
            Matcher m = p.matcher(cut.getSourceFile().getCharContent(false));
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    69
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    70
            while (m.find()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    71
                TreePath tp = pathFor(trees, cut, m.start() - 1);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    72
                Element found = trees.getElement(tp);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    73
                String expected = m.group(1);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    74
                String actual = found != null ? found.getKind() + ":" + symbolToString(found) : "<null>";
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    75
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    76
                if (!expected.equals(actual)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    77
                    throw new IllegalStateException("expected=" + expected + "; actual=" + actual);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    78
                }
17557
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
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    83
    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
    84
        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
    85
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    86
        new TreePathScanner<Void, Void>() {
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    87
            @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
    88
                if (   node != null
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    89
                    && 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
    90
                    && 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
    91
                    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
    92
                    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
    93
                }
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    94
                return null;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    95
            }
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    96
        }.scan(cut, null);
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    97
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
    98
        return result[0];
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
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   101
    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
   102
        switch (el.getKind()) {
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   103
            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
   104
            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
   105
            default:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   106
                return el.toString();
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
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   110
    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
   111
        private final String text;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   112
        public TestFileObject(String text) {
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   113
            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
   114
            this.text = text;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   115
        }
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   116
        @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
   117
            return text;
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
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff changeset
   121
}