Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 621f9e0c3ebac7ebb425bda8c1f03b36fb628391 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
/*******************************************************************************
 * Copyright (c) 2000, 2012 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.tests.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Hashtable;

import junit.framework.Test;

import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.core.ExternalFoldersManager;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.util.Util;

/**
 * TO DO:
 * - source attachment on external jar.
 * - don't use assertTrue where assertEquals should be used
 * - don't hardcode positions
*/
public class AttachSourceTests extends ModifyingResourceTests {
	static {
//		TESTS_NAMES = new String[] { "testConstructorAccess" };
//		TESTS_NUMBERS = new int[] { 5 };
//		TESTS_RANGE = new int[] { 169, 180 };
	}

	public static Test suite() {
		return buildModelTestSuite(AttachSourceTests.class);
	}

	/** @deprecated using deprecated code */
	private static final int AST_INTERNAL_JLS2 = AST.JLS2;

	private IPackageFragmentRoot pkgFragmentRoot;

public AttachSourceTests(String name) {
	super(name);
}
protected String getExternalFolder() {
	return getExternalResourcePath("externalFolder");
}
public ASTNode runConversion(IClassFile classFile, boolean resolveBindings) {
	ASTParser parser = ASTParser.newParser(AST_INTERNAL_JLS2);
	parser.setSource(classFile);
	parser.setResolveBindings(resolveBindings);
	parser.setWorkingCopyOwner(null);
	return parser.createAST(null);
}
protected void setUp() throws Exception {
	super.setUp();
	attachSource(this.pkgFragmentRoot, "/AttachSourceTests/attachsrc.zip", "");
}
/**
 * Create project and set the jar placeholder.
 */
public void setUpSuite() throws Exception {
	super.setUpSuite();

	setUpJavaProject("AttachSourceTests");
	addLibraryEntry("/AttachSourceTests/b153133.jar", false);
	this.pkgFragmentRoot = this.currentProject.getPackageFragmentRoot(getFile("/AttachSourceTests/attach.jar"));
	setUpGenericJar();
	setUpInnerClassesJar();
	setupExternalLibrary();
}
private void setupExternalLibrary() throws IOException {
	String externalFolder = getExternalFolder();
	String[] pathsAndContents =
		new String[] {
			"p/X.java",
			"package p;\n" +
			"public class X {\n" +
			"}"
		};
	org.eclipse.jdt.core.tests.util.Util.createClassFolder(pathsAndContents, externalFolder + "/lib", "1.4");
	org.eclipse.jdt.core.tests.util.Util.createSourceDir(pathsAndContents, externalFolder + "/src");

	org.eclipse.jdt.core.tests.util.Util.createJar(pathsAndContents, externalFolder + "/lib.abc", "1.4");
	org.eclipse.jdt.core.tests.util.Util.createSourceZip(pathsAndContents, externalFolder + "/src.abc");
}
private void setUpGenericJar() throws IOException, CoreException {
	String[] pathAndContents = new String[] {
		"generic/X.java",
		"package generic;\n" +
		"public class X<T> {\n" +
		"  void foo(X<T> x) {\n" +
		"  }\n" +
		"  <K, V> V foo(K key, V value) {\n" +
		"    return value;\n" +
		"  }\n" +
		"  void foo(int i, X<Object[]> x) {\n" +
		"  }\n" +
		"  void foo(boolean b, X<? extends X> x) {\n" +
		"  }\n" +
		"  void foo(float f, X<?> x) {\n" +
		"  }\n" +
		"  void foo(Y<? extends Integer, ? extends Object> y) {\n" +
		"  }\n" +
		"  void foo(Z.Inner<Object> inner) {\n" +
		"  }\n" +
		"  void foo(AType<Object> t) {\n" +
		"  }\n" +
		"}\n" +
		"class Y<K, V> {\n" +
		"}\n" +
		"class Z {\n" +
		"  class Inner<E> {\n" +
		"  }\n" +
		"}\n" +
		"class AType<E> {\n" + // type name containing character 'T'
		"}",
		"Container.java",
		"public class Container {\n" + 
		"	class Inner<S> {\n" + 
		"		Inner(String st, Class<S> s) {\n" + 
		"			super();\n" + 
		"		}\n" + 
		"	}\n" + 
		"}"
	};
	addLibrary("generic.jar", "genericsrc.zip", pathAndContents, JavaCore.VERSION_1_5);
}
private void setUpInnerClassesJar() throws IOException, CoreException {
	String[] pathAndContents = new String[] {
		"inner/X.java",
		"package inner;\n" +
		"public class X {\n" +
		"  void foo() {\n" +
		"    new X() {};\n" +
		"    class Y {}\n" +
		"    new Y() {\n" +
		"      class Z {}\n" +
		"    };\n" +
		"    class W {\n" +
		"      void bar() {\n" +
		"        new W() {};\n" +
		"      }\n" +
		"    }\n" +
		"    new Object() {\n" +
		"      class U {\n" +
		"        U(String s) {\n" +
		"        }\n" +
		"      }\n" +
		"    };\n" +
		"  }\n" +
		"  class V {\n" +
		"    V(String s) {\n" +
		"    }\n" +
		"  }\n" +
		"  class Inner {\n" +
		"    Inner() {\n" +
		"    }\n" +
		"  }\n" +
		"}"
	};
	addLibrary("innerClasses.jar", "innerClassessrc.zip", pathAndContents, JavaCore.VERSION_1_4);
}
protected void tearDown() throws Exception {
	IPackageFragmentRoot[] roots = this.currentProject.getAllPackageFragmentRoots();
	for (int i = 0; i < roots.length; i++) {
		IPackageFragmentRoot root = roots[i];
		if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
			attachSource(root, null, null); // detach source
		}
	}
	super.tearDown();
}
/**
 * Reset the jar placeholder and delete project.
 */
public void tearDownSuite() throws Exception {
	org.eclipse.jdt.core.tests.util.Util.flushDirectoryContent(new File(getExternalFolder()));
	deleteProject(this.currentProject);
	super.tearDownSuite();
}

/**
 * Test AST.parseCompilationUnit(IClassFile, boolean).
 */
public void testASTParsing() throws JavaModelException {
	attachSource(this.pkgFragmentRoot, "/AttachSourceTests/attachsrc.zip", "");
	IClassFile classFile = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	ASTNode node = runConversion(classFile, true);
	assertNotNull("No node", node);
	attachSource(this.pkgFragmentRoot, null, null);
	IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	assertTrue("source code should no longer exist for A", cf.getSource() == null);
	try {
		node = runConversion(classFile, true);
		assertTrue("Should not be here", false);
	} catch(IllegalStateException e) {
		assertTrue(true);
	}
}
/**
 * Test AST.parseCompilationUnit(IClassFile, boolean).
 * Test for http://bugs.eclipse.org/bugs/show_bug.cgi?id=30471
 */
