summaryrefslogtreecommitdiff
path: root/external/ikvm/runtime/openjdk/java.lang.cs
blob: 7def9892efb47a6f17ef27dbc3a92f5be3973cda (plain)
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
/*
  Copyright (C) 2007-2013 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Threading;
using IKVM.Internal;

static class Java_java_lang_Class
{
	public static java.lang.Class forName0(string name, bool initialize, java.lang.ClassLoader loader)
	{
#if FIRST_PASS
		return null;
#else
		//Console.WriteLine("forName: " + name + ", loader = " + loader);
		TypeWrapper tw = null;
		if (name.IndexOf(',') > 0)
		{
			// we essentially require full trust before allowing arbitrary types to be loaded,
			// hence we do the "createClassLoader" permission check
			java.lang.SecurityManager sm = java.lang.System.getSecurityManager();
			if (sm != null)
				sm.checkPermission(new java.lang.RuntimePermission("createClassLoader"));
			Type type = Type.GetType(name);
			if (type != null)
			{
				tw = ClassLoaderWrapper.GetWrapperFromType(type);
			}
			if (tw == null)
			{
				java.lang.Throwable.suppressFillInStackTrace = true;
				throw new java.lang.ClassNotFoundException(name);
			}
		}
		else
		{
			try
			{
				ClassLoaderWrapper classLoaderWrapper = ClassLoaderWrapper.GetClassLoaderWrapper(loader);
				tw = classLoaderWrapper.LoadClassByDottedName(name);
			}
			catch (ClassNotFoundException x)
			{
				java.lang.Throwable.suppressFillInStackTrace = true;
				throw new java.lang.ClassNotFoundException(x.Message);
			}
			catch (ClassLoadingException x)
			{
				throw x.InnerException;
			}
			catch (RetargetableJavaException x)
			{
				throw x.ToJava();
			}
		}
		if (initialize && !tw.IsArray)
		{
			try
			{
				tw.Finish();
			}
			catch (RetargetableJavaException x)
			{
				throw x.ToJava();
			}
			tw.RunClassInit();
		}
		return tw.ClassObject;
#endif
	}

	public static bool isInstance(java.lang.Class thisClass, object obj)
	{
		return TypeWrapper.FromClass(thisClass).IsInstance(obj);
	}

	public static bool isAssignableFrom(java.lang.Class thisClass, java.lang.Class otherClass)
	{
#if !FIRST_PASS
		if (otherClass == null)
		{
			throw new java.lang.NullPointerException();
		}
#endif
		return TypeWrapper.FromClass(otherClass).IsAssignableTo(TypeWrapper.FromClass(thisClass));
	}

	public static bool isInterface(java.lang.Class thisClass)
	{
		return TypeWrapper.FromClass(thisClass).IsInterface;
	}

	public static bool isArray(java.lang.Class thisClass)
	{
		return TypeWrapper.FromClass(thisClass).IsArray;
	}

	public static bool isPrimitive(java.lang.Class thisClass)
	{
		return TypeWrapper.FromClass(thisClass).IsPrimitive;
	}

	public static string getName0(java.lang.Class thisClass)
	{
		TypeWrapper tw = TypeWrapper.FromClass(thisClass);
		if (tw.IsPrimitive)
		{
			if (tw == PrimitiveTypeWrapper.BYTE)
			{
				return "byte";
			}
			else if (tw == PrimitiveTypeWrapper.CHAR)
			{
				return "char";
			}
			else if (tw == PrimitiveTypeWrapper.DOUBLE)
			{
				return "double";
			}
			else if (tw == PrimitiveTypeWrapper.FLOAT)
			{
				return "float";
			}
			else if (tw == PrimitiveTypeWrapper.INT)
			{
				return "int";
			}
			else if (tw == PrimitiveTypeWrapper.LONG)
			{
				return "long";
			}
			else if (tw == PrimitiveTypeWrapper.SHORT)
			{
				return "short";
			}
			else if (tw == PrimitiveTypeWrapper.BOOLEAN)
			{
				return "boolean";
			}
			else if (tw == PrimitiveTypeWrapper.VOID)
			{
				return "void";
			}
		}
		return tw.Name;
	}

	public static string getSigName(java.lang.Class thisClass)
	{
		return TypeWrapper.FromClass(thisClass).SigName;
	}

	public static java.lang.ClassLoader getClassLoader0(java.lang.Class thisClass)
	{
		return TypeWrapper.FromClass(thisClass).GetClassLoader().GetJavaClassLoader();
	}

	public static java.lang.Class getSuperclass(java.lang.Class thisClass)
	{
		TypeWrapper super = TypeWrapper.FromClass(thisClass).BaseTypeWrapper;
		return super != null ? super.ClassObject : null;
	}

	public static java.lang.Class[] getInterfaces(java.lang.Class thisClass)
	{
#if FIRST_PASS
		return null;
#else
		TypeWrapper[] ifaces = TypeWrapper.FromClass(thisClass).Interfaces;
		java.lang.Class[] interfaces = new java.lang.Class[ifaces.Length];
		for (int i = 0; i < ifaces.Length; i++)
		{
			interfaces[i] = ifaces[i].ClassObject;
		}
		return interfaces;
#endif
	}

	public static java.lang.Class getComponentType(java.lang.Class thisClass)
	{
		TypeWrapper tw = TypeWrapper.FromClass(thisClass);
		return tw.IsArray ? tw.ElementTypeWrapper.ClassObject : null;
	}

	public static int getModifiers(java.lang.Class thisClass)
	{
		// the 0x7FFF mask comes from JVM_ACC_WRITTEN_FLAGS in hotspot\src\share\vm\utilities\accessFlags.hpp
		// masking out ACC_SUPER comes from instanceKlass::compute_modifier_flags() in hotspot\src\share\vm\oops\instanceKlass.cpp
		const int mask = 0x7FFF & (int)~IKVM.Attributes.Modifiers.Super;
		return (int)TypeWrapper.FromClass(thisClass).ReflectiveModifiers & mask;
	}

	public static object[] getSigners(java.lang.Class thisClass)
	{
#if FIRST_PASS
		return null;
#else
		return thisClass.signers;
#endif
	}

	public static void setSigners(java.lang.Class thisClass, object[] signers)
	{
#if !FIRST_PASS
		thisClass.signers = signers;
#endif
	}

	public static object[] getEnclosingMethod0(java.lang.Class thisClass)
	{
		try
		{
			TypeWrapper tw = TypeWrapper.FromClass(thisClass);
			tw.Finish();
			string[] enc = tw.GetEnclosingMethod();
			if (enc == null)
			{
				return null;
			}
			return new object[] { tw.GetClassLoader().LoadClassByDottedName(enc[0]).ClassObject, enc[1], enc[2] == null ? null : enc[2].Replace('.', '/') };
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
	}

	public static java.lang.Class getDeclaringClass(java.lang.Class thisClass)
	{
		try
		{
			TypeWrapper wrapper = TypeWrapper.FromClass(thisClass);
			wrapper.Finish();
			TypeWrapper decl = wrapper.DeclaringTypeWrapper;
			if (decl == null)
			{
				return null;
			}
			if (!decl.IsAccessibleFrom(wrapper))
			{
				throw new IllegalAccessError(string.Format("tried to access class {0} from class {1}", decl.Name, wrapper.Name));
			}
			decl.Finish();
			if (Array.IndexOf(decl.InnerClasses, wrapper) == -1)
			{
				throw new IncompatibleClassChangeError(string.Format("{0} and {1} disagree on InnerClasses attribute", decl.Name, wrapper.Name));
			}
			return decl.ClassObject;
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
	}

	public static java.security.ProtectionDomain getProtectionDomain0(java.lang.Class thisClass)
	{
#if FIRST_PASS
		return null;
#else
		TypeWrapper wrapper = TypeWrapper.FromClass(thisClass);
		while (wrapper.IsArray)
		{
			wrapper = wrapper.ElementTypeWrapper;
		}
		java.security.ProtectionDomain pd = wrapper.ClassObject.pd;
		if (pd == null)
		{
			// The protection domain for statically compiled code is created lazily (not at java.lang.Class creation time),
			// to work around boot strap issues.
			AssemblyClassLoader acl = wrapper.GetClassLoader() as AssemblyClassLoader;
			if (acl != null)
			{
				pd = acl.GetProtectionDomain();
			}
		}
		return pd;
#endif
	}

	public static void setProtectionDomain0(java.lang.Class thisClass, java.security.ProtectionDomain pd)
	{
#if !FIRST_PASS
		thisClass.pd = pd;
#endif
	}

	public static java.lang.Class getPrimitiveClass(string name)
	{
		// note that this method isn't used anymore (because it is an intrinsic (during core class library compilation))
		// it still remains for compat because it might be invoked through reflection by evil code
		switch (name)
		{
			case "byte":
				return PrimitiveTypeWrapper.BYTE.ClassObject;
			case "char":
				return PrimitiveTypeWrapper.CHAR.ClassObject;
			case "double":
				return PrimitiveTypeWrapper.DOUBLE.ClassObject;
			case "float":
				return PrimitiveTypeWrapper.FLOAT.ClassObject;
			case "int":
				return PrimitiveTypeWrapper.INT.ClassObject;
			case "long":
				return PrimitiveTypeWrapper.LONG.ClassObject;
			case "short":
				return PrimitiveTypeWrapper.SHORT.ClassObject;
			case "boolean":
				return PrimitiveTypeWrapper.BOOLEAN.ClassObject;
			case "void":
				return PrimitiveTypeWrapper.VOID.ClassObject;
			default:
				throw new ArgumentException(name);
		}
	}

	public static string getGenericSignature(java.lang.Class thisClass)
	{
		TypeWrapper tw = TypeWrapper.FromClass(thisClass);
		tw.Finish();
		return tw.GetGenericSignature();
	}

	internal static object AnnotationsToMap(ClassLoaderWrapper loader, object[] objAnn)
	{
#if FIRST_PASS
		return null;
#else
		java.util.LinkedHashMap map = new java.util.LinkedHashMap();
		if (objAnn != null)
		{
			foreach (object obj in objAnn)
			{
				java.lang.annotation.Annotation a = obj as java.lang.annotation.Annotation;
				if (a != null)
				{
					map.put(a.annotationType(), FreezeOrWrapAttribute(a));
				}
				else if (obj is IKVM.Attributes.DynamicAnnotationAttribute)
				{
					a = (java.lang.annotation.Annotation)JVM.NewAnnotation(loader.GetJavaClassLoader(), ((IKVM.Attributes.DynamicAnnotationAttribute)obj).Definition);
					if (a != null)
					{
						map.put(a.annotationType(), a);
					}
				}
			}
		}
		return map;
#endif
	}

#if !FIRST_PASS
	internal static java.lang.annotation.Annotation FreezeOrWrapAttribute(java.lang.annotation.Annotation ann)
	{
		ikvm.@internal.AnnotationAttributeBase attr = ann as ikvm.@internal.AnnotationAttributeBase;
		if (attr != null)
		{
#if DONT_WRAP_ANNOTATION_ATTRIBUTES
			attr.freeze();
#else
			// freeze to make sure the defaults are set
			attr.freeze();
			ann = sun.reflect.annotation.AnnotationParser.annotationForMap(attr.annotationType(), attr.getValues());
#endif
		}
		return ann;
	}
#endif

	public static object getDeclaredAnnotationsImpl(java.lang.Class thisClass)
	{
#if FIRST_PASS
		return null;
#else
		TypeWrapper wrapper = TypeWrapper.FromClass(thisClass);
		try
		{
			wrapper.Finish();
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
		return AnnotationsToMap(wrapper.GetClassLoader(), wrapper.GetDeclaredAnnotations());
#endif
	}

	public static object getDeclaredFields0(java.lang.Class thisClass, bool publicOnly)
	{
#if FIRST_PASS
		return null;
#else
		Profiler.Enter("Class.getDeclaredFields0");
		try
		{
			TypeWrapper wrapper = TypeWrapper.FromClass(thisClass);
			// we need to finish the type otherwise all fields will not be in the field map yet
			wrapper.Finish();
			FieldWrapper[] fields = wrapper.GetFields();
			List<java.lang.reflect.Field> list = new List<java.lang.reflect.Field>();
			for (int i = 0; i < fields.Length; i++)
			{
				if (fields[i].IsHideFromReflection)
				{
					// skip
				}
				else if (publicOnly && !fields[i].IsPublic)
				{
					// caller is only asking for public field, so we don't return this non-public field
				}
				else
				{
					list.Add((java.lang.reflect.Field)fields[i].ToField(false, i));
				}
			}
			return list.ToArray();
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
		finally
		{
			Profiler.Leave("Class.getDeclaredFields0");
		}
#endif
	}

	public static object getDeclaredMethods0(java.lang.Class thisClass, bool publicOnly)
	{
#if FIRST_PASS
		return null;
#else
		Profiler.Enter("Class.getDeclaredMethods0");
		try
		{
			TypeWrapper wrapper = TypeWrapper.FromClass(thisClass);
			wrapper.Finish();
			if (wrapper.HasVerifyError)
			{
				// TODO we should get the message from somewhere
				throw new VerifyError();
			}
			if (wrapper.HasClassFormatError)
			{
				// TODO we should get the message from somewhere
				throw new ClassFormatError(wrapper.Name);
			}
			MethodWrapper[] methods = wrapper.GetMethods();
			List<java.lang.reflect.Method> list = new List<java.lang.reflect.Method>();
			for (int i = 0; i < methods.Length; i++)
			{
				// we don't want to expose "hideFromReflection" methods (one reason is that it would
				// mess up the serialVersionUID computation)
				if (!methods[i].IsHideFromReflection
					&& methods[i].Name != "<clinit>" && methods[i].Name != "<init>"
					&& (!publicOnly || methods[i].IsPublic))
				{
					list.Add((java.lang.reflect.Method)methods[i].ToMethodOrConstructor(false));
				}
			}
			return list.ToArray();
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
		finally
		{
			Profiler.Leave("Class.getDeclaredMethods0");
		}
#endif
	}

	public static object getDeclaredConstructors0(java.lang.Class thisClass, bool publicOnly)
	{
#if FIRST_PASS
		return null;
#else
		Profiler.Enter("Class.getDeclaredConstructors0");
		try
		{
			TypeWrapper wrapper = TypeWrapper.FromClass(thisClass);
			wrapper.Finish();
			if (wrapper.HasVerifyError)
			{
				// TODO we should get the message from somewhere
				throw new VerifyError();
			}
			if (wrapper.HasClassFormatError)
			{
				// TODO we should get the message from somewhere
				throw new ClassFormatError(wrapper.Name);
			}
			MethodWrapper[] methods = wrapper.GetMethods();
			List<java.lang.reflect.Constructor> list = new List<java.lang.reflect.Constructor>();
			for (int i = 0; i < methods.Length; i++)
			{
				// we don't want to expose "hideFromReflection" methods (one reason is that it would
				// mess up the serialVersionUID computation)
				if (!methods[i].IsHideFromReflection
					&& methods[i].Name == "<init>"
					&& (!publicOnly || methods[i].IsPublic))
				{
					list.Add((java.lang.reflect.Constructor)methods[i].ToMethodOrConstructor(false));
				}
			}
			return list.ToArray();
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
		finally
		{
			Profiler.Leave("Class.getDeclaredConstructors0");
		}
#endif
	}

	public static java.lang.Class[] getDeclaredClasses0(java.lang.Class thisClass)
	{
#if FIRST_PASS
		return null;
#else
		try
		{
			TypeWrapper wrapper = TypeWrapper.FromClass(thisClass);
			// NOTE to get at the InnerClasses we need to finish the type
			wrapper.Finish();
			TypeWrapper[] wrappers = wrapper.InnerClasses;
			java.lang.Class[] innerclasses = new java.lang.Class[wrappers.Length];
			for (int i = 0; i < innerclasses.Length; i++)
			{
				if (wrappers[i].IsUnloadable)
				{
					throw new java.lang.NoClassDefFoundError(wrappers[i].Name);
				}
				if (!wrappers[i].IsAccessibleFrom(wrapper))
				{
					throw new IllegalAccessError(string.Format("tried to access class {0} from class {1}", wrappers[i].Name, wrapper.Name));
				}
				wrappers[i].Finish();
				innerclasses[i] = wrappers[i].ClassObject;
			}
			return innerclasses;
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
#endif
	}

	public static bool desiredAssertionStatus0(java.lang.Class clazz)
	{
		return IKVM.Runtime.Assertions.IsEnabled(TypeWrapper.FromClass(clazz));
	}
}

static class Java_java_lang_ClassLoader
{
	public static java.net.URL getBootstrapResource(string name)
	{
		foreach (java.net.URL url in ClassLoaderWrapper.GetBootstrapClassLoader().GetResources(name))
		{
			return url;
		}
		return null;
	}

	public static java.util.Enumeration getBootstrapResources(string name)
	{
#if FIRST_PASS
		return null;
#else
		return new ikvm.runtime.EnumerationWrapper(ClassLoaderWrapper.GetBootstrapClassLoader().GetResources(name));
#endif
	}

	public static java.lang.Class defineClass0(java.lang.ClassLoader thisClassLoader, string name, byte[] b, int off, int len, java.security.ProtectionDomain pd)
	{
		return defineClass1(thisClassLoader, name, b, off, len, pd, null);
	}

	public static java.lang.Class defineClass1(java.lang.ClassLoader thisClassLoader, string name, byte[] b, int off, int len, java.security.ProtectionDomain pd, string source)
	{
		// it appears the source argument is only used for trace messages in HotSpot. We'll just ignore it for now.
		Profiler.Enter("ClassLoader.defineClass");
		try
		{
			try
			{
				ClassLoaderWrapper classLoaderWrapper = ClassLoaderWrapper.GetClassLoaderWrapper(thisClassLoader);
				ClassFileParseOptions cfp = ClassFileParseOptions.LineNumberTable;
				if (classLoaderWrapper.EmitDebugInfo)
				{
					cfp |= ClassFileParseOptions.LocalVariableTable;
				}
				if (classLoaderWrapper.RelaxedClassNameValidation)
				{
					cfp |= ClassFileParseOptions.RelaxedClassNameValidation;
				}
				ClassFile classFile = new ClassFile(b, off, len, name, cfp);
				if (name != null && classFile.Name != name)
				{
#if !FIRST_PASS
					throw new java.lang.NoClassDefFoundError(name + " (wrong name: " + classFile.Name + ")");
#endif
				}
				TypeWrapper type = classLoaderWrapper.DefineClass(classFile, pd);
				return type.ClassObject;
			}
			catch (RetargetableJavaException x)
			{
				throw x.ToJava();
			}
		}
		finally
		{
			Profiler.Leave("ClassLoader.defineClass");
		}
	}

	public static java.lang.Class defineClass2(java.lang.ClassLoader thisClassLoader, string name, java.nio.ByteBuffer bb, int off, int len, java.security.ProtectionDomain pd, string source)
	{
#if FIRST_PASS
		return null;
#else
		byte[] buf = new byte[bb.remaining()];
		bb.get(buf);
		return defineClass1(thisClassLoader, name, buf, 0, buf.Length, pd, source);
#endif
	}

	public static void resolveClass0(java.lang.ClassLoader thisClassLoader, java.lang.Class clazz)
	{
		// no-op
	}

	public static java.lang.Class findBootstrapClass(java.lang.ClassLoader thisClassLoader, string name)
	{
#if FIRST_PASS
		return null;
#else
		TypeWrapper tw;
		try
		{
			tw = ClassLoaderWrapper.GetBootstrapClassLoader().LoadClassByDottedNameFast(name);
		}
		catch (RetargetableJavaException x)
		{
			throw x.ToJava();
		}
		return tw != null ? tw.ClassObject : null;
#endif
	}

	public static java.lang.Class findLoadedClass0(java.lang.ClassLoader thisClassLoader, string name)
	{
		if (name == null)
		{
			return null;
		}
		ClassLoaderWrapper loader = ClassLoaderWrapper.GetClassLoaderWrapper(thisClassLoader);
		TypeWrapper tw = loader.FindLoadedClass(name);
		return tw != null ? tw.ClassObject : null;
	}

	public static object retrieveDirectives()
	{
		return IKVM.Runtime.Assertions.RetrieveDirectives();
	}
}

static class Java_java_lang_ClassLoader_00024NativeLibrary
{
	public static void load(object thisNativeLibrary, string name)
	{
#if !FIRST_PASS
		if (VirtualFileSystem.IsVirtualFS(name))
		{
			// we fake success for native libraries loaded from VFS
			((java.lang.ClassLoader.NativeLibrary)thisNativeLibrary).handle = -1;
		}
		else
		{
			doLoad(thisNativeLibrary, name);
		}
#endif
	}

#if !FIRST_PASS
	// we don't want to inline this method, because that would needlessly cause IKVM.Runtime.JNI.dll to be loaded when loading a fake native library from VFS
	[MethodImpl(MethodImplOptions.NoInlining)]
	[SecuritySafeCritical]
	private static void doLoad(object thisNativeLibrary, string name)
	{
		java.lang.ClassLoader.NativeLibrary lib = (java.lang.ClassLoader.NativeLibrary)thisNativeLibrary;
		lib.handle = IKVM.Runtime.JniHelper.LoadLibrary(name, TypeWrapper.FromClass(lib.fromClass).GetClassLoader());
	}
#endif

	public static long find(object thisNativeLibrary, string name)
	{
		// TODO
		throw new NotImplementedException();
	}

	[SecuritySafeCritical]
	public static void unload(object thisNativeLibrary)
	{
#if !FIRST_PASS
		java.lang.ClassLoader.NativeLibrary lib = (java.lang.ClassLoader.NativeLibrary)thisNativeLibrary;
		long handle = Interlocked.Exchange(ref lib.handle, 0);
		if (handle != 0)
		{
			IKVM.Runtime.JniHelper.UnloadLibrary(handle, TypeWrapper.FromClass(lib.fromClass).GetClassLoader());
		}
#endif
	}
}

static class Java_java_lang_Compiler
{
	public static void initialize()
	{
	}

	public static void registerNatives()
	{
	}

	public static bool compileClass(object clazz)
	{
		return false;
	}

	public static bool compileClasses(string str)
	{
		return false;
	}

	public static object command(object any)
	{
		return null;
	}

	public static void enable()
	{
	}

	public static void disable()
	{
	}
}

static class Java_java_lang_Double
{
	public static long doubleToRawLongBits(double value)
	{
		IKVM.Runtime.DoubleConverter converter = new IKVM.Runtime.DoubleConverter();
		return IKVM.Runtime.DoubleConverter.ToLong(value, ref converter);
	}

	public static double longBitsToDouble(long bits)
	{
		IKVM.Runtime.DoubleConverter converter = new IKVM.Runtime.DoubleConverter();
		return IKVM.Runtime.DoubleConverter.ToDouble(bits, ref converter);
	}
}

static class Java_java_lang_Float
{
	public static int floatToRawIntBits(float value)
	{
		IKVM.Runtime.FloatConverter converter = new IKVM.Runtime.FloatConverter();
		return IKVM.Runtime.FloatConverter.ToInt(value, ref converter);
	}

	public static float intBitsToFloat(int bits)
	{
		IKVM.Runtime.FloatConverter converter = new IKVM.Runtime.FloatConverter();
		return IKVM.Runtime.FloatConverter.ToFloat(bits, ref converter);
	}
}

static class Java_java_lang_Package
{
	private static Dictionary<string, string> systemPackages;

	private static void LazyInitSystemPackages()
	{
		if (systemPackages == null)
		{
			Dictionary<string, string> dict = new Dictionary<string, string>();
			string path = VirtualFileSystem.GetAssemblyResourcesPath(JVM.CoreAssembly) + "resources.jar";
			foreach (string pkg in ClassLoaderWrapper.GetBootstrapClassLoader().GetPackages())
			{
				dict[pkg.Replace('.', '/') + "/"] = path;
			}
			Interlocked.CompareExchange(ref systemPackages, dict, null);
		}
	}

	public static string getSystemPackage0(string name)
	{
		LazyInitSystemPackages();
		string path;
		systemPackages.TryGetValue(name, out path);
		return path;
	}

	public static string[] getSystemPackages0()
	{
		LazyInitSystemPackages();
		string[] pkgs = new string[systemPackages.Count];
		systemPackages.Keys.CopyTo(pkgs, 0);
		return pkgs;
	}
}

static class Java_java_lang_ProcessEnvironment
{
	public static string environmentBlock()
	{
		StringBuilder sb = new StringBuilder();
		foreach (System.Collections.DictionaryEntry de in Environment.GetEnvironmentVariables())
		{
			sb.Append(de.Key).Append('=').Append(de.Value).Append('\u0000');
		}
		if (sb.Length == 0)
		{
			sb.Append('\u0000');
		}
		sb.Append('\u0000');
		return sb.ToString();
	}
}

static class Java_java_lang_Runtime
{
	public static int availableProcessors(object thisRuntime)
	{
		return Environment.ProcessorCount;
	}

	public static long freeMemory(object thisRuntime)
	{
		// TODO figure out if there is anything meaningful we can return here
		return 10 * 1024 * 1024;
	}

	public static long totalMemory(object thisRuntime)
	{
		// NOTE this really is a bogus number, but we have to return something
		return GC.GetTotalMemory(false) + freeMemory(thisRuntime);
	}

	public static long maxMemory(object thisRuntime)
	{
		// spec says: If there is no inherent limit then the value Long.MAX_VALUE will be returned.
		return Int64.MaxValue;
	}

	public static void gc(object thisRuntime)
	{
		GC.Collect();
	}

	public static void traceInstructions(object thisRuntime, bool on)
	{
	}

	public static void traceMethodCalls(object thisRuntime, bool on)
	{
	}

	public static void runFinalization0()
	{
		GC.WaitForPendingFinalizers();
	}
}

static class Java_java_lang_SecurityManager
{
	// this field is set by code in the JNI assembly itself,
	// to prevent having to load the JNI assembly when it isn't used.
	internal static volatile Assembly jniAssembly;

	public static java.lang.Class[] getClassContext(object thisSecurityManager)
	{
#if FIRST_PASS
		return null;
#else
		List<java.lang.Class> stack = new List<java.lang.Class>();
		StackTrace trace = new StackTrace();
		for (int i = 0; i < trace.FrameCount; i++)
		{
			StackFrame frame = trace.GetFrame(i);
			MethodBase method = frame.GetMethod();
			Type type = method.DeclaringType;
			// NOTE these checks should be the same as the ones in Reflection.getCallerClass
			if (Java_sun_reflect_Reflection.IsHideFromJava(method)
				|| type == null
				|| type.Assembly == typeof(object).Assembly
				|| type.Assembly == typeof(Java_java_lang_SecurityManager).Assembly
				|| type.Assembly == jniAssembly
				|| type == typeof(java.lang.reflect.Constructor)
				|| type == typeof(java.lang.reflect.Method))
			{
				continue;
			}
			if (type == typeof(java.lang.SecurityManager))
			{
				continue;
			}
			stack.Add(ClassLoaderWrapper.GetWrapperFromType(type).ClassObject);
		}
		return stack.ToArray();
#endif
	}

	public static object currentClassLoader0(object thisSecurityManager)
	{
		java.lang.Class currentClass = currentLoadedClass0(thisSecurityManager);
		if (currentClass != null)
		{
			return TypeWrapper.FromClass(currentClass).GetClassLoader().GetJavaClassLoader();
		}
		return null;
	}

	public static int classDepth(object thisSecurityManager, string name)
	{
		throw new NotImplementedException();
	}

	public static int classLoaderDepth0(object thisSecurityManager)
	{
		throw new NotImplementedException();
	}

	public static java.lang.Class currentLoadedClass0(object thisSecurityManager)
	{
		throw new NotImplementedException();
	}
}

static class Java_java_lang_StrictMath
{
	public static double sin(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.sin(d);
#endif
	}

	public static double cos(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.cos(d);
#endif
	}

	public static double tan(double d)
	{
		return fdlibm.tan(d);
	}

	public static double asin(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.asin(d);
#endif
	}

	public static double acos(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.acos(d);
#endif
	}

	public static double atan(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.atan(d);
#endif
	}

	public static double exp(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.exp(d);
#endif
	}

	public static double log(double d)
	{
		// FPU behavior is correct
		return Math.Log(d);
	}

	public static double log10(double d)
	{
		// FPU behavior is correct
		return Math.Log10(d);
	}

	public static double sqrt(double d)
	{
		// FPU behavior is correct
		return Math.Sqrt(d);
	}

	public static double cbrt(double d)
	{
		return fdlibm.cbrt(d);
	}

	public static double IEEEremainder(double f1, double f2)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.IEEEremainder(f1, f2);
#endif
	}

	public static double ceil(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.ceil(d);
#endif
	}

	public static double floor(double d)
	{
		return fdlibm.floor(d);
	}

	public static double atan2(double y, double x)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.atan2(y, x);
#endif
	}

	public static double pow(double x, double y)
	{
		return fdlibm.__ieee754_pow(x, y);
	}

	public static double sinh(double d)
	{
		return Math.Sinh(d);
	}

	public static double cosh(double d)
	{
		return Math.Cosh(d);
	}

	public static double tanh(double d)
	{
		return Math.Tanh(d);
	}

	public static double rint(double d)
	{
#if FIRST_PASS
		return 0;
#else
		return ikvm.@internal.JMath.rint(d);
#endif
	}

	public static double hypot(double a, double b)
	{
		return fdlibm.__ieee754_hypot(a, b);
	}

	public static double expm1(double d)
	{
		return fdlibm.expm1(d);
	}

	public static double log1p(double d)
	{
		return fdlibm.log1p(d);
	}
}

static class Java_java_lang_System
{
	public static void arraycopy(object src, int srcPos, object dest, int destPos, int length)
	{
		IKVM.Runtime.ByteCodeHelper.arraycopy(src, srcPos, dest, destPos, length);
	}
}

static class Java_java_lang_Thread
{
	private static readonly object mainThreadGroup;

#if !FIRST_PASS
	static Java_java_lang_Thread()
	{
		mainThreadGroup = new java.lang.ThreadGroup(java.lang.ThreadGroup.createRootGroup(), "main");
	}
#endif

	public static object getMainThreadGroup()
	{
		return mainThreadGroup;
	}

	// this is called from JniInterface.cs
	internal static void WaitUntilLastJniThread()
	{
#if !FIRST_PASS
		int count = java.lang.Thread.currentThread().isDaemon() ? 0 : 1;
		while (Interlocked.CompareExchange(ref java.lang.Thread.nonDaemonCount[0], 0, 0) > count)
		{
			Thread.Sleep(1);
		}
#endif
	}

	// this is called from JniInterface.cs
	internal static void AttachThreadFromJni(object threadGroup)
	{
#if !FIRST_PASS
		if (threadGroup == null)
		{
			threadGroup = mainThreadGroup;
		}
		if (java.lang.Thread.current == null)
		{
			new java.lang.Thread((java.lang.ThreadGroup)threadGroup);
		}
#endif
	}

	public static java.lang.StackTraceElement[] getStackTrace(StackTrace stack)
	{
#if FIRST_PASS
		return null;
#else
		List<java.lang.StackTraceElement> stackTrace = new List<java.lang.StackTraceElement>();
		ExceptionHelper.ExceptionInfoHelper.Append(stackTrace, stack, 0, true);
		return stackTrace.ToArray();
#endif
	}

	public static object getThreads()
	{
#if FIRST_PASS
		return null;
#else
		return java.security.AccessController.doPrivileged(ikvm.runtime.Delegates.toPrivilegedAction(delegate
		{
			java.lang.ThreadGroup root = (java.lang.ThreadGroup)mainThreadGroup;
			for (; ; )
			{
				java.lang.Thread[] threads = new java.lang.Thread[root.activeCount()];
				if (root.enumerate(threads) == threads.Length)
				{
					return threads;
				}
			}
		}));
#endif
	}
}

static class Java_java_lang_ProcessImpl
{
	public static string mapVfsExecutable(string path)
	{
		if (VirtualFileSystem.IsVirtualFS(path))
		{
			return VirtualFileSystem.MapExecutable(path);
		}
		return path;
	}

	public static int parseCommandString(string cmdstr)
	{
		int pos = cmdstr.IndexOf(' ');
		if (pos == -1)
		{
			return cmdstr.Length;
		}
		if (cmdstr[0] == '"')
		{
			int close = cmdstr.IndexOf('"', 1);
			return close == -1 ? cmdstr.Length : close + 1;
		}
		if (Environment.OSVersion.Platform != PlatformID.Win32NT)
		{
			return pos;
		}
		IList<string> path = null;
		for (; ; )
		{
			string str = cmdstr.Substring(0, pos);
			if (Path.IsPathRooted(str))
			{
				if (Exists(str))
				{
					return pos;
				}
			}
			else
			{
				if (path == null)
				{
					path = GetSearchPath();
				}
				foreach (string p in path)
				{
					if (Exists(Path.Combine(p, str)))
					{
						return pos;
					}
				}
			}
			if (pos == cmdstr.Length)
			{
				return cmdstr.IndexOf(' ');
			}
			pos = cmdstr.IndexOf(' ', pos + 1);
			if (pos == -1)
			{
				pos = cmdstr.Length;
			}
		}
	}

	private static List<string> GetSearchPath()
	{
		List<string> list = new List<string>();
		list.Add(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));
		list.Add(Environment.CurrentDirectory);
		list.Add(Environment.SystemDirectory);
		string windir = Path.GetDirectoryName(Environment.SystemDirectory);
		list.Add(Path.Combine(windir, "System"));
		list.Add(windir);
		string path = Environment.GetEnvironmentVariable("PATH");
		if (path != null)
		{
			foreach (string p in path.Split(Path.PathSeparator))
			{
				list.Add(p);
			}
		}
		return list;
	}

	private static bool Exists(string file)
	{
		try
		{
			if (File.Exists(file))
			{
				return true;
			}
			else if (Directory.Exists(file))
			{
				return false;
			}
			else if (file.IndexOf('.') == -1 && File.Exists(file + ".exe"))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch
		{
			return false;
		}
	}
}