1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
package org.javacs;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;
import org.junit.Test;
public class JavaCompilerServiceTest {
private static final Logger LOG = Logger.getLogger("main");
static {
Main.setRootFormat();
}
private JavaCompilerService compiler =
new JavaCompilerService(
Collections.singleton(simpleProjectSrc()),
JavaCompilerServiceTest::allJavaFiles,
Collections.emptySet(),
Collections.emptySet());
static Path mavenProjectSrc() {
return Paths.get("src/test/test-project/workspace/src").normalize();
}
static Path simpleProjectSrc() {
return Paths.get("src/test/test-project/simple").normalize();
}
static Set<Path> allJavaFiles() {
try {
return Files.walk(simpleProjectSrc())
.filter(f -> f.getFileName().toString().endsWith(".java"))
.collect(Collectors.toSet());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static String contents(String resourceFile) {
var root = JavaCompilerServiceTest.simpleProjectSrc();
var file = root.resolve(resourceFile);
List<String> lines;
try {
lines = Files.readAllLines(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
var join = new StringJoiner("\n");
for (var l : lines) join.add(l);
return join.toString();
}
private URI resourceUri(String resourceFile) {
var root = JavaCompilerServiceTest.simpleProjectSrc();
var file = root.resolve(resourceFile);
return file.toUri();
}
@Test
public void element() {
var uri = resourceUri("HelloWorld.java");
var contents = contents("HelloWorld.java");
var found = compiler.compileFocus(uri, contents, 3, 24).element();
assertThat(found.getSimpleName(), hasToString(containsString("println")));
}
@Test
public void elementWithError() {
var uri = resourceUri("CompleteMembers.java");
var contents = contents("CompleteMembers.java");
var found = compiler.compileFocus(uri, contents, 3, 12).element();
assertThat(found, notNullValue());
}
private List<String> completionNames(List<Completion> found) {
var result = new ArrayList<String>();
for (var c : found) {
if (c.element != null) result.add(c.element.getSimpleName().toString());
else if (c.packagePart != null) result.add(c.packagePart.name);
else if (c.keyword != null) result.add(c.keyword);
else if (c.className != null) result.add(Parser.lastName(c.className.name));
else if (c.snippet != null) result.add(c.snippet.snippet);
}
return result;
}
private List<String> elementNames(List<Element> found) {
return found.stream().map(e -> e.getSimpleName().toString()).collect(Collectors.toList());
}
@Test
public void identifiers() {
var uri = resourceUri("CompleteIdentifiers.java");
var contents = contents("CompleteIdentifiers.java");
var focus = compiler.compileFocus(uri, contents, 13, 21);
var found = focus.scopeMembers("complete");
var names = elementNames(found);
assertThat(names, hasItem("completeLocal"));
assertThat(names, hasItem("completeParam"));
// assertThat(names, hasItem("super"));
// assertThat(names, hasItem("this"));
assertThat(names, hasItem("completeOtherMethod"));
assertThat(names, hasItem("completeInnerField"));
assertThat(names, hasItem("completeOuterField"));
assertThat(names, hasItem("completeOuterStatic"));
// assertThat(names, hasItem("CompleteIdentifiers"));
}
@Test
public void identifiersInMiddle() {
var uri = resourceUri("CompleteInMiddle.java");
var contents = contents("CompleteInMiddle.java");
var focus = compiler.compileFocus(uri, contents, 13, 21);
var found = focus.scopeMembers("complete");
var names = elementNames(found);
assertThat(names, hasItem("completeLocal"));
assertThat(names, hasItem("completeParam"));
// assertThat(names, hasItem("super"));
// assertThat(names, hasItem("this"));
assertThat(names, hasItem("completeOtherMethod"));
assertThat(names, hasItem("completeInnerField"));
assertThat(names, hasItem("completeOuterField"));
assertThat(names, hasItem("completeOuterStatic"));
// assertThat(names, hasItem("CompleteInMiddle"));
}
@Test
public void completeIdentifiers() {
var uri = resourceUri("CompleteIdentifiers.java");
var contents = contents("CompleteIdentifiers.java");
var ctx = compiler.parseFile(uri, contents).completionContext(13, 21).get();
var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character);
var found = focus.completeIdentifiers(ctx.inClass, ctx.inMethod, ctx.partialName);
var names = completionNames(found);
assertThat(names, hasItem("completeLocal"));
assertThat(names, hasItem("completeParam"));
// assertThat(names, hasItem("super"));
// assertThat(names, hasItem("this"));
assertThat(names, hasItem("completeOtherMethod"));
assertThat(names, hasItem("completeInnerField"));
assertThat(names, hasItem("completeOuterField"));
assertThat(names, hasItem("completeOuterStatic"));
// assertThat(names, hasItem("CompleteIdentifiers"));
}
@Test
public void members() {
var uri = resourceUri("CompleteMembers.java");
var contents = contents("CompleteMembers.java");
var focus = compiler.compileFocus(uri, contents, 3, 14);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("subMethod"));
assertThat(names, hasItem("superMethod"));
assertThat(names, hasItem("equals"));
}
@Test
public void completeMembers() {
var uri = resourceUri("CompleteMembers.java");
var contents = contents("CompleteMembers.java");
var ctx = compiler.parseFile(uri, contents).completionContext(3, 15).get();
var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("subMethod"));
assertThat(names, hasItem("superMethod"));
assertThat(names, hasItem("equals"));
}
@Test
public void completeExpression() {
var uri = resourceUri("CompleteExpression.java");
var contents = contents("CompleteExpression.java");
var ctx = compiler.parseFile(uri, contents).completionContext(3, 37).get();
var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("instanceMethod"));
assertThat(names, not(hasItem("create")));
assertThat(names, hasItem("equals"));
}
@Test
public void completeClass() {
var uri = resourceUri("CompleteClass.java");
var contents = contents("CompleteClass.java");
var ctx = compiler.parseFile(uri, contents).completionContext(3, 23).get();
var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItems("staticMethod", "staticField"));
assertThat(names, hasItems("class"));
assertThat(names, not(hasItem("instanceMethod")));
assertThat(names, not(hasItem("instanceField")));
}
@Test
public void completeImports() {
var uri = resourceUri("CompleteImports.java");
var contents = contents("CompleteImports.java");
var ctx = compiler.parseFile(uri, contents).completionContext(1, 18).get();
var focus = compiler.compileFocus(uri, contents, ctx.line, ctx.character);
var found = focus.completeMembers(false);
var names = completionNames(found);
assertThat(names, hasItem("List"));
assertThat(names, hasItem("concurrent"));
}
/*
@Test
public void gotoDefinition() {
var def =
compiler.definition("/GotoDefinition.java";
assertTrue(def.isPresent());
var t = def.get();
var unit = t.getCompilationUnit();
assertThat(unit.getSourceFile().getName(), endsWith("GotoDefinition.java"));
var trees = compiler.trees();
var pos = trees.getSourcePositions();
var lines = unit.getLineMap();
long start = pos.getStartPosition(unit, t.getLeaf());
long line = lines.getLineNumber(start);
assertThat(line, equalTo(6L));
}
*/
@Test
public void references() {
var file = "GotoDefinition.java";
var to = compiler.compileFocus(resourceUri(file), contents(file), 6, 13).element();
var possible = compiler.potentialReferences(to);
assertThat(
"GotoDefinition.java can have references to itself",
possible,
hasItem(hasToString(endsWith("/GotoDefinition.java"))));
var batch = compiler.compileBatch(possible);
var refs = batch.references(to);
var stringify = new ArrayList<String>();
for (var r : refs) {
var uri = r.getCompilationUnit().getSourceFile().toUri();
var fileName = Paths.get(uri).getFileName();
var range = batch.range(r).get();
stringify.add(String.format("%s:%d", fileName, range.start.line + 1));
}
assertThat(stringify, hasItem("GotoDefinition.java:3"));
assertThat(stringify, not(hasItem("GotoDefinition.java:6")));
}
@Test
public void overloads() {
var uri = resourceUri("Overloads.java");
var contents = contents("Overloads.java");
var found = compiler.compileFocus(uri, contents, 3, 15).methodInvocation().get();
var strings = found.overloads.stream().map(Object::toString).collect(Collectors.toList());
assertThat(strings, hasItem(containsString("print(int)")));
assertThat(strings, hasItem(containsString("print(java.lang.String)")));
}
@Test
public void lint() {
var uri = resourceUri("HasError.java");
var files = Collections.singleton(uri);
var diags = compiler.compileBatch(files).lint();
assertThat(diags, not(empty()));
}
@Test
public void localDoc() {
var uri = resourceUri("LocalMethodDoc.java");
var contents = contents("LocalMethodDoc.java");
var method = compiler.compileFocus(uri, contents, 3, 21).methodInvocation().get().activeMethod.get();
var ptr = new Ptr(method);
var file = compiler.docs().find(ptr).get();
var parse = compiler.docs().parse(file);
var path = parse.fuzzyFind(ptr).get();
var doc = parse.doc(path);
assertThat(doc.toString(), containsString("A great method"));
}
@Test
public void fixImports() {
var uri = resourceUri("MissingImport.java");
var contents = contents("MissingImport.java");
var qualifiedNames = compiler.compileFile(uri, contents).fixImports();
assertThat(qualifiedNames, hasItem("java.util.List"));
}
@Test
public void matchesPartialName() {
assertTrue(CompileFocus.matchesPartialName("foobar", "foo"));
assertFalse(CompileFocus.matchesPartialName("foo", "foobar"));
}
@Test
public void packageName() {
var compiler =
new JavaCompilerService(
Collections.singleton(mavenProjectSrc()),
JavaCompilerServiceTest::allJavaFiles,
Collections.emptySet(),
Collections.emptySet());
assertThat(
compiler.pathBasedPackageName(FindResource.path("/org/javacs/example/Goto.java")),
equalTo("org.javacs.example"));
}
}
|