public void testASTParsing2() throws JavaModelException {
	attachSource(this.pkgFragmentRoot, "/AttachSourceTests/attachsrc.zip", "");
	IClassFile classFile = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	ASTNode node = runConversion(classFile, false);
	assertNotNull("No node", node);
	attachSource(this.pkgFragmentRoot, null, null);
	IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	assertTrue("source code should no longer exist for A", cf.getSource() == null);
	try {
		node = runConversion(classFile, false);
		assertTrue("Should not be here", false);
	} catch(IllegalStateException e) {
		assertTrue(true);
	}
}
/**
 * Changing the source attachment file should update the java model.
 * (regression test for bug 23292 Must restart Eclipse after debug of source in .zip is updated)
 */
public void testChangeSourceAttachmentFile() throws CoreException {
	IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	IMethod method = cf.getType().getMethod("foo", new String[] {});

	// check initial source
	assertSourceEquals(
		"unexpected initial source for foo()",
		"public void foo() {\n" +
		"	}",
		method.getSource());

	// replace source attachment file
	swapFiles("AttachSourceTests/attachsrc.zip", "AttachSourceTests/attachsrc.new.zip");
	assertSourceEquals(
		"unexpected source for foo() after replacement",
		"public void foo() {\n" +
		"		System.out.println(\"foo\");\n" +
		"	}",
		method.getSource());

	// delete source attachment file
	deleteFile("AttachSourceTests/attachsrc.zip");
	assertSourceEquals(
		"unexpected source for foo() after deletion",
		null,
		method.getSource());

	// add source attachment file back
	moveFile("AttachSourceTests/attachsrc.new.zip", "AttachSourceTests/attachsrc.zip");
	((JavaProject)this.currentProject).resetResolvedClasspath();
	assertSourceEquals(
		"unexpected source for foo() after addition",
		"public void foo() {\n" +
		"	}",
		method.getSource());
}
/**
 * Ensure that a class file with an attached source can retrieve its children given a source index.
 */
public void testClassFileGetElementAt01() throws JavaModelException {
	IClassFile classFile = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	String source = classFile.getSource();
	IJavaElement element = classFile.getElementAt(source.indexOf("class A"));
	assertElementExists(
		"Unexpected element",
		"A [in A.class [in x.y [in attach.jar [in AttachSourceTests]]]]",
		element);
}
/**
 * Ensure that a class file with an attached source can retrieve its children given a source index.
 */
public void testClassFileGetElementAt02() throws JavaModelException {
	IClassFile classFile = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	String source = classFile.getSource();
	IJavaElement element = classFile.getElementAt(source.indexOf("public A"));
	assertElementExists(
		"Unexpected element",
		"A() [in A [in A.class [in x.y [in attach.jar [in AttachSourceTests]]]]]",
		element);
}
/**
 * Ensure that a class file with an attached source can retrieve its children given a source index.
 */
public void testClassFileGetElementAt03() throws JavaModelException {
	IClassFile classFile = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	String source = classFile.getSource();
	IJavaElement element = classFile.getElementAt(source.indexOf("void foo"));
	assertElementExists(
		"Unexpected element",
		"foo() [in A [in A.class [in x.y [in attach.jar [in AttachSourceTests]]]]]",
		element);
}
/*
 * Ensure that a constructor of a binary member type can be retrieved with its source position.
 * (regression test for bug 119249 codeResolve, search, etc. don't work on constructor of binary inner class)
 */
public void testClassFileGetElementAt04() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IClassFile classFile = fragment.getClassFile("X$V.class");
	String source = classFile.getSource();
	IJavaElement element = classFile.getElementAt(source.indexOf("V(String s)"));
	assertElementExists(
		"Unexpected element",
		"V(inner.X, java.lang.String) [in V [in X$V.class [in inner [in innerClasses.jar [in AttachSourceTests]]]]]",
		element);
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of a .class file is implicetely attached when prj=src=bin
 * (regression test for bug 41444 [navigation] error dialog on opening class file)
 */
public void testClassFileInOutput() throws CoreException {
	IClassFile classFile = getClassFile("AttachSourceTests/src/A.class");
	String source = classFile.getSource();
	assertSourceEquals(
		"Unexpected source",
		"public class A {\n" +
		"}",
		source);
}
/**
 * Retrieves the source code for "A.class", which is
 * the entire CU for "A.java".
 */
public void testClassRetrieval() throws JavaModelException {
	IClassFile objectCF = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	assertTrue("source code does not exist for the entire attached compilation unit", objectCF.getSource() != null);
}
/**
 * Removes the source attachment from the jar.
 */
public void testDetachSource() throws JavaModelException {
	attachSource(this.pkgFragmentRoot, null, null);
	IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	assertTrue("source code should no longer exist for A", cf.getSource() == null);
	assertTrue("name range should no longer exist for A", cf.getType().getNameRange().getOffset() == -1);
	assertTrue("source range should no longer exist for A", cf.getType().getSourceRange().getOffset() == -1);
	assertTrue("Source attachment path should be null", null == this.pkgFragmentRoot.getSourceAttachmentPath());
	assertTrue("Source attachment root path should be null", null ==this.pkgFragmentRoot.getSourceAttachmentRootPath());
}
/*
 * Ensures that one can attach an external source folder to a library folder.
 */
