/
BasicGroovyBuildTests.java
3623 lines (3175 loc) · 142 KB
/
BasicGroovyBuildTests.java
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2000, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.groovy.tests.builder;
import java.io.File;
import java.net.URL;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;
import junit.framework.Test;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.GenericsType;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.stmt.ReturnStatement;
import org.codehaus.groovy.vmplugin.VMPluginFactory;
import org.codehaus.groovy.vmplugin.v5.Java5;
import org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode;
import org.codehaus.jdt.groovy.internal.compiler.ast.JDTResolver;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.codehaus.jdt.groovy.model.ModuleNodeMapper.ModuleNodeInfo;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.search.TypeNameMatch;
import org.eclipse.jdt.core.tests.builder.Problem;
import org.eclipse.jdt.core.tests.util.GroovyUtils;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.core.util.CompilerUtils;
import org.eclipse.jdt.groovy.search.VariableScope;
import org.eclipse.jdt.internal.core.builder.AbstractImageBuilder;
/**
* Basic tests for the builder - compiling and running some very simple java and
* groovy code
*/
public class BasicGroovyBuildTests extends GroovierBuilderTests {
public BasicGroovyBuildTests(String name) {
super(name);
}
public static Test suite() {
return buildTestSuite(BasicGroovyBuildTests.class);
}
/**
* Testing that the classpath computation works for multi dependent
* projects. This classpath will be used for the ast transform loader.
*/
public void testMultiProjectDependenciesAndAstTransformClasspath()
throws JavaModelException {
// Construct ProjectA
IPath projectAPath = env.addProject("ProjectA"); //$NON-NLS-1$
env.addExternalJars(projectAPath, Util.getJavaClassLibs());
fullBuild(projectAPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectAPath, ""); //$NON-NLS-1$
IPath rootA = env.addPackageFragmentRoot(projectAPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectAPath, "bin"); //$NON-NLS-1$
env.addClass(rootA, "p1", "Hello", "package p1;\n"
+ "public class Hello {\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(\"Hello world\");\n" + " }\n"
+ "}\n");
// Construct ProjectB
IPath projectBPath = env.addProject("ProjectB"); //$NON-NLS-1$
env.addExternalJars(projectBPath, Util.getJavaClassLibs());
fullBuild(projectBPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectBPath, ""); //$NON-NLS-1$
IPath rootB = env.addPackageFragmentRoot(projectBPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectBPath, "bin"); //$NON-NLS-1$
env.addClass(rootB, "p1", "Hello", "package p1;\n"
+ "public class Hello {\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(\"Hello world\");\n" + " }\n"
+ "}\n");
env.addRequiredProject(projectBPath, projectAPath, new IPath[] {}/*
* include
* all
*/,
new IPath[] {}/* exclude none */, true);
// Construct ProjectC
IPath projectCPath = env.addProject("ProjectC"); //$NON-NLS-1$
env.addExternalJars(projectCPath, Util.getJavaClassLibs());
fullBuild(projectCPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectCPath, ""); //$NON-NLS-1$
IPath rootC = env.addPackageFragmentRoot(projectCPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectCPath, "bin"); //$NON-NLS-1$
env.addClass(rootC, "p1", "Hello", "package p1;\n"
+ "public class Hello {\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(\"Hello world\");\n" + " }\n"
+ "}\n");
env.addRequiredProject(projectCPath, projectBPath, new IPath[] {}/*
* include
* all
*/,
new IPath[] {}/* exclude none */, true);
// Construct ProjectD
IPath projectDPath = env.addProject("ProjectD"); //$NON-NLS-1$
env.addExternalJars(projectDPath, Util.getJavaClassLibs());
fullBuild(projectDPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectDPath, ""); //$NON-NLS-1$
IPath rootD = env.addPackageFragmentRoot(projectDPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectDPath, "bin"); //$NON-NLS-1$
env.addClass(rootD, "p1", "Hello", "package p1;\n"
+ "public class Hello {\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(\"Hello world\");\n" + " }\n"
+ "}\n");
env.addRequiredProject(projectDPath, projectCPath, new IPath[] {}/*
* include
* all
*/,
new IPath[] {}/* exclude none */, true);
// incrementalBuild(projectAPath);
// expectingCompiledClassesV("p1.Hello");
// expectingNoProblems();
// executeClass(projectAPath, "p1.Hello", "Hello world", "");
incrementalBuild(projectDPath);
expectingCompiledClassesV("p1.Hello");
expectingNoProblems();
executeClass(projectDPath, "p1.Hello", "Hello world", "");
String classpathForProjectD = CompilerUtils.calculateClasspath(env
.getJavaProject(projectDPath));
StringTokenizer st = new StringTokenizer(classpathForProjectD,
File.pathSeparator);
// look at how project A and B manifest in this classpath
boolean foundAndCheckedA = false;
boolean foundAndCheckedB = false;
boolean foundAndCheckedC = false;
while (st.hasMoreElements()) {
String pathElement = st.nextToken();
// ProjectA is on ProjectDs classpath indirectly. It is on ProjectBs
// classpath and re-exported
if (pathElement.indexOf("ProjectA") != -1) {
// System.out.println("ProjectA element is ["+pathElement+"]");
if (pathElement.indexOf("ProjectA") == 1) {
fail("Path element looks incorrect. Path for ProjectA should be an absolute location, not ["
+ pathElement + "]");
}
if (!pathElement.endsWith("bin")) {
fail("Expected pathelement to end with the output folder 'bin', but it did not: ["
+ pathElement + "]");
}
foundAndCheckedA = true;
}
// ProjectB is on ProjectDs classpath indirectly. It is on ProjectCs
// classpath and re-exported
if (pathElement.indexOf("ProjectB") != -1) {
System.out.println("ProjectB element is [" + pathElement + "]");
if (pathElement.indexOf("ProjectB") == 1) {
fail("Path element looks incorrect. Path for ProjectB should be an absolute location, not ["
+ pathElement + "]");
}
if (!pathElement.endsWith("bin")) {
fail("Expected pathelement to end with the output folder 'bin', but it did not: ["
+ pathElement + "]");
}
foundAndCheckedB = true;
}
// ProjectB is directly on ProjectCs classpath
if (pathElement.indexOf("ProjectC") != -1) {
System.out.println("ProjectC element is [" + pathElement + "]");
if (pathElement.indexOf("ProjectC") == 1) {
fail("Path element looks incorrect. Path for ProjectC should be an absolute location, not ["
+ pathElement + "]");
}
if (!pathElement.endsWith("bin")) {
fail("Expected pathelement to end with the output folder 'bin', but it did not: ["
+ pathElement + "]");
}
foundAndCheckedC = true;
}
}
if (!foundAndCheckedC) {
fail("Unable to check entry for ProjectC in the classpath, didn't find it:\n"
+ classpathForProjectD);
}
if (!foundAndCheckedB) {
fail("Unable to check entry for ProjectB in the classpath, didn't find it:\n"
+ classpathForProjectD);
}
if (!foundAndCheckedA) {
fail("Unable to check entry for ProjectA in the classpath, didn't find it:\n"
+ classpathForProjectD);
}
// System.out.println(">>"+classpathForProjectD);
}
// build hello world and run it
public void testBuildJavaHelloWorld() throws JavaModelException {
IPath projectPath = env.addProject("Project"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
env.addClass(root, "p1", "Hello", "package p1;\n"
+ "public class Hello {\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(\"Hello world\");\n" + " }\n"
+ "}\n");
incrementalBuild(projectPath);
expectingCompiledClassesV("p1.Hello");
expectingNoProblems();
executeClass(projectPath, "p1.Hello", "Hello world", "");
}
public void testNPEAnno_1398() throws Exception {
IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$ //$NON-NLS-2$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
env.addClass(root, "", "Anno", "public @interface Anno {\n"
+ " String[] value();\n" + "}");
env.addGroovyClass(root, "", "Const", "public class Const {\n"
+ " private final static String instance= \"abc\";\n"
+ " }");
env.addGroovyClass(root, "", "A", "@Anno(Const.instance)\n"
+ "class A {}\n");
incrementalBuild(projectPath);
expectingCompiledClassesV("Anno", "Const", "A");
expectingNoProblems();
IPath pathToSecond = env.addGroovyClass(root, "", "A",
"@Anno(Const.instance)\n" + "class A {}\n");
incrementalBuild(projectPath);
if (GroovyUtils.GROOVY_LEVEL < 18) {
Problem[] probs = env.getProblemsFor(pathToSecond);
boolean p1found = false;
boolean p2found = false;
for (int i = 0; i < probs.length; i++) {
if (probs[i]
.getMessage()
.equals("Groovy:expected 'Const.instance' to be an inline constant of type java.lang.String not a property expression in @Anno")) {
p1found = true;
}
if (probs[i]
.getMessage()
.startsWith(
"Groovy:Attribute 'value' should have type 'java.lang.String'; but found type 'java.lang.Object' in @Anno")) {
p2found = true;
}
}
if (!p1found) {
printProblemsFor(pathToSecond);
fail("Didn't get expected message 'Groovy:expected 'Const.instance' to be an inline constant of type java.lang.String not a property expression in @Anno'\n");
}
if (!p2found) {
printProblemsFor(pathToSecond);
fail("Didn't get expected message 'Groovy:Attribute 'value' should have type 'java.lang.String'; but found type 'java.lang.Object' in @Anno'\n");
}
} else {
expectingNoProblems();
expectingCompiledClassesV("A");
}
}
// bug not yet fixed
// public void testGreclipse1506() throws Exception {
// if (GroovyUtils.GROOVY_LEVEL < 20) {
// return;
// }
// IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
// env.addExternalJars(projectPath, Util.getJavaClassLibs());
// env.addGroovyJars(projectPath);
//// URL jar = Platform.getBundle("org.eclipse.jdt.groovy.core.tests.compiler").getEntry("lib/slf4j-api-1.6.6.jar");
//// env.addExternalJar(projectPath, FileLocator.resolve(jar).getFile());
// fullBuild(projectPath);
// // remove old package fragment root so that names don't collide
// env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
//
// IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
// env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
//
// JDTResolver.recordInstances = true;
//
// env.addGroovyClass(root, "", "LoggerTest",
// "import groovy.transform.TypeChecked;\n"+
// "import groovy.util.logging.Slf4j;\n"+
// "\n"+
// "@Slf4j\n"+
// "@TypeChecked\n"+
// "public class LoggerTest\n"+
// "{\n"+
// " public static void main(String... args)\n"+
// " {\n"+
// " println 'println'\n"+
// // " LoggerTest.log.info('Logged');\n"+
// " log.info('foo')\n"+
// " }\n"+
// "}\n" );
//
// incrementalBuild(projectPath);
// // expectingCompiledClassesV("Foo","List2");
// expectingNoProblems();
// }
// if (GroovyUtils.GROOVY_LEVEL<20) {
// return;
// }
// runNegativeTest(new String[]{
// "Foo.groovy",
// "import groovy.transform.CompileStatic\n"+
// "@CompileStatic\n"+
// "void method(String message) {\n"+
// " List<Integer> ls = new ArrayList<Integer>();\n"+
// " ls.add(123);\n"+
// " ls.add('abc');\n"+
// "}"
// },
// "----------\n" +
// "1. ERROR in Foo.groovy (at line 6)\n" +
// " ls.add(\'abc\');\n" +
// " ^"+(isGE20()?"^^^^^^^^^^^^":"")+"\n" +
// "Groovy:[Static type checking] - Cannot find matching method java.util.ArrayList#add(java.lang.String)"+(isGE20()?". Please check if the declared type is right and if the method exists.":"")+"\n" +
// "----------\n");
public void testCompileStatic_1505() throws Exception {
try {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
JDTResolver.recordInstances = true;
env.addGroovyClass(root, "", "Foo",
"import groovy.transform.CompileStatic\n"+
"@CompileStatic\n"+
"void method(String message) {\n"+
" Collection<Integer> cs;\n"+
// " List<Integer> ls = new ArrayList<Integer>();\n"+
// " ls.add(123);\n"+
// " ls.add('abc');\n"+
// GRECLIPSE-1511 code
" List<String> second = []\n"+
" List<String> artefactResources2\n"+
" second.addAll(artefactResources2)\n"+
"}\n"
// "interface List2<E> extends Collection<E> {\n"+
// " boolean add(E e);\n" +
// "}"
);
incrementalBuild(projectPath);
// expectingCompiledClassesV("Foo","List2");
expectingNoProblems();
// Now compare the generics structure for List (built by jdtresolver mapping into groovy) against List2 (built by groovy)
// Access the jdtresolver bits and pieces
JDTClassNode jcn = JDTResolver.getCachedNode("java.util.Collection<E>");
assertNotNull(jcn);
System.out.println("JDT ClassNode="+jcn);
// JDTClassNode jcn2 = jdtr.getCachedNode("List2");
// System.out.println(jcn2);
ClassNode listcn = new ClassNode(java.util.Collection.class);
VMPluginFactory.getPlugin().setAdditionalClassInformation(listcn);
listcn.lazyClassInit();
System.out.println("Groovy ClassNode="+listcn);
// IJavaProject ijp = env.getJavaProject("Project");
// GroovyCompilationUnit unit = (GroovyCompilationUnit) ijp.findType("Foo")
// .getCompilationUnit();
// now find the class reference
// ClassNode cn = unit.getModuleNode().getClasses().get(1);
// System.out.println(cn);
// Compare java.util.List from JDTClassNode and List2 from groovy
compareClassNodes(jcn.redirect(),listcn.redirect(),0);
MethodNode jmn = getMethodNode(jcn,"add",1); // boolean add(E)
MethodNode rmn = getMethodNode(listcn,"add",1);
compareMethodNodes(jmn,rmn);
jmn = getMethodNode(jcn,"addAll",1);
rmn = getMethodNode(listcn,"addAll",1);
compareMethodNodes(jmn,rmn);
// Want to compare type information in the
// env.addClass(root, "", "Client", "public class Client {\n"
// + " { new Outer.Inner(); }\n" + "}\n");
// incrementalBuild(projectPath);
// expectingNoProblems();
// expectingCompiledClassesV("Client");
} finally {
JDTResolver.recordInstances = false;
}
}
// public void testCompileStatic_1506() throws Exception {
// try {
// if (GroovyUtils.GROOVY_LEVEL < 20) {
// return;
// }
// IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
// env.addExternalJars(projectPath, Util.getJavaClassLibs());
// env.addGroovyJars(projectPath);
// fullBuild(projectPath);
// // remove old package fragment root so that names don't collide
// env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
//
// IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
// env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
//
// JDTResolver.recordInstances = true;
//
// env.addGroovyClass(root, "", "Foo",
// "import groovy.transform.CompileStatic\n"+
// "@CompileStatic\n"+
// "void method(String message) {\n"+
// " Collection<Integer> cs;\n"+
//// " List<Integer> ls = new ArrayList<Integer>();\n"+
//// " ls.add(123);\n"+
//// " ls.add('abc');\n"+
// // GRECLIPSE-1511 code
// " List<String> second = []\n"+
// " List<String> artefactResources2\n"+
// " second.addAll(artefactResources2)\n"+
// "}\n"+
// "interface ListOfFile extends ArrayList<File> {\n"+
// "}"
// );
//
// incrementalBuild(projectPath);
//// expectingCompiledClassesV("Foo","List2");
// expectingNoProblems();
//
// // Now compare the generics structure for List (built by jdtresolver mapping into groovy) against List2 (built by groovy)
//
// // Access the jdtresolver bits and pieces
//
// JDTClassNode jcn = JDTResolver.getCachedNode("ListOfFile");
//
// assertNotNull(jcn);
// System.out.println("JDT ClassNode="+jcn);
//// JDTClassNode jcn2 = jdtr.getCachedNode("List2");
//// System.out.println(jcn2);
//
//// List<File> C = new ArrayList<File>();
// ClassNode listcn = new ClassNode(java.util.Collection.class);
// VMPluginFactory.getPlugin().setAdditionalClassInformation(listcn);
// listcn.lazyClassInit();
// System.out.println("Groovy ClassNode="+listcn);
//
//// IJavaProject ijp = env.getJavaProject("Project");
//// GroovyCompilationUnit unit = (GroovyCompilationUnit) ijp.findType("Foo")
//// .getCompilationUnit();
//
// // now find the class reference
//// ClassNode cn = unit.getModuleNode().getClasses().get(1);
//// System.out.println(cn);
//
// // Compare java.util.List from JDTClassNode and List2 from groovy
// compareClassNodes(jcn.redirect(),listcn.redirect(),0);
// MethodNode jmn = getMethodNode(jcn,"add",1); // boolean add(E)
// MethodNode rmn = getMethodNode(listcn,"add",1);
// compareMethodNodes(jmn,rmn);
//
// jmn = getMethodNode(jcn,"addAll",1);
// rmn = getMethodNode(listcn,"addAll",1);
// compareMethodNodes(jmn,rmn);
//
// // Want to compare type information in the
// // env.addClass(root, "", "Client", "public class Client {\n"
// // + " { new Outer.Inner(); }\n" + "}\n");
// // incrementalBuild(projectPath);
// // expectingNoProblems();
// // expectingCompiledClassesV("Client");
// } finally {
// JDTResolver.recordInstances = false;
// }
// }
public void testCompileStatic_ArrayArray() throws Exception {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
JDTResolver.recordInstances = true;
env.addClass(root, "", "ISourceRange", "class ISourceRange {}");
env.addClass(root, "", "TypeNameMatch", "class TypeNameMatch {}");
env.addClass(root, "", "IChooseImportQuery",
"interface IChooseImportQuery {\n"+
" TypeNameMatch[] chooseImports(TypeNameMatch[][] matches, ISourceRange[] range);\n"+
"}"
);
env.addGroovyClass(root, "", "NoChoiceQuery",
"class NoChoiceQuery implements IChooseImportQuery {\n"+
" public TypeNameMatch[] chooseImports(TypeNameMatch[][] matches, ISourceRange[] range) {\n"+
" throw new Exception(\"Should not have a choice, but found $matches[0][0] and $matches[0][1]\")\n"+
" return []\n"+
" }\n"+
"}"
);
incrementalBuild(projectPath);
expectingNoProblems();
expectingCompiledClassesV("IChooseImportQuery","ISourceRange","NoChoiceQuery","TypeNameMatch");
}
public void testCompileStatic_FileAddAll() throws Exception {
try {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
JDTResolver.recordInstances = true;
env.addGroovyClass(root, "", "Foo",
"import groovy.transform.CompileStatic\n"+
"@CompileStatic\n"+
"class Foo {\n"+
"List<String> jvmArgs = new ArrayList<String>();\n"+
" void method(String message) {\n"+
" List<String> cmd = ['java'];\n"+
" cmd.addAll(jvmArgs);\n"+
" }\n"+
"}\n"
);
incrementalBuild(projectPath);
expectingNoProblems();
expectingCompiledClassesV("Foo");
// Now compare the generics structure for List (built by jdtresolver mapping into groovy) against List2 (built by groovy)
} finally {
JDTResolver.recordInstances = false;
}
}
public void testCompileStatic_ListFileArgIteratedOver() throws Exception {
try {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
JDTResolver.recordInstances = true;
env.addGroovyClass(root, "", "Foo",
"import groovy.transform.CompileStatic\n"+
"class Foo {\n"+
"@CompileStatic\n"+
"private populateSourceDirectories() {\n"+
" List<File> pluginDependencies\n"+
" for (zip in pluginDependencies) {\n"+
" registerPluginZipWithScope(zip);\n"+
" }\n"+
"}\n"+
"private void registerPluginZipWithScope(File pluginzip) {}\n"+
"}\n"
);
incrementalBuild(projectPath);
expectingNoProblems();
expectingCompiledClassesV("Foo");
// Now compare the generics structure for List (built by jdtresolver mapping into groovy) against List2 (built by groovy)
} finally {
JDTResolver.recordInstances = false;
}
}
public void testCompileStatic_IterableParameter() throws Exception {
try {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
JDTResolver.recordInstances = true;
env.addGroovyClass(root, "", "Foo",
"import groovy.transform.CompileStatic\n"+
"@CompileStatic\n"+
"class Foo {\n"+
"private populateSourceDirectories() {\n"+
" List<File> pluginDependencies\n"+
" foo(pluginDependencies);\n"+
"}\n"+
"private void foo(Iterable<File> iterable) {}\n"+
"}\n"
);
incrementalBuild(projectPath);
expectingNoProblems();
expectingCompiledClassesV("Foo");
// Now compare the generics structure for List (built by jdtresolver mapping into groovy) against List2 (built by groovy)
} finally {
JDTResolver.recordInstances = false;
}
}
public void testCompileStatic_BuildSettings() throws Exception {
try {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project","1.6"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
JDTResolver.recordInstances = true;
env.addGroovyClass(root, "", "BuildSettings",
"import groovy.transform.CompileStatic\n"+
"\n"+
"class BuildSettings {\n"+
"\n"+
" List<File> compileDependencies = []\n"+
" List<File> defaultCompileDependencies = []\n"+
"\n"+
" @CompileStatic\n"+
" void getCompileDependencies() {\n"+
" compileDependencies += defaultCompileDependencies\n"+
" }\n"+
"\n"+
"}\n");
incrementalBuild(projectPath);
expectingNoProblems();
expectingCompiledClassesV("BuildSettings");
// Now compare the generics structure for List (built by jdtresolver mapping into groovy) against List2 (built by groovy)
} finally {
JDTResolver.recordInstances = false;
}
}
private MethodNode getMethodNode(ClassNode jcn, String selector, int paramCount) {
List<MethodNode> mns = jcn.getDeclaredMethods(selector);
for (MethodNode mn: mns) {
if (mn.getParameters().length==paramCount) {
return mn;
}
}
return null;
}
private void compareMethodNodes(MethodNode jmn, MethodNode mn) {
System.out.println("\n\n\nComparing method nodes jmn="+jmn+" mn="+mn);
System.out.println("Comparing return types");
compareClassNodes(jmn.getReturnType(), mn.getReturnType(),1);
compareParameterArrays(jmn.getParameters(),mn.getParameters(),1);
}
private void compareParameterArrays(Parameter[] jps,
Parameter[] ps,int d) {
if (ps==null) {
if (jps!=null) {
fail("Expected null parameters but was "+arrayToString(jps));
}
} else {
if (ps.length!=jps.length) {
fail("Expected same number of parameters, should be "+arrayToString(ps)+" but was "+arrayToString(jps));
}
for (int p=0;p<ps.length;p++) {
System.out.println("Comparing parameters jp="+jps[p]+" p="+ps[p]);
compareParameters(jps[p],ps[p],d+1);
}
}
}
private void compareParameters(Parameter jp, Parameter p,int d) {
compareClassNodes(jp.getType(),p.getType(),d+1);
}
// check whether these are identical (in everything except name!)
private void compareClassNodes(ClassNode jcn, ClassNode cn,int d) {
System.out.println("Comparing ClassNodes\njcn="+jcn.toString()+"\n cn="+cn.toString());
assertEquals(cn.isGenericsPlaceHolder(),jcn.isGenericsPlaceHolder());
// Check GenericsType info
GenericsType[] gt_cn = cn.getGenericsTypes();
GenericsType[] gt_jcn = jcn.getGenericsTypes();
if (gt_cn==null) {
if (gt_jcn!=null) {
fail("Should have been null but was "+arrayToString(gt_jcn));
}
} else {
if (gt_jcn==null) {
fail("Did not expect genericstypes to be null, should be "+arrayToString(gt_cn));
}
assertNotNull(gt_jcn);
assertEquals(gt_cn.length,gt_jcn.length);
for (int i=0;i<gt_cn.length;i++) {
System.out.println("Comparing generics types information, index #"+i);
compareGenericsTypes(gt_jcn[i],gt_cn[i],d+1);
}
}
}
private void compareGenericsTypes(GenericsType jgt, GenericsType gt,int d) {
// protected ClassNode[] upperBounds;
// protected ClassNode lowerBound;
// protected ClassNode type;
// protected String name;
// protected boolean placeholder;
// private boolean resolved;
// private boolean wildcard;
// assertEquals(jgt.getText(),gt.getText());
assertEquals(jgt.getName(),gt.getName());
assertEquals(jgt.isPlaceholder(),gt.isPlaceholder());
assertEquals(jgt.isResolved(),gt.isResolved());
assertEquals(jgt.isWildcard(),gt.isWildcard());
compareType(jgt.getType(),gt.getType(),d+1);
compareUpperBounds(jgt.getUpperBounds(),gt.getUpperBounds(),d+1);
compareLowerBound(jgt.getLowerBound(),gt.getLowerBound(),d+1);
}
private void compareType(ClassNode jcn, ClassNode cn,int d) {
System.out.println("Compare type of GenericsType: jcn="+jcn+" cn="+cn);
compareClassNodes(jcn, cn,d+1);
}
private String arrayToString(ClassNode[] cns) {
if (cns==null) {
return "NULL";
}
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i=0;i<cns.length;i++) {
if (i>0) sb.append(",");
sb.append(cns[i]);
}
sb.append("]");
return sb.toString();
}
private void compareUpperBounds(ClassNode[] jcnlist, ClassNode[] cnlist,int d) {
System.out.println("Comparing upper bounds: jcn="+arrayToString(jcnlist)+" cn="+arrayToString(cnlist));
if (cnlist==null) {
if (jcnlist!=null) {
fail("Should be null but is "+arrayToString(jcnlist));
}
} else {
if (jcnlist==null) {
fail("Array not expected to be null, should be "+arrayToString(cnlist));
}
assertEquals(cnlist.length,cnlist.length);
for (int i=0;i<cnlist.length;i++) {
compareClassNodes(jcnlist[i].redirect(), cnlist[i].redirect(),d+1);
}
}
}
private void compareLowerBound(ClassNode jcn,
ClassNode cn,int d) {
System.out.println("Comparing lower bound");
if (jcn==null) {
assertNull(cn);
} else {
assertNotNull(cn);
compareClassNodes(jcn.redirect(), cn.redirect(),d+1);
}
}
public void testInners_983() throws Exception {
IPath projectPath = env.addProject("Project"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
env.addGroovyClass(root, "", "Outer", "class Outer {\n"
+ " static class Inner {}\n" + "}\n");
env.addClass(root, "", "Client", "public class Client {\n"
+ " { new Outer.Inner(); }\n" + "}\n");
incrementalBuild(projectPath);
expectingCompiledClassesV("Client", "Outer", "Outer$Inner");
expectingNoProblems();
env.addClass(root, "", "Client", "public class Client {\n"
+ " { new Outer.Inner(); }\n" + "}\n");
incrementalBuild(projectPath);
expectingNoProblems();
expectingCompiledClassesV("Client");
}
public void testCompileStatic() throws Exception {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
env.addGroovyClass(root, "", "Outer",
"import groovy.transform.CompileStatic;\n"
+ "@CompileStatic \n" + "int fact(int n) {\n"
+ " if (n==1) {return 1;\n"
+ " } else {return n+fact(n-1);}\n" + "}\n");
// env.addClass(root, "", "Client",
// "public class Client {\n"+
// " { new Outer.Inner(); }\n"+
// "}\n"
// );
incrementalBuild(projectPath);
expectingCompiledClassesV("Outer");
expectingNoProblems();
// env.addClass(root, "", "Client", "public class Client {\n"
// + " { new Outer.Inner(); }\n" + "}\n");
// incrementalBuild(projectPath);
// expectingNoProblems();
// expectingCompiledClassesV("Client");
}
// verify generics are correct for the 'Closure<?>' as CompileStatic will attempt an exact match
public void testCompileStatic2() throws Exception {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
env.addGroovyClass(root, "", "A",
"class A {\n"+
" public void profile(String name, groovy.lang.Closure<?> callable) { }\n"+
"}\n");
incrementalBuild(projectPath);
expectingCompiledClassesV("A");
expectingNoProblems();
env.addGroovyClass(root, "", "B",
"@groovy.transform.CompileStatic\n"+
"class B extends A {\n"+
"\n"+
" def foo() {\n"+
" profile(\"creating plugin manager with classes\") {\n"+
" System.out.println('abc');\n"+
" }\n"+
" }\n"+
"\n"+
"}\n");
incrementalBuild(projectPath);
expectingCompiledClassesV("B","B$_foo_closure1");
expectingNoProblems();
}
public void testCompileStatic3() throws Exception {
if (GroovyUtils.GROOVY_LEVEL < 20) {
return;
}
IPath projectPath = env.addProject("Project"); //$NON-NLS-1$
env.addExternalJars(projectPath, Util.getJavaClassLibs());
env.addGroovyJars(projectPath);
fullBuild(projectPath);
// remove old package fragment root so that names don't collide
env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
env.addGroovyClass(root, "", "Foo",
"class Foo {\n"+
" @groovy.transform.CompileStatic\n"+
" public static void main(String[] args) {\n"+