11package org .javacs ;
22
3- import com .sun .source .util .JavacTask ;
4- import com .sun .source .util .TaskEvent ;
5- import com .sun .source .util .TaskListener ;
3+ import static org .hamcrest .Matchers .*;
4+ import static org .junit .Assert .*;
5+
6+ import com .sun .source .tree .*;
7+ import com .sun .source .util .*;
8+ import com .sun .tools .javac .TaskPoolProvider ;
69import java .io .IOException ;
710import java .nio .charset .Charset ;
811import java .nio .file .Path ;
1518
1619public class SimpleTest implements TaskListener , DiagnosticListener <JavaFileObject > {
1720 final JavaCompiler compiler = ServiceLoader .load (JavaCompiler .class ).iterator ().next ();
18- final Path src = Paths .get ("src/main/java" ).toAbsolutePath ();
21+ final Path src = Paths .get ("dummy/src" ).toAbsolutePath ();
22+ final Path foo = src .resolve ("foo/bar/Foo.java" );
1923 final List <String > options = List .of ("-sourcepath" , src .toString (), "-verbose" , "-proc:none" );
2024
2125 @ Before
@@ -24,42 +28,56 @@ public void setLogFormat() {
2428 }
2529
2630 @ Test
27- public void standardFileManager () throws IOException {
31+ public void freshTask () throws IOException {
2832 var fileManager = compiler .getStandardFileManager (this , null , Charset .defaultCharset ());
29-
30- LOG .info ("Compile once..." );
31- var files = fileManager .getJavaFileObjects (src .resolve ("org/javacs/JavaLanguageServer.java" ));
32- var task = (JavacTask ) compiler .getTask (null , fileManager , this , options , null , files );
33-
34- task .addTaskListener (this );
35- task .analyze ();
36-
37- LOG .info ("Compile twice..." );
38- task = (JavacTask ) compiler .getTask (null , fileManager , this , options , null , files );
39- task .addTaskListener (this );
40- task .analyze ();
33+ for (var i = 0 ; i < 2 ; i ++) {
34+ LOG .info (String .format ("Compile %d..." , i ));
35+ var files = fileManager .getJavaFileObjects (foo );
36+ var task = (JavacTask ) compiler .getTask (null , fileManager , this , options , null , files );
37+ checkInvokeType (task );
38+ }
4139 }
4240
4341 @ Test
44- public void sourceFileManager () throws IOException {
45- FileStore .setWorkspaceRoots (Set .of (Paths .get ("." ).toAbsolutePath ()));
46- var fileManager = new SourceFileManager ();
42+ public void taskPool () throws IOException {
43+ var fileManager = compiler .getStandardFileManager (this , null , Charset .defaultCharset ());
44+ var pool = TaskPoolProvider .createTaskPool (1 );
45+ for (var i = 0 ; i < 2 ; i ++) {
46+ var files = fileManager .getJavaFileObjects (foo );
47+ LOG .info (String .format ("Compile %d..." , i ));
48+ pool .getTask (
49+ null ,
50+ fileManager ,
51+ this ,
52+ options ,
53+ null ,
54+ files ,
55+ task -> {
56+ checkInvokeType (task );
57+ return "Done!" ;
58+ });
59+ }
60+ }
4761
48- LOG .info ("Compile once..." );
49- var files =
50- fileManager .getJavaFileObjectsFromFiles (
51- List .of (src .resolve ("org/javacs/JavaLanguageServer.java" ).toFile ()));
52- var task = (JavacTask ) compiler .getTask (null , fileManager , this , options , null , files );
62+ private void checkInvokeType (JavacTask task ) {
5363 task .addTaskListener (this );
54- task .analyze ();
55- LOG .info ("...finished once" );
56-
57- for (var i = 0 ; i < 3 ; i ++) {
58- LOG .info (String .format ("Compile again %d..." , i ));
59- task = (JavacTask ) compiler .getTask (null , fileManager , this , options , null , files );
60- task .addTaskListener (this );
64+ try {
65+ var root = task .parse ().iterator ().next ();
6166 task .analyze ();
62- LOG .info (String .format ("...finished %d" , i ));
67+ LOG .info ("Scan " + root );
68+ new TreePathScanner <Void , Void >() {
69+ @ Override
70+ public Void visitMethodInvocation (MethodInvocationTree t , Void __ ) {
71+ if (t .getMethodSelect ().toString ().equals ("Bar.test" )) {
72+ var type = Trees .instance (task ).getTypeMirror (getCurrentPath ());
73+ LOG .info ("Check " + t + ": " + type );
74+ assertThat (type .toString (), equalTo ("int" ));
75+ }
76+ return null ;
77+ }
78+ }.scan (root , null );
79+ } catch (IOException e ) {
80+ throw new RuntimeException (e );
6381 }
6482 }
6583
0 commit comments