author | mcimadamore |
Wed, 26 Oct 2016 15:41:25 +0100 | |
changeset 41856 | 13a056e8f16e |
parent 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
2 |
* Copyright (c) 2013, 2015, 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 |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
28 |
* @modules jdk.compiler |
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
29 |
* @build TestGetElementReference |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
30 |
* @run main TestGetElementReference |
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 |
|
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.CompilationUnitTree; |
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.tree.Tree; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
35 |
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
|
36 |
import java.io.File; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
37 |
import java.io.IOException; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
38 |
import java.net.URI; |
36526 | 39 |
import java.util.ArrayList; |
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
40 |
import java.util.Arrays; |
36526 | 41 |
import java.util.List; |
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
import javax.tools.Diagnostic; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
46 |
import javax.tools.DiagnosticCollector; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
47 |
import javax.tools.JavaFileObject; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
48 |
import javax.tools.SimpleJavaFileObject; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
49 |
import javax.tools.StandardJavaFileManager; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
50 |
import javax.tools.ToolProvider; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
51 |
|
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
52 |
public class TestGetElementReference { |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
53 |
|
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
54 |
public static void main(String... args) throws IOException { |
36526 | 55 |
analyze("TestGetElementReferenceData.java"); |
56 |
analyze("mod/module-info.java", "mod/api/pkg/Api.java"); |
|
57 |
} |
|
58 |
||
59 |
private static void analyze(String... fileNames) throws IOException { |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
60 |
try (StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) { |
36526 | 61 |
List<JavaFileObject> files = new ArrayList<>(); |
62 |
for (String fileName : fileNames) { |
|
63 |
File source = new File(System.getProperty("test.src", "."), fileName.replace('/', File.separatorChar)).getAbsoluteFile(); |
|
64 |
for (JavaFileObject f : fm.getJavaFileObjects(source)) { |
|
65 |
files.add(f); |
|
66 |
} |
|
67 |
} |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
68 |
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); |
36526 | 69 |
JavacTask ct = (JavacTask) ToolProvider.getSystemJavaCompiler().getTask(null, null, diagnostics, Arrays.asList("-Xjcov"), null, files); |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
70 |
Trees trees = Trees.instance(ct); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
71 |
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
|
72 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
73 |
ct.analyze(); |
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
74 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
75 |
for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
76 |
if (d.getKind() == Diagnostic.Kind.ERROR) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
77 |
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
|
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 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
81 |
Pattern p = Pattern.compile("/\\*getElement:(.*?)\\*/"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
82 |
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
|
83 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
84 |
while (m.find()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
85 |
TreePath tp = pathFor(trees, cut, m.start() - 1); |
36526 | 86 |
String expected = m.group(1); |
87 |
if (expected.startsWith("getParentPath:")) { |
|
88 |
tp = tp.getParentPath(); |
|
89 |
expected = expected.substring("getParentPath:".length()); |
|
90 |
} |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
91 |
Element found = trees.getElement(tp); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
92 |
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
|
93 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
94 |
if (!expected.equals(actual)) { |
36526 | 95 |
throw new IllegalStateException("expected=" + expected + "; actual=" + actual + "; tree: " + tp.getLeaf()); |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
22444
diff
changeset
|
96 |
} |
17557
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 |
} |
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 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
|
102 |
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
|
103 |
|
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
104 |
new TreePathScanner<Void, Void>() { |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
105 |
@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
|
106 |
if ( node != null |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
107 |
&& 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
|
108 |
&& 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
|
109 |
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
|
110 |
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
|
111 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
112 |
return null; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
113 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
114 |
}.scan(cut, null); |
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 |
return result[0]; |
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 |
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
|
120 |
switch (el.getKind()) { |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
121 |
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
|
122 |
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
|
123 |
default: |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
124 |
return el.toString(); |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
125 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
126 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
127 |
|
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
128 |
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
|
129 |
private final String text; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
130 |
public TestFileObject(String text) { |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
131 |
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
|
132 |
this.text = text; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
133 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
134 |
@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
|
135 |
return text; |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
136 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
137 |
} |
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
138 |
|
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
diff
changeset
|
139 |
} |