public void testExternalFolder1() throws CoreException {
	try {
		IProject p = createProject("P1");
		IFolder lib = p.getFolder("lib");
		lib.createLink(new Path(getExternalFolder() + "/lib"), IResource.NONE, null);
		IJavaProject javaProject = createJavaProject("P2", new String[0], new String[] {"/P1/lib"}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(lib);
		attachSource(root, getExternalFolder() + "/src", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteProject("P1");
		deleteProject("P2");
	}
}
/*
 * Ensures that one can attach a source folder to an external library folder.
 */
public void testExternalFolder2() throws CoreException {
	try {
		IProject p = createProject("P1");
		IFolder src = p.getFolder("src");
		src.createLink(new Path(getExternalFolder() + "/src"), IResource.NONE, null);
		String externalLib = getExternalFolder() + "/lib";
		IJavaProject javaProject = createJavaProject("P2", new String[0], new String[] {externalLib}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(externalLib);
		attachSource(root, "/P1/src", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteProject("P1");
		deleteProject("P2");
	}
}
/*
 * Ensures that one can attach an external source folder to an external library folder.
 */
public void testExternalFolder3() throws CoreException {
	try {
		String externalLib = getExternalFolder() + "/lib";
		IJavaProject javaProject = createJavaProject("P", new String[0], new String[] {externalLib}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(externalLib);
		attachSource(root, getExternalFolder() + "/src", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteProject("P");
	}
}
/*
 * Ensures that root paths are correctly detected when attaching an external source folder to an external library folder.
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=227813 )
 */
public void testExternalFolder4() throws Exception {
	try {
		String externalFolder = getExternalFolder();
		String[] pathsAndContents =
			new String[] {
				"p/X.java",
				"package p;\n" +
				"public class X {\n" +
				"}"
			};
		org.eclipse.jdt.core.tests.util.Util.createSourceDir(pathsAndContents, externalFolder + "/src227813/root1/subroot");
		pathsAndContents =
			new String[] {
				"q/X.java",
				"package q;\n" +
				"public class X {\n" +
				"}"
			};
		org.eclipse.jdt.core.tests.util.Util.createSourceDir(pathsAndContents, externalFolder + "/src227813/root2/subroot");

		String externalLib = externalFolder + "/lib";
		IJavaProject javaProject = createJavaProject("P", new String[0], new String[] {externalLib}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(externalLib);
		attachSource(root, externalFolder + "/src227813", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteExternalResource("externalFolder/src227813");
		deleteProject("P");
	}
}
/*
 * Ensures that root paths are correctly detected when attaching an external source folder that contains a META-INF folder to an external library folder.
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=228639 )
 */
public void testExternalFolder5() throws Exception {
	try {
		String externalFolder = getExternalFolder();
		String[] pathsAndContents =
			new String[] {
				"p/X.java",
				"package p;\n" +
				"public class X {\n" +
				"}"
			};
		org.eclipse.jdt.core.tests.util.Util.createSourceDir(pathsAndContents, externalFolder + "/src228639/src");

		createExternalFolder("externalFolder/src228639/META-INF");
		createExternalFolder("externalFolder/lib/META-INF");

		String externalLib = externalFolder + "/lib";
		IJavaProject javaProject = null;
		javaProject = createJavaProject("P", new String[0], new String[] {externalLib}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(externalLib);
		attachSource(root, externalFolder + "/src228639", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteExternalResource("externalFolder/src228639");
		deleteExternalResource("externalFolder/lib/META-INF");
		deleteProject("P");
	}
}
/*
 * Ensures that one can attach an external ZIP archive containing sources to a library folder.
 */
public void testZIPArchive1() throws CoreException {
	try {
		IProject p = createProject("P1");
		IFolder lib = p.getFolder("lib");
		lib.createLink(new Path(getExternalFolder() + "/lib"), IResource.NONE, null);
		IJavaProject javaProject = createJavaProject("P2", new String[0], new String[] {"/P1/lib"}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(lib);
		attachSource(root, getExternalFolder() + "/src.abc", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteProject("P1");
		deleteProject("P2");
	}
}
/*
 * Ensures that one can attach a source folder to an external ZIP archive.
 */
public void testZIPArchive2() throws CoreException {
	try {
		IProject p = createProject("P1");
		IFolder src = p.getFolder("src");
		src.createLink(new Path(getExternalFolder() + "/src"), IResource.NONE, null);
		String externalLib = getExternalFolder() + "/lib.abc";
		IJavaProject javaProject = createJavaProject("P2", new String[0], new String[] {externalLib}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(externalLib);
		attachSource(root, "/P1/src", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteProject("P1");
		deleteProject("P2");
	}
}
/*
 * Ensures that one can attach an external ZIP archive containing sources to an external ZIP archive.
 */
public void testZIPArchive3() throws CoreException {
	try {
		String externalLib = getExternalFolder() + "/lib.abc";
		IJavaProject javaProject = createJavaProject("P", new String[0], new String[] {externalLib}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(externalLib);
		attachSource(root, getExternalFolder() + "/src.abc", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteProject("P");
	}
}
/*
 * Ensures that one can attach an internal ZIP archive containing sources to an internal ZIP archive.
 */
public void testZIPArchive4() throws CoreException {
	try {
		IProject p = createProject("P1");
		IFile lib = p.getFile("lib.abc");
		lib.createLink(new Path(getExternalFolder() + "/lib.abc"), IResource.NONE, null);
		IFile src = p.getFile("src.abc");
		src.createLink(new Path(getExternalFolder() + "/src.abc"), IResource.NONE, null);
		IJavaProject javaProject = createJavaProject("P2", new String[0], new String[] {"/P1/lib.abc"}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(lib);
		attachSource(root, "/P1/src.abc", "");
		IType type = root.getPackageFragment("p").getClassFile("X.class").getType();
		assertSourceEquals(
			"Unexpected source",
			"public class X {\n" +
			"}",
			type.getSource());
	} finally {
		deleteProject("P1");
		deleteProject("P2");
	}
}
/*
 * Test that a path must have at least one segment
 */
public void test264301() throws CoreException {
	String os = Platform.getOS();
	if (!Platform.OS_WIN32.equals(os)) {
		return;
	}

	try {
		IJavaProject javaProject = createJavaProject("Test", new String[]{""}, new String[]{"/AttachSourceTests/test.jar"}, "");
		createFolder("/Test/test1");
		createFile("/Test/test1/Test.java",
			"package test1;\n" +
			"\n" +
			"public class Test {}");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(getFile("/AttachSourceTests/test.jar"));
		try {
			attachSource(root, "C:/", null);
			assertTrue("Should not be there", false);
		} catch(JavaModelException e) {
			// expected exception
		}
	} finally {
		deleteProject("Test");
	}
}
/*
 * Ensures that the source of a generic method can be retrieved.
 */
public void testGeneric1() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"QX<QT;>;"});
	assertSourceEquals(
		"Unexpected source",
		"void foo(X<T> x) {\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of a generic method can be retrieved.
 */
public void testGeneric2() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"QK;", "QV;"});
	assertSourceEquals(
		"Unexpected source",
		"<K, V> V foo(K key, V value) {\n" +
		"    return value;\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of a generic method can be retrieved.
 * (regression test for bug 129317 Outline view inconsistent with code
 */
public void testGeneric3() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"I", "Lgeneric.X<[Ljava.lang.Object;>;"});
	assertSourceEquals(
		"Unexpected source",
		"void foo(int i, X<Object[]> x) {\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
public void testGeneric4() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"Z", "Lgeneric.X<+Lgeneric.X;>;"});
	assertSourceEquals(
		"Unexpected source",
		"void foo(boolean b, X<? extends X> x) {\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
public void testGeneric5() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"F", "Lgeneric.X<*>;"});
	assertSourceEquals(
		"Unexpected source",
		"void foo(float f, X<?> x) {\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
public void testGeneric6() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"Lgeneric.Y<+Ljava.lang.Integer;+Ljava.lang.Object;>;"});
	assertSourceEquals(
		"Unexpected source",
		"void foo(Y<? extends Integer, ? extends Object> y) {\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
public void testGeneric7() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"Lgeneric.Z.Inner<Ljava.lang.Object;>;"});
	assertSourceEquals(
		"Unexpected source",
		"void foo(Z.Inner<Object> inner) {\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
public void testGeneric8() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	IType type = root.getPackageFragment("generic").getClassFile("X.class").getType();
	IMethod method = type.getMethod("foo", new String[] {"Lgeneric.AType<Ljava.lang.Object;>;"});
	assertSourceEquals(
		"Unexpected source",
		"void foo(AType<Object> t) {\n" +
		"  }",
		method.getSource());
	attachSource(root, null, null); // detach source
}
/**
 * Ensures that name ranges exists for BinaryMembers that have
 * mapped source.
 */
public void testGetNameRange01() throws JavaModelException {
	IClassFile classFile = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	IMethod method = classFile.getType().getMethod("foo", null);
	assertSourceEquals("Unexpected name source", "foo", getNameSource(classFile.getSource(), method));
}
/**
 * Ensures that name ranges exists for BinaryMembers that have
 * mapped source.
 */
public void testGetNameRange02() throws JavaModelException {
	IClassFile classFile = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	assertSourceEquals("Unexpected name source", "A", getNameSource(classFile.getSource(), classFile.getType()));
}
/*
 * Ensure that the name range for a constructor of a binary member type is correct.
 * (regression test for bug 119249 codeResolve, search, etc. don't work on constructor of binary inner class)
 */
public void testGetNameRange03() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IClassFile classFile = fragment.getClassFile("X$V.class");
	IMethod constructor = classFile.getType().getMethod("V", new String[] {"Linner.X;", "Ljava.lang.String;"});
	assertSourceEquals("Unexpected name source", "V", getNameSource(classFile.getSource(), constructor));

	attachSource(root, null, null); // detach source
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=108784
public void testGetNameRange04() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IClassFile classFile = fragment.getClassFile("X$Inner.class");
	IMethod[] methods = classFile.getType().getMethods();
	for (int i = 0; i < methods.length; i++) {
		IMethod iMethod = methods[i];
		ISourceRange nameRange = iMethod.getNameRange();
		assertTrue("Unexpected name range", nameRange.getOffset() != -1);
		assertTrue("Unexpected name range", nameRange.getLength() != 0);
	}

	attachSource(root, null, null); // detach source
}
/**
 * Retrieves the source attachment paths for jar root.
 */
public void testGetSourceAttachmentPath() throws JavaModelException {
	IPath saPath= this.pkgFragmentRoot.getSourceAttachmentPath();
	assertEquals("Source attachment path not correct for root " + this.pkgFragmentRoot, "/AttachSourceTests/attachsrc.zip", saPath.toString());
	assertEquals("Source attachment root path should be empty", new Path(""), this.pkgFragmentRoot.getSourceAttachmentRootPath());
}
/**
 * Ensures that a source range exists for the class file that has
 * mapped source.
 */
public void testGetSourceRange() throws JavaModelException {
	IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	ISourceRange sourceRange = cf.getSourceRange();
	assertTrue("Class file should have associated source range", sourceRange != null);
	assertEquals("Unexpected offset", 0, sourceRange.getOffset());
	assertEquals("Unexpected length", 100, sourceRange.getLength());
}
/**
 * Ensures that a source range exists for the (inner) class file that has
 * mapped source.
 */
public void testGetSourceRangeInnerClass() throws JavaModelException {
	IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A$Inner.class");
	ISourceRange sourceRange = cf.getSourceRange();
	assertTrue("Inner class file should have associated source range", sourceRange != null);
	assertEquals("Unexpected offset", 0, sourceRange.getOffset());
	assertEquals("Unexpected length", 100, sourceRange.getLength());
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass1() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"public class X {\n" +
		"  void foo() {\n" +
		"    new X() {};\n" +
		"    class Y {}\n" +
		"    new Y() {\n" +
		"      class Z {}\n" +
		"    };\n" +
		"    class W {\n" +
		"      void bar() {\n" +
		"        new W() {};\n" +
		"      }\n" +
		"    }\n" +
		"    new Object() {\n" +
		"      class U {\n" +
		"        U(String s) {\n" +
		"        }\n" +
		"      }\n" +
		"    };\n" +
		"  }\n" +
		"  class V {\n" +
		"    V(String s) {\n" +
		"    }\n" +
		"  }\n" +
		"  class Inner {\n" + 
		"    Inner() {\n" + 
		"    }\n" + 
		"  }\n" + 
		"}",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass2() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$1.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"new X() {}",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass3() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$2.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"new Y() {\n" +
		"      class Z {}\n" +
		"    }",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass4() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$3.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"new W() {}",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass5() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$1$Y.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"class Y {}",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass6() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$1$W.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"class W {\n" +
		"      void bar() {\n" +
		"        new W() {};\n" +
		"      }\n" +
		"    }",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass7() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$2$Z.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"class Z {}",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 */
public void testInnerClass8() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$V.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"class V {\n" +
		"    V(String s) {\n" +
		"    }\n" +
		"  }",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/*
 * Ensures that the source of an inner class can be retrieved.
 * (regression test for bug 124611 IAE in Signature.createCharArrayTypeSignature)
 */
public void testInnerClass9() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IType type = fragment.getClassFile("X$4$U.class").getType();
	assertSourceEquals(
		"Unexpected source",
		"class U {\n" +
		"        U(String s) {\n" +
		"        }\n" +
		"      }",
		type.getSource());
	attachSource(root, null, null); // detach source
}
/**
 * Ensures that a source folder can be attached to a lib folder.
 */
public void testLibFolder() throws JavaModelException {
	IPackageFragmentRoot root = this.getPackageFragmentRoot("/AttachSourceTests/lib");
	attachSource(root, "/AttachSourceTests/srcLib", "");

	IClassFile cf = root.getPackageFragment("p").getClassFile("X.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p;\n" +
		"public class X {\n" +
		"	public void foo() {\n" +
		"	}\n" +
		"}",
		cf.getSource());
}
/**
 * Retrieves the source code for methods of class A.
 */
public void testMethodRetrieval() throws JavaModelException {
	IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
	IMethod[] methods = cf.getType().getMethods();
	for (int i = 0; i < methods.length; i++) {
		IMethod method = methods[i];
		assertTrue("source code does not exist for the method " + method, method.getSource() != null);
		assertTrue("method name range not correct", method.getNameRange().getOffset() != -1 && method.getNameRange().getLength() != 0);
	}
}
/**
 * Closes the jar, to ensure when it is re-opened the source
 * attachment still exists.
 */
public void testPersistence() throws JavaModelException {
	this.pkgFragmentRoot.close();
	testClassRetrieval();
	testMethodRetrieval();
}

/*
 * Ensures that having a project as a class folder and attaching its sources finds the source
 * of a class in a non-default package.
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=65186)
 */
public void testProjectAsClassFolder1() throws CoreException {
	try {
		createJavaProject("P1");
		createFolder("/P1/p");
		createFile(
			"/P1/p/X.java",
			"package p;\n" +
			"public class X {\n" +
			"}"
		);
		IProject p1 = getProject("P1");
		p1.build(IncrementalProjectBuilder.FULL_BUILD, null);
		IJavaProject javaProject = createJavaProject("P2", new String[]{""}, new String[]{"/P1"}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(p1);
		attachSource(root, "/P1", null);
		IClassFile cf = root.getPackageFragment("p").getClassFile("X.class");
		assertSourceEquals(
			"Unexpected source for class file P1/p/X.class",
			"package p;\n" +
			"public class X {\n" +
			"}",
			cf.getSource());
	} finally {
		deleteProject("P1");
		deleteProject("P2");
	}
}

/*
 * Ensures that having a project as a class folder and attaching its sources finds the source
 * of a class in the default package.
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=65186)
 */
public void testProjectAsClassFolder2() throws CoreException {
	try {
		createJavaProject("P1");
		createFile(
			"/P1/X.java",
			"public class X {\n" +
			"}"
		);
		IProject p1 = getProject("P1");
		p1.build(IncrementalProjectBuilder.FULL_BUILD, null);
		IJavaProject javaProject = createJavaProject("P2", new String[]{""}, new String[]{"/P1"}, "");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(p1);
		attachSource(root, "/P1", null);
		IClassFile cf = root.getPackageFragment("").getClassFile("X.class");
		assertSourceEquals(
			"Unexpected source for class file P1/X.class",
			"public class X {\n" +
			"}",
			cf.getSource());
	} finally {
		deleteProject("P1");
		deleteProject("P2");
	}
}

/*
 * Ensures that having a project as source attachment finds the source
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=65186)
 */
public void testProjectAsSourceAttachment() throws CoreException {
	try {
		IJavaProject javaProject = createJavaProject("Test", new String[]{""}, new String[]{"/AttachSourceTests/test.jar"}, "");
		createFolder("/Test/test1");
		createFile("/Test/test1/Test.java",
			"package test1;\n" +
			"\n" +
			"public class Test {}");
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(getFile("/AttachSourceTests/test.jar"));
		attachSource(root, "/Test", null);
		IClassFile cf = root.getPackageFragment("test1").getClassFile("Test.class");
		assertSourceEquals(
			"Unexpected source for class file test1/Test.class",
			"package test1;\n" +
			"\n" +
			"public class Test {}",
			cf.getSource());
	} finally {
		deleteProject("Test");
	}
}

/*
 * Ensures that a source attached during a session is not taken into account on restart
 * if the entry has a source attachment.
 * (regression test for bug 183413 PDE can't find the source for plug-ins in the target)
 */
public void testRestart() throws Exception {
	try {
		this.pkgFragmentRoot.attachSource(new Path("/AttachSourceTests/attachsrc.new.zip"), new Path("")/*source root*/, null/*no progress*/);

		simulateExitRestart();
		JavaCore.initializeAfterLoad(null);

		// ensure source is correct
		IClassFile cf = this.pkgFragmentRoot.getPackageFragment("x.y").getClassFile("A.class");
		IMethod method = cf.getType().getMethod("foo", new String[] {});
		assertSourceEquals(
			"unexpected source for foo()",
			"public void foo() {\n" +
			"	}",
			method.getSource());
	} finally {
		this.pkgFragmentRoot.attachSource(null/*no source attachment*/, null/*no source root*/, null/*no progress*/);
	}
}

/**
 * Attaches a source zip to a jar.  The source zip has
 * a nested root structure and exists as a resource.  Tests that
 * the attachment is persisted as a server property for the jar.
 */
public void testRootPath() throws JavaModelException {
	IJavaProject project = getJavaProject("AttachSourceTests");
	IFile jar = (IFile) project.getProject().findMember("attach2.jar");
	IFile srcZip=(IFile) project.getProject().findMember("attach2src.zip");
	JarPackageFragmentRoot root = (JarPackageFragmentRoot) project.getPackageFragmentRoot(jar);
	root.attachSource(srcZip.getFullPath(), new Path("src/nested"), null);

	IClassFile cf = root.getPackageFragment("x.y").getClassFile("B.class");
	assertTrue("source code does not exist for the entire attached compilation unit", cf.getSource() != null);
	root.close();
	cf = root.getPackageFragment("x.y").getClassFile("B.class");
	assertTrue("source code does not exist for the entire attached compilation unit", cf.getSource() != null);

	IPath rootSAPath= root.getSourceAttachmentRootPath();
	assertEquals("Unexpected source attachment root path for " + root.getPath(), "src/nested", rootSAPath.toString());

	IPath saPath= root.getSourceAttachmentPath();
	assertEquals("Unexpected source attachment path for " + root.getPath(), "/AttachSourceTests/attach2src.zip", saPath.toString());

	root.close();
}
/**
 * Attaches a source zip to a jar specifying an invalid root path.
 * Ensures that the root path is just used as a hint, and that the source is still retrieved.
 */
public void testRootPath2() throws JavaModelException {
	IJavaProject project = getJavaProject("AttachSourceTests");
	IFile jar = (IFile) project.getProject().findMember("attach2.jar");
	IFile srcZip=(IFile) project.getProject().findMember("attach2src.zip");
	JarPackageFragmentRoot root = (JarPackageFragmentRoot) project.getPackageFragmentRoot(jar);
	root.attachSource(srcZip.getFullPath(), new Path(""), null);

	IClassFile cf = root.getPackageFragment("x.y").getClassFile("B.class");
	assertTrue("source code does not exist for the entire attached compilation unit", cf.getSource() != null);
	root.close();
}
/**
 * Attaches a sa source folder can be attached to a lib folder specifying an invalid root path.
 * Ensures that the root path is just used as a hint, and that the source is still retrieved.
 */
public void testRootPath3() throws JavaModelException {
	IPackageFragmentRoot root = this.getPackageFragmentRoot("/AttachSourceTests/lib");
	attachSource(root, "/AttachSourceTests/srcLib", "invalid");

	IClassFile cf = root.getPackageFragment("p").getClassFile("X.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p;\n" +
		"public class X {\n" +
		"	public void foo() {\n" +
		"	}\n" +
		"}",
		cf.getSource());
	root.close();
}
/**
 * Attach a jar with a source attachment that doesn't contain the source folders
 */
public void testRootPath4() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/test.jar"));
	attachSource(root, "/AttachSourceTests/src.zip", "invalid");

	IClassFile cf = root.getPackageFragment("test1").getClassFile("Test.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package test1;\n" +
		"\n" +
		"public class Test {}",
		cf.getSource());
	root.close();
}
/**
 * Attach a jar with a source attachment that doesn't contain the source folders
 */
public void testRootPath5() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/update.jar"));
	attachSource(root, "/AttachSourceTests/src.zip", "invalid");

	IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("A.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p1.p2;\n" +
		"\n" +
		"public class A {}",
		cf.getSource());

	cf = root.getPackageFragment("").getClassFile("B.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"public class B {}",
		cf.getSource());

	attachSource(root, null, null); // detach source
}
/**
 * Attach a jar with a source attachment that doesn't contain the source folders
 */
public void testRootPath6() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/update.jar"));
	attachSource(root, "/AttachSourceTests/src.zip", null);

	IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("A.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p1.p2;\n" +
		"\n" +
		"public class A {}",
		cf.getSource());

	cf = root.getPackageFragment("").getClassFile("B.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"public class B {}",
		cf.getSource());

	attachSource(root, null, null); // detach source
}
/**
 * Attach a jar with a source attachment that doesn't contain the source folders
 */
public void testRootPath7() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/full.jar"));
	attachSource(root, "/AttachSourceTests/src.zip", null);

	IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("A.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p1.p2;\n" +
		"\n" +
		"public class A {}",
		cf.getSource());

	cf = root.getPackageFragment("").getClassFile("B.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"public class B {}",
		cf.getSource());

	cf = root.getPackageFragment("test1").getClassFile("Test.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package test1;\n" +
		"\n" +
		"public class Test {}",
		cf.getSource());

	attachSource(root, null, null); // detach source
}
/**
 * Attach a jar with a source attachment that contains the source folders
 */
public void testRootPath8() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/full.jar"));
	attachSource(root, "/AttachSourceTests/fullsrc.zip", null);

	IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("A.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p1.p2;\n" +
		"\n" +
		"public class A {}",
		cf.getSource());

	cf = root.getPackageFragment("").getClassFile("B.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"public class B {}",
		cf.getSource());

	cf = root.getPackageFragment("test1").getClassFile("Test.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package test1;\n" +
		"\n" +
		"public class Test {}",
		cf.getSource());

	attachSource(root, null, null); // detach source
}
/**
 * Attach a jar with a source attachment that contains the source folders
 */
public void testRootPath9() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/full.jar"));
	attachSource(root, "/AttachSourceTests/fullsrc.zip", "invalid");

	IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("A.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p1.p2;\n" +
		"\n" +
		"public class A {}",
		cf.getSource());

	cf = root.getPackageFragment("").getClassFile("B.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"public class B {}",
		cf.getSource());

	cf = root.getPackageFragment("test1").getClassFile("Test.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package test1;\n" +
		"\n" +
		"public class Test {}",
		cf.getSource());

	attachSource(root, null, null); // detach source
}
/**
 * Attach a jar with a source attachment that is itself
 */
public void testRootPath10() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/test2.jar"));
	attachSource(root, "/AttachSourceTests/test2.jar", null);

	IClassFile cf = root.getPackageFragment("p").getClassFile("X.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p;\n" +
		"\n" +
		"public class X {\n" +
		"\n" +
		"	public static void main(String[] args) {\n" +
		"	}\n" +
		"}",
		cf.getSource());
	attachSource(root, null, null); // detach source
}
/**
 * http://bugs.eclipse.org/bugs/show_bug.cgi?id=35965
 */
public void testRootPath11() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/test4.jar"));
	attachSource(root, "/AttachSourceTests/test4_src.zip", null);

	IClassFile cf = root.getPackageFragment("P1").getClassFile("D.class");
	assertSourceEquals(
		"Unexpected source for class file P1.D",
		"package P1;\n" +
		"\n" +
		"public class D {}",
		cf.getSource());

	cf = root.getPackageFragment("P1.p2").getClassFile("A.class");
	assertSourceEquals(
		"Unexpected source for class file P1.p2.A",
		"package P1.p2;\n" +
		"\n" +
		"public class A {}",
		cf.getSource());

	assertTrue("Not a binary root", root.getKind() == IPackageFragmentRoot.K_BINARY);
	assertEquals("wrong jdk level", ClassFileConstants.JDK1_2, Util.getJdkLevel(root.getResource()));
	attachSource(root, null, null); // detach source
}
/**
 * Attach a jar with a source attachment that is itself. The jar contains 2 root paths for the same class file.
 * (regression test for bug 74014 prefix path for source attachments - automatic detection does not seem to work)
 */
public void testRootPath12() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/test5.jar"));
	attachSource(root, "/AttachSourceTests/test5.jar", null);

	IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("X.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p1.p2;\n" +
		"public class X {\n" +
		"}\n",
		cf.getSource());
	attachSource(root, null, null); // detach source
}
/**
 * @see "http://bugs.eclipse.org/bugs/show_bug.cgi?id=110172"
 */
public void testBug110172() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/test6.jar"));
	assertTrue("Root doesn't exist", root.exists());
	attachSource(root, "/AttachSourceTests/test6src.zip", null);

	try {
		// check the javadoc source range in a class file
		IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("X.class");
		assertNotNull(cf);
		final String source = cf.getSource();
		assertNotNull("No source", source);
		IJavaElement[] children = cf.getChildren();
		assertEquals("Wrong number of children", 1, children.length);
		IJavaElement element = children[0];
		assertTrue("Not a type", element instanceof IType);
		IType type = (IType) element;
		IJavaElement[] members = type.getChildren();
		final int length = members.length;
		assertEquals("Wrong number", 9, length);
		for (int i = 0; i < length; i++) {
			element = members[i];
			assertTrue(element instanceof IMember);
			final ISourceRange javadocRange = ((IMember) element).getJavadocRange();
			final String elementName = element.getElementName();
			if ("f".equals(elementName)) {
				assertNotNull("No javadoc source range", javadocRange);
				final int start = javadocRange.getOffset();
				final int end = javadocRange.getLength() + start - 1;
				String javadocSource = source.substring(start, end);
				assertTrue("Wrong javadoc", javadocSource.indexOf("field f") != -1);
			} else if ("foo".equals(elementName)) {
				assertNotNull("No javadoc source range", javadocRange);
				final int start = javadocRange.getOffset();
				final int end = javadocRange.getLength() + start - 1;
				String javadocSource = source.substring(start, end);
				assertTrue("Wrong javadoc", javadocSource.indexOf("method foo") != -1);
			} else if ("A".equals(elementName)) {
				assertNotNull("No javadoc source range", javadocRange);
				final int start = javadocRange.getOffset();
				final int end = javadocRange.getLength() + start - 1;
				String javadocSource = source.substring(start, end);
				assertTrue("Wrong javadoc", javadocSource.indexOf("member type A") != -1);
			} else if ("X".equals(elementName)) {
				// need treatment for the two constructors
				assertTrue("Not an IMethod", element instanceof IMethod);
				IMethod method = (IMethod) element;
				switch(method.getNumberOfParameters()) {
					case 0 :
						assertNull("Has a javadoc source range", javadocRange);
						break;
					case 1:
						assertNotNull("No javadoc source range", javadocRange);
						final int start = javadocRange.getOffset();
						final int end = javadocRange.getLength() + start - 1;
						String javadocSource = source.substring(start, end);
						assertTrue("Wrong javadoc", javadocSource.indexOf("constructor") != -1);
				}
			} else if ("f3".equals(elementName)) {
				assertNotNull("No javadoc source range", javadocRange);
				final int start = javadocRange.getOffset();
				final int end = javadocRange.getLength() + start - 1;
				String javadocSource = source.substring(start, end);
				assertTrue("Wrong javadoc", javadocSource.indexOf("Real") != -1);
			} else if ("f2".equals(elementName)) {
				assertNull("Has a javadoc source range", javadocRange);
			} else if ("foo2".equals(elementName)) {
				assertNull("Has a javadoc source range", javadocRange);
			} else if ("B".equals(elementName)) {
				assertNull("Has a javadoc source range", javadocRange);
			}
		}
	} finally {
		attachSource(root, null, null); // detach source
	}
}
/**
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=242029
 */
public void testRootPath13() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(this.getFile("/AttachSourceTests/test7.jar"));
	attachSource(root, "/AttachSourceTests/test7src/", null);
	
	IClassFile cf = root.getPackageFragment("p1.p2").getClassFile("X.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package p1.p2;\n" +
		"public class X {\n" +
		"}",
		cf.getSource());
	cf = root.getPackageFragment("tests.p1").getClassFile("TestforX.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package tests.p1;\n" +
		"public class TestforX {\n" +
		"}",
		cf.getSource());
	attachSource(root, null, null); // detach source
}
/**
 * @test bug 153133: [model] toggle breakpoint in constructor creates a class load breakpoint
 * @see "http://bugs.eclipse.org/bugs/show_bug.cgi?id=153133"
 */
public void testBug153133() throws JavaModelException {
	IPackageFragmentRoot root = this.currentProject.getPackageFragmentRoot(getFile("/AttachSourceTests/b153133.jar"));
	assertTrue("Root doesn't exist", root.exists());

	try {
		// Get class file type from jar
		IClassFile cf = root.getPackageFragment("test").getClassFile("Test.class");
		assertNotNull(cf);
		final String source = cf.getSource();
		assertNotNull("No source", source);
		IJavaElement[] children = cf.getChildren();
		assertEquals("Wrong number of children", 1, children.length);
		IJavaElement element = children[0];
		assertTrue("Not a type", element instanceof IType);
		IType type = (IType) element;
		IJavaElement[] members = type.getChildren();
		final int length = members.length;
		assertEquals("Wrong number", 7, length);

		// Need to get type members constructors
		for (int i = 0; i < length; i++) {
			assertTrue(members[i] instanceof IMember);
			if (((IMember)members[i]).getElementType() == IJavaElement.TYPE) {
				IType typeMember = (IType) members[i];
				String typeName = typeMember.getElementName();
				IMethod[] methods = typeMember.getMethods();
				assertEquals("Expected only one constructor defined in type "+typeName, 1, methods.length);
				// Verify that source range is valid
				assertTrue("Expected a constructor instead of a method in type "+typeName, methods[0].isConstructor());
				IMethod constructor = methods[0];
				ISourceRange sourceRange = constructor.getSourceRange();
				assertTrue("Constructor "+constructor.getElementName()+" has invalid offset: "+sourceRange, sourceRange.getOffset() >= 0);
				assertTrue("Constructor "+constructor.getElementName()+" has invalid length: "+sourceRange, sourceRange.getLength() > 0);
			}
		}
	} finally {
		removeClasspathEntry(new Path("/JavaSearchBugs/lib/b148215.jar"));
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=267046
public void test267046() throws JavaModelException {
	IJavaProject project = getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/267046.jar"));
	attachSource(root, "/AttachSourceTests/267046_src.zip", null);

	IClassFile cf = root.getPackageFragment("test").getClassFile("Foo.class");
	assertSourceEquals(
		"Unexpected source for class file",
		"package test;\n" + 
		"\n" + 
		"public class Foo {\n" + 
		"	public static class Bar<A, B> {\n" + 
		"	}\n" + 
		"\n" + 
		"	public static void gotchaFunc1(Bar<byte[], Object> bar) {\n" + 
		"	}\n" + 
		"	public static void gotchaFunc2(Bar<boolean[], Object> bar) {\n" + 
		"	}\n" + 
		"	public static void gotchaFunc3(Bar<char[], Object> bar) {\n" + 
		"	}\n" + 
		"	public static void gotchaFunc4(Bar<double[], Object> bar) {\n" + 
		"	}\n" + 
		"	public static void gotchaFunc5(Bar<float[], Object> bar) {\n" + 
		"	}\n" + 
		"	public static void gotchaFunc6(Bar<int[], Object> bar) {\n" + 
		"	}\n" + 
		"	public static void gotchaFunc7(Bar<long[], Object> bar) {\n" + 
		"	}\n" + 
		"	public static void gotchaFunc8(Bar<short[], Object> bar) {\n" + 
		"	}\n" + 
		"}\n",
		cf.getSource());
	IType type = cf.getType();
	IMethod[] methods = type.getMethods();
	for (int i = 0, max = methods.length; i < max; i++) {
		IMethod currentMethod = methods[i];
		if (currentMethod.isConstructor()) continue;
		assertNotNull("No source for method : " + currentMethod.getElementName(), currentMethod.getSource());
	}

	attachSource(root, null, null); // detach source
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=88265
// Test to ensure availability and correctness of API SourceRange
public void test88265 () {
	org.eclipse.jdt.core.SourceRange one = new org.eclipse.jdt.core.SourceRange(10, 7);
	org.eclipse.jdt.core.SourceRange two = new org.eclipse.jdt.core.SourceRange(9, 13);
	assertTrue(two.getOffset() == 9);
	assertTrue(two.getLength() == 13);
	assertFalse(one.equals(two));
	SourceRange three = new org.eclipse.jdt.core.SourceRange(10, 7);
	assertTrue(one.equals(three));
	assertTrue(SourceRange.isAvailable(one));
	assertFalse(SourceRange.isAvailable(null));
	assertFalse(SourceRange.isAvailable(new SourceRange(-1, 0)));
}
/*
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=285230
 */
public void testClassFileBuffer() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/innerClasses.jar"));
	attachSource(root, "/AttachSourceTests/innerClassessrc.zip", null);
	IPackageFragment fragment = root.getPackageFragment("inner");

	IClassFile classFile = fragment.getClassFile("X$V.class");
	IBuffer buffer = classFile.getBuffer();
	classFile = fragment.getClassFile("X.class");
	IBuffer buffer2 = classFile.getBuffer();
	assertTrue("Same buffer is not reused", buffer2 == buffer);
	attachSource(root, null, null); // detach source
}
/**
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=242029
 */
public void testConstructorAccess() throws JavaModelException {
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	IPackageFragmentRoot root = project.getPackageFragmentRoot(getFile("/AttachSourceTests/generic.jar"));
	attachSource(root, "/AttachSourceTests/genericsrc.zip", null);
	
	IClassFile cf = root.getPackageFragment("").getClassFile("Container$Inner.class");
	final IType type = cf.getType();
	final IMethod[] methods = type.getMethods();
	assertEquals("wrong size", 1, methods.length);
	assertTrue("Not a constructor", methods[0].isConstructor());
	assertSourceEquals(
		"Unexpected source for generic constructor",
		"Inner(String st, Class<S> s) {\n" + 
		"			super();\n" + 
		"		}",
		methods[0].getSource());
	attachSource(root, null, null); // detach source
}
/**
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=336046
 */
public void testBug336046() throws Exception {
	String externalSourceLocation = getExternalFolder() + File.separator + "336046src";
	IJavaProject project = this.getJavaProject("/AttachSourceTests");
	Hashtable javaCoreOptions = JavaCore.getOptions();
	IJavaProject importedProject = null;
	try {
		
		String classpathContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
		"<classpath>\n" +
		"    <classpathentry kind=\"lib\" path=\"attach.jar\"/>\n" +
		"    <classpathentry kind=\"lib\" path=\"attach2.jar\"/>\n" +
		"    <classpathentry kind=\"lib\" path=\"test.jar\"/>\n" + 
		"    <classpathentry kind=\"lib\" path=\"update.jar\"/>\n" +   
		"    <classpathentry kind=\"lib\" path=\"full.jar\"/>\n" +
		"    <classpathentry kind=\"lib\" path=\"test2.jar\"/>\n" +  
		"    <classpathentry kind=\"lib\" path=\"test4.jar\"/>  \n" +  
		"    <classpathentry kind=\"lib\" path=\"test5.jar\"/>  \n" +  
		"    <classpathentry kind=\"lib\" path=\"test6.jar\"/>\n" +
		"    <classpathentry kind=\"lib\" path=\"test7.jar\"/>\n" +
		"    <classpathentry kind=\"lib\" path=\"267046.jar\"/>\n" +
		"    <classpathentry kind=\"lib\" path=\"bug336046.jar\" sourcepath=\"" + externalSourceLocation + "\"/>\n" +
		"    <classpathentry kind=\"lib\" path=\"lib\"/>\n" +
		"    <classpathentry kind=\"src\" path=\"src\" output=\"src\"/>\n" +
		"    <classpathentry kind=\"var\" path=\"JCL_LIB\"/>\n" +
		"    <classpathentry kind=\"output\" path=\"bin\"/>\n" +
		"</classpath>";
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		
		IPath sourceLocation = project.getProject().getLocation();
		IPath destination = new Path(getExternalFolder()).append("ImportedProject");
		String classpathLocation = destination.append(".classpath").toString();
		File srcFolder = destination.append("336046src").toFile();
		copyDirectory(new File(sourceLocation.toString()), new File(destination.toString()));
		project.getProject().close(null);

		FileOutputStream fos = new FileOutputStream(classpathLocation);
		fos.write(classpathContent.getBytes());
		assertTrue(srcFolder.renameTo(new File(getExternalFolder() + File.separator + "336046src")));
		fos.close();

		IProject newProject = workspace.getRoot().getProject("ImportedProject");
		URI uri=  URIUtil.toURI(destination);
		IProjectDescription desc = workspace.newProjectDescription(newProject.getName());
		desc.setLocationURI(uri);
		newProject.create(desc, null);
		if (!newProject.isOpen()) {
			newProject.open(null);
		}
		importedProject = JavaCore.create(newProject);
		importedProject.setOptions(project.getOptions(false));

		((JavaProject)importedProject).resolveClasspath(importedProject.getRawClasspath());
		IFolder linkedFolder = ExternalFoldersManager.getExternalFoldersManager().getFolder(new Path(getExternalFolder() + File.separator + "336046src"));
		assertNotNull(linkedFolder);
	}
	finally {
		if (importedProject != null)
			importedProject.getProject().delete(true, true, null);
		project.getProject().open(null);
		JavaCore.setOptions(javaCoreOptions);
	}
}
}

Back to the top