-
Notifications
You must be signed in to change notification settings - Fork 7
/
TypeWrapper.cs
4795 lines (4430 loc) · 133 KB
/
TypeWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2002-2011 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.Collections.Generic;
#if STATIC_COMPILER || STUB_GENERATOR
using IKVM.Reflection;
using IKVM.Reflection.Emit;
using Type = IKVM.Reflection.Type;
#else
using System.Reflection;
#if !NOEMIT
using System.Reflection.Emit;
#endif
#endif
using System.Diagnostics;
using System.Security;
using System.Security.Permissions;
using IKVM.Attributes;
namespace IKVM.Internal
{
static class StringConstants
{
internal static readonly string CLINIT = string.Intern("<clinit>");
internal static readonly string INIT = string.Intern("<init>");
internal static readonly string SIG_VOID = string.Intern("()V");
internal static readonly string FINALIZE = string.Intern("finalize");
internal static readonly string CLONE = string.Intern("clone");
}
struct ExModifiers
{
internal readonly Modifiers Modifiers;
internal readonly bool IsInternal;
internal ExModifiers(Modifiers modifiers, bool isInternal)
{
this.Modifiers = modifiers;
this.IsInternal = isInternal;
}
}
static class AttributeHelper
{
#if !NOEMIT
private static CustomAttributeBuilder hideFromJavaAttribute;
#endif
#if STATIC_COMPILER
private static CustomAttributeBuilder ghostInterfaceAttribute;
private static CustomAttributeBuilder deprecatedAttribute;
private static CustomAttributeBuilder editorBrowsableNever;
private static ConstructorInfo implementsAttribute;
private static ConstructorInfo throwsAttribute;
private static ConstructorInfo sourceFileAttribute;
private static ConstructorInfo lineNumberTableAttribute1;
private static ConstructorInfo lineNumberTableAttribute2;
private static ConstructorInfo enclosingMethodAttribute;
private static ConstructorInfo signatureAttribute;
private static CustomAttributeBuilder paramArrayAttribute;
private static ConstructorInfo nonNestedInnerClassAttribute;
private static ConstructorInfo nonNestedOuterClassAttribute;
private static Type typeofModifiers = JVM.LoadType(typeof(Modifiers));
private static Type typeofSourceFileAttribute = JVM.LoadType(typeof(SourceFileAttribute));
private static Type typeofLineNumberTableAttribute = JVM.LoadType(typeof(LineNumberTableAttribute));
#endif // STATIC_COMPILER
private static Type typeofRemappedClassAttribute = JVM.LoadType(typeof(RemappedClassAttribute));
private static Type typeofRemappedTypeAttribute = JVM.LoadType(typeof(RemappedTypeAttribute));
private static Type typeofModifiersAttribute = JVM.LoadType(typeof(ModifiersAttribute));
private static Type typeofRemappedInterfaceMethodAttribute = JVM.LoadType(typeof(RemappedInterfaceMethodAttribute));
private static Type typeofNameSigAttribute = JVM.LoadType(typeof(NameSigAttribute));
private static Type typeofJavaModuleAttribute = JVM.LoadType(typeof(JavaModuleAttribute));
private static Type typeofSignatureAttribute = JVM.LoadType(typeof(SignatureAttribute));
private static Type typeofInnerClassAttribute = JVM.LoadType(typeof(InnerClassAttribute));
private static Type typeofImplementsAttribute = JVM.LoadType(typeof(ImplementsAttribute));
private static Type typeofGhostInterfaceAttribute = JVM.LoadType(typeof(GhostInterfaceAttribute));
private static Type typeofExceptionIsUnsafeForMappingAttribute = JVM.LoadType(typeof(ExceptionIsUnsafeForMappingAttribute));
private static Type typeofThrowsAttribute = JVM.LoadType(typeof(ThrowsAttribute));
private static Type typeofHideFromReflectionAttribute = JVM.LoadType(typeof(HideFromReflectionAttribute));
private static Type typeofHideFromJavaAttribute = JVM.LoadType(typeof(HideFromJavaAttribute));
private static Type typeofNoPackagePrefixAttribute = JVM.LoadType(typeof(NoPackagePrefixAttribute));
private static Type typeofAnnotationAttributeAttribute = JVM.LoadType(typeof(AnnotationAttributeAttribute));
private static Type typeofNonNestedInnerClassAttribute = JVM.LoadType(typeof(NonNestedInnerClassAttribute));
private static Type typeofNonNestedOuterClassAttribute = JVM.LoadType(typeof(NonNestedOuterClassAttribute));
private static Type typeofEnclosingMethodAttribute = JVM.LoadType(typeof(EnclosingMethodAttribute));
#if STATIC_COMPILER
private static object ParseValue(ClassLoaderWrapper loader, TypeWrapper tw, string val)
{
if(tw == CoreClasses.java.lang.String.Wrapper)
{
return val;
}
else if(tw.TypeAsTBD.IsEnum)
{
return EnumHelper.Parse(tw.TypeAsTBD, val);
}
else if(tw.TypeAsTBD == Types.Type)
{
TypeWrapper valtw = loader.LoadClassByDottedNameFast(val);
if(valtw != null)
{
return valtw.TypeAsBaseType;
}
return StaticCompiler.Universe.GetType(val, true);
}
else if(tw == PrimitiveTypeWrapper.BOOLEAN)
{
return bool.Parse(val);
}
else if(tw == PrimitiveTypeWrapper.BYTE)
{
return (byte)sbyte.Parse(val);
}
else if(tw == PrimitiveTypeWrapper.CHAR)
{
return char.Parse(val);
}
else if(tw == PrimitiveTypeWrapper.SHORT)
{
return short.Parse(val);
}
else if(tw == PrimitiveTypeWrapper.INT)
{
return int.Parse(val);
}
else if(tw == PrimitiveTypeWrapper.FLOAT)
{
return float.Parse(val);
}
else if(tw == PrimitiveTypeWrapper.LONG)
{
return long.Parse(val);
}
else if(tw == PrimitiveTypeWrapper.DOUBLE)
{
return double.Parse(val);
}
else
{
throw new NotImplementedException();
}
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, TypeBuilder tb, IKVM.Internal.MapXml.Attribute attr)
{
bool declarativeSecurity;
CustomAttributeBuilder cab = CreateCustomAttribute(loader, attr, out declarativeSecurity);
if (declarativeSecurity)
{
tb.__AddDeclarativeSecurity(cab);
}
else
{
tb.SetCustomAttribute(cab);
}
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, FieldBuilder fb, IKVM.Internal.MapXml.Attribute attr)
{
fb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, ParameterBuilder pb, IKVM.Internal.MapXml.Attribute attr)
{
pb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, MethodBuilder mb, IKVM.Internal.MapXml.Attribute attr)
{
bool declarativeSecurity;
CustomAttributeBuilder cab = CreateCustomAttribute(loader, attr, out declarativeSecurity);
if (declarativeSecurity)
{
mb.__AddDeclarativeSecurity(cab);
}
else
{
mb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
}
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, ConstructorBuilder cb, IKVM.Internal.MapXml.Attribute attr)
{
bool declarativeSecurity;
CustomAttributeBuilder cab = CreateCustomAttribute(loader, attr, out declarativeSecurity);
if (declarativeSecurity)
{
cb.__AddDeclarativeSecurity(cab);
}
else
{
cb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
}
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, PropertyBuilder pb, IKVM.Internal.MapXml.Attribute attr)
{
pb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, AssemblyBuilder ab, IKVM.Internal.MapXml.Attribute attr)
{
ab.SetCustomAttribute(CreateCustomAttribute(loader, attr));
}
private static void GetAttributeArgsAndTypes(ClassLoaderWrapper loader, IKVM.Internal.MapXml.Attribute attr, out Type[] argTypes, out object[] args)
{
// TODO add error handling
TypeWrapper[] twargs = loader.ArgTypeWrapperListFromSigNoThrow(attr.Sig);
argTypes = new Type[twargs.Length];
args = new object[argTypes.Length];
for(int i = 0; i < twargs.Length; i++)
{
argTypes[i] = twargs[i].TypeAsSignatureType;
TypeWrapper tw = twargs[i];
if(tw == CoreClasses.java.lang.Object.Wrapper)
{
tw = loader.FieldTypeWrapperFromSigNoThrow(attr.Params[i].Sig);
}
if(tw.IsArray)
{
Array arr = Array.CreateInstance(Type.__GetSystemType(Type.GetTypeCode(tw.ElementTypeWrapper.TypeAsArrayType)), attr.Params[i].Elements.Length);
for(int j = 0; j < arr.Length; j++)
{
arr.SetValue(ParseValue(loader, tw.ElementTypeWrapper, attr.Params[i].Elements[j].Value), j);
}
args[i] = arr;
}
else
{
args[i] = ParseValue(loader, tw, attr.Params[i].Value);
}
}
}
private static CustomAttributeBuilder CreateCustomAttribute(ClassLoaderWrapper loader, IKVM.Internal.MapXml.Attribute attr)
{
bool ignore;
return CreateCustomAttribute(loader, attr, out ignore);
}
private static CustomAttributeBuilder CreateCustomAttribute(ClassLoaderWrapper loader, IKVM.Internal.MapXml.Attribute attr, out bool isDeclarativeSecurity)
{
// TODO add error handling
Type[] argTypes;
object[] args;
GetAttributeArgsAndTypes(loader, attr, out argTypes, out args);
if(attr.Type != null)
{
Type t = StaticCompiler.GetTypeForMapXml(loader, attr.Type);
isDeclarativeSecurity = t.IsSubclassOf(Types.SecurityAttribute);
ConstructorInfo ci = t.GetConstructor(argTypes);
if(ci == null)
{
throw new InvalidOperationException(string.Format("Constructor missing: {0}::<init>{1}", attr.Type, attr.Sig));
}
PropertyInfo[] namedProperties;
object[] propertyValues;
if(attr.Properties != null)
{
namedProperties = new PropertyInfo[attr.Properties.Length];
propertyValues = new object[attr.Properties.Length];
for(int i = 0; i < namedProperties.Length; i++)
{
namedProperties[i] = t.GetProperty(attr.Properties[i].Name);
propertyValues[i] = ParseValue(loader, loader.FieldTypeWrapperFromSigNoThrow(attr.Properties[i].Sig), attr.Properties[i].Value);
}
}
else
{
namedProperties = new PropertyInfo[0];
propertyValues = new object[0];
}
FieldInfo[] namedFields;
object[] fieldValues;
if(attr.Fields != null)
{
namedFields = new FieldInfo[attr.Fields.Length];
fieldValues = new object[attr.Fields.Length];
for(int i = 0; i < namedFields.Length; i++)
{
namedFields[i] = t.GetField(attr.Fields[i].Name);
fieldValues[i] = ParseValue(loader, loader.FieldTypeWrapperFromSigNoThrow(attr.Fields[i].Sig), attr.Fields[i].Value);
}
}
else
{
namedFields = new FieldInfo[0];
fieldValues = new object[0];
}
return new CustomAttributeBuilder(ci, args, namedProperties, propertyValues, namedFields, fieldValues);
}
else
{
if(attr.Properties != null)
{
throw new NotImplementedException("Setting property values on Java attributes is not implemented");
}
TypeWrapper t = loader.LoadClassByDottedName(attr.Class);
isDeclarativeSecurity = t.TypeAsBaseType.IsSubclassOf(Types.SecurityAttribute);
MethodWrapper mw = t.GetMethodWrapper("<init>", attr.Sig, false);
mw.Link();
ConstructorInfo ci = (ConstructorInfo)mw.GetMethod();
if(ci == null)
{
throw new InvalidOperationException(string.Format("Constructor missing: {0}::<init>{1}", attr.Class, attr.Sig));
}
FieldInfo[] namedFields;
object[] fieldValues;
if(attr.Fields != null)
{
namedFields = new FieldInfo[attr.Fields.Length];
fieldValues = new object[attr.Fields.Length];
for(int i = 0; i < namedFields.Length; i++)
{
FieldWrapper fw = t.GetFieldWrapper(attr.Fields[i].Name, attr.Fields[i].Sig);
fw.Link();
namedFields[i] = fw.GetField();
fieldValues[i] = ParseValue(loader, loader.FieldTypeWrapperFromSigNoThrow(attr.Fields[i].Sig), attr.Fields[i].Value);
}
}
else
{
namedFields = new FieldInfo[0];
fieldValues = new object[0];
}
return new CustomAttributeBuilder(ci, args, namedFields, fieldValues);
}
}
private static Assembly GetSystemAssembly()
{
AssemblyName name = Types.Object.Assembly.GetName();
name.Name = "System";
return StaticCompiler.Load(name.FullName);
}
private static CustomAttributeBuilder GetEditorBrowsableNever()
{
if (editorBrowsableNever == null)
{
Assembly system = GetSystemAssembly();
editorBrowsableNever = new CustomAttributeBuilder(system.GetType("System.ComponentModel.EditorBrowsableAttribute", true).GetConstructor(new Type[] { system.GetType("System.ComponentModel.EditorBrowsableState", true) }), new object[] { (int)System.ComponentModel.EditorBrowsableState.Never });
}
return editorBrowsableNever;
}
internal static void SetEditorBrowsableNever(TypeBuilder tb)
{
tb.SetCustomAttribute(GetEditorBrowsableNever());
}
internal static void SetEditorBrowsableNever(MethodBuilder mb)
{
mb.SetCustomAttribute(GetEditorBrowsableNever());
}
internal static void SetEditorBrowsableNever(ConstructorBuilder cb)
{
cb.SetCustomAttribute(GetEditorBrowsableNever());
}
internal static void SetEditorBrowsableNever(PropertyBuilder pb)
{
pb.SetCustomAttribute(GetEditorBrowsableNever());
}
internal static void SetDeprecatedAttribute(MethodBase mb)
{
if(deprecatedAttribute == null)
{
deprecatedAttribute = new CustomAttributeBuilder(JVM.Import(typeof(ObsoleteAttribute)).GetConstructor(Type.EmptyTypes), new object[0]);
}
MethodBuilder method = mb as MethodBuilder;
if(method != null)
{
method.SetCustomAttribute(deprecatedAttribute);
}
else
{
((ConstructorBuilder)mb).SetCustomAttribute(deprecatedAttribute);
}
}
internal static void SetDeprecatedAttribute(TypeBuilder tb)
{
if(deprecatedAttribute == null)
{
deprecatedAttribute = new CustomAttributeBuilder(JVM.Import(typeof(ObsoleteAttribute)).GetConstructor(Type.EmptyTypes), new object[0]);
}
tb.SetCustomAttribute(deprecatedAttribute);
}
internal static void SetDeprecatedAttribute(FieldBuilder fb)
{
if(deprecatedAttribute == null)
{
deprecatedAttribute = new CustomAttributeBuilder(JVM.Import(typeof(ObsoleteAttribute)).GetConstructor(Type.EmptyTypes), new object[0]);
}
fb.SetCustomAttribute(deprecatedAttribute);
}
internal static void SetDeprecatedAttribute(PropertyBuilder pb)
{
if(deprecatedAttribute == null)
{
deprecatedAttribute = new CustomAttributeBuilder(JVM.Import(typeof(ObsoleteAttribute)).GetConstructor(Type.EmptyTypes), new object[0]);
}
pb.SetCustomAttribute(deprecatedAttribute);
}
internal static void SetThrowsAttribute(MethodBase mb, string[] exceptions)
{
if(exceptions != null && exceptions.Length != 0)
{
if(throwsAttribute == null)
{
throwsAttribute = typeofThrowsAttribute.GetConstructor(new Type[] { JVM.Import(typeof(string[])) });
}
if(mb is MethodBuilder)
{
MethodBuilder method = (MethodBuilder)mb;
method.SetCustomAttribute(new CustomAttributeBuilder(throwsAttribute, new object[] { exceptions }));
}
else
{
ConstructorBuilder constructor = (ConstructorBuilder)mb;
constructor.SetCustomAttribute(new CustomAttributeBuilder(throwsAttribute, new object[] { exceptions }));
}
}
}
internal static void SetGhostInterface(TypeBuilder typeBuilder)
{
if(ghostInterfaceAttribute == null)
{
ghostInterfaceAttribute = new CustomAttributeBuilder(typeofGhostInterfaceAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
}
typeBuilder.SetCustomAttribute(ghostInterfaceAttribute);
}
internal static void SetNonNestedInnerClass(TypeBuilder typeBuilder, string className)
{
if(nonNestedInnerClassAttribute == null)
{
nonNestedInnerClassAttribute = typeofNonNestedInnerClassAttribute.GetConstructor(new Type[] { Types.String });
}
typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(nonNestedInnerClassAttribute, new object[] { className }));
}
internal static void SetNonNestedOuterClass(TypeBuilder typeBuilder, string className)
{
if(nonNestedOuterClassAttribute == null)
{
nonNestedOuterClassAttribute = typeofNonNestedOuterClassAttribute.GetConstructor(new Type[] { Types.String });
}
typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(nonNestedOuterClassAttribute, new object[] { className }));
}
#endif // STATIC_COMPILER
#if !NOEMIT
internal static void HideFromReflection(MethodBuilder mb)
{
CustomAttributeBuilder cab = new CustomAttributeBuilder(typeofHideFromReflectionAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
mb.SetCustomAttribute(cab);
}
internal static void HideFromReflection(FieldBuilder fb)
{
CustomAttributeBuilder cab = new CustomAttributeBuilder(typeofHideFromReflectionAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
fb.SetCustomAttribute(cab);
}
internal static void HideFromReflection(PropertyBuilder pb)
{
CustomAttributeBuilder cab = new CustomAttributeBuilder(typeofHideFromReflectionAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
pb.SetCustomAttribute(cab);
}
#endif
internal static bool IsHideFromReflection(MemberInfo mi)
{
return mi.IsDefined(typeofHideFromReflectionAttribute, false);
}
#if !NOEMIT
internal static void HideFromJava(TypeBuilder typeBuilder)
{
if(hideFromJavaAttribute == null)
{
hideFromJavaAttribute = new CustomAttributeBuilder(typeofHideFromJavaAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
}
typeBuilder.SetCustomAttribute(hideFromJavaAttribute);
}
internal static void HideFromJava(ConstructorBuilder cb)
{
if(hideFromJavaAttribute == null)
{
hideFromJavaAttribute = new CustomAttributeBuilder(typeofHideFromJavaAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
}
cb.SetCustomAttribute(hideFromJavaAttribute);
}
internal static void HideFromJava(MethodBuilder mb)
{
if(hideFromJavaAttribute == null)
{
hideFromJavaAttribute = new CustomAttributeBuilder(typeofHideFromJavaAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
}
mb.SetCustomAttribute(hideFromJavaAttribute);
}
internal static void HideFromJava(FieldBuilder fb)
{
if(hideFromJavaAttribute == null)
{
hideFromJavaAttribute = new CustomAttributeBuilder(typeofHideFromJavaAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
}
fb.SetCustomAttribute(hideFromJavaAttribute);
}
#endif // !NOEMIT
#if STATIC_COMPILER
internal static void HideFromJava(PropertyBuilder pb)
{
if(hideFromJavaAttribute == null)
{
hideFromJavaAttribute = new CustomAttributeBuilder(typeofHideFromJavaAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
}
pb.SetCustomAttribute(hideFromJavaAttribute);
}
#endif // STATIC_COMPILER
internal static bool IsHideFromJava(Type type)
{
return type.IsDefined(typeofHideFromJavaAttribute, false)
|| (type.IsNested && (type.DeclaringType.IsDefined(typeofHideFromJavaAttribute, false) || type.Name.StartsWith("__<", StringComparison.Ordinal)));
}
internal static bool IsHideFromJava(MemberInfo mi)
{
// NOTE all privatescope fields and methods are "hideFromJava"
// because Java cannot deal with the potential name clashes
FieldInfo fi = mi as FieldInfo;
if(fi != null && (fi.Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.PrivateScope)
{
return true;
}
MethodBase mb = mi as MethodBase;
if(mb != null && (mb.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.PrivateScope)
{
return true;
}
if (mi.Name.StartsWith("__<", StringComparison.Ordinal))
{
return true;
}
return mi.IsDefined(typeofHideFromJavaAttribute, false);
}
#if STATIC_COMPILER
internal static void SetImplementsAttribute(TypeBuilder typeBuilder, TypeWrapper[] ifaceWrappers)
{
if(ifaceWrappers != null && ifaceWrappers.Length != 0)
{
string[] interfaces = new string[ifaceWrappers.Length];
for(int i = 0; i < interfaces.Length; i++)
{
interfaces[i] = ifaceWrappers[i].Name;
}
if(implementsAttribute == null)
{
implementsAttribute = typeofImplementsAttribute.GetConstructor(new Type[] { JVM.Import(typeof(string[])) });
}
typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(implementsAttribute, new object[] { interfaces }));
}
}
#endif
internal static bool IsGhostInterface(Type type)
{
return type.IsDefined(typeofGhostInterfaceAttribute, false);
}
internal static bool IsRemappedType(Type type)
{
return type.IsDefined(typeofRemappedTypeAttribute, false);
}
internal static bool IsExceptionIsUnsafeForMapping(Type type)
{
return type.IsDefined(typeofExceptionIsUnsafeForMappingAttribute, false);
}
internal static ModifiersAttribute GetModifiersAttribute(MemberInfo member)
{
#if !STATIC_COMPILER && !STUB_GENERATOR
object[] attr = member.GetCustomAttributes(typeof(ModifiersAttribute), false);
return attr.Length == 1 ? (ModifiersAttribute)attr[0] : null;
#else
IList<CustomAttributeData> attr = CustomAttributeData.__GetCustomAttributes(member, typeofModifiersAttribute, false);
if(attr.Count == 1)
{
IList<CustomAttributeTypedArgument> args = attr[0].ConstructorArguments;
if(args.Count == 2)
{
return new ModifiersAttribute((Modifiers)args[0].Value, (bool)args[1].Value);
}
return new ModifiersAttribute((Modifiers)args[0].Value);
}
return null;
#endif
}
internal static ExModifiers GetModifiers(MethodBase mb, bool assemblyIsPrivate)
{
ModifiersAttribute attr = GetModifiersAttribute(mb);
if(attr != null)
{
return new ExModifiers(attr.Modifiers, attr.IsInternal);
}
Modifiers modifiers = 0;
if(mb.IsPublic)
{
modifiers |= Modifiers.Public;
}
else if(mb.IsPrivate)
{
modifiers |= Modifiers.Private;
}
else if(mb.IsFamily || mb.IsFamilyOrAssembly)
{
modifiers |= Modifiers.Protected;
}
else if(assemblyIsPrivate)
{
modifiers |= Modifiers.Private;
}
// NOTE Java doesn't support non-virtual methods, but we set the Final modifier for
// non-virtual methods to approximate the semantics
if((mb.IsFinal || (!mb.IsVirtual && ((modifiers & Modifiers.Private) == 0))) && !mb.IsStatic && !mb.IsConstructor)
{
modifiers |= Modifiers.Final;
}
if(mb.IsAbstract)
{
modifiers |= Modifiers.Abstract;
}
else
{
// Some .NET interfaces (like System._AppDomain) have synchronized methods,
// Java doesn't allow synchronized on an abstract methods, so we ignore it for
// abstract methods.
if((mb.GetMethodImplementationFlags() & MethodImplAttributes.Synchronized) != 0)
{
modifiers |= Modifiers.Synchronized;
}
}
if(mb.IsStatic)
{
modifiers |= Modifiers.Static;
}
if((mb.Attributes & MethodAttributes.PinvokeImpl) != 0)
{
modifiers |= Modifiers.Native;
}
ParameterInfo[] parameters = mb.GetParameters();
if(parameters.Length > 0 && parameters[parameters.Length - 1].IsDefined(JVM.Import(typeof(ParamArrayAttribute)), false))
{
modifiers |= Modifiers.VarArgs;
}
return new ExModifiers(modifiers, false);
}
internal static ExModifiers GetModifiers(FieldInfo fi, bool assemblyIsPrivate)
{
ModifiersAttribute attr = GetModifiersAttribute(fi);
if(attr != null)
{
return new ExModifiers(attr.Modifiers, attr.IsInternal);
}
Modifiers modifiers = 0;
if(fi.IsPublic)
{
modifiers |= Modifiers.Public;
}
else if(fi.IsPrivate)
{
modifiers |= Modifiers.Private;
}
else if(fi.IsFamily || fi.IsFamilyOrAssembly)
{
modifiers |= Modifiers.Protected;
}
else if(assemblyIsPrivate)
{
modifiers |= Modifiers.Private;
}
if(fi.IsInitOnly || fi.IsLiteral)
{
modifiers |= Modifiers.Final;
}
if(fi.IsNotSerialized)
{
modifiers |= Modifiers.Transient;
}
if(fi.IsStatic)
{
modifiers |= Modifiers.Static;
}
if(Array.IndexOf(fi.GetRequiredCustomModifiers(), Types.IsVolatile) != -1)
{
modifiers |= Modifiers.Volatile;
}
return new ExModifiers(modifiers, false);
}
#if STATIC_COMPILER
internal static void SetModifiers(MethodBuilder mb, Modifiers modifiers, bool isInternal)
{
CustomAttributeBuilder customAttributeBuilder;
if (isInternal)
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers, Types.Boolean }), new object[] { modifiers, isInternal });
}
else
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers }), new object[] { modifiers });
}
mb.SetCustomAttribute(customAttributeBuilder);
}
internal static void SetModifiers(ConstructorBuilder cb, Modifiers modifiers, bool isInternal)
{
CustomAttributeBuilder customAttributeBuilder;
if (isInternal)
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers, Types.Boolean }), new object[] { modifiers, isInternal });
}
else
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers }), new object[] { modifiers });
}
cb.SetCustomAttribute(customAttributeBuilder);
}
internal static void SetModifiers(FieldBuilder fb, Modifiers modifiers, bool isInternal)
{
CustomAttributeBuilder customAttributeBuilder;
if (isInternal)
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers, Types.Boolean }), new object[] { modifiers, isInternal });
}
else
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers }), new object[] { modifiers });
}
fb.SetCustomAttribute(customAttributeBuilder);
}
internal static void SetModifiers(PropertyBuilder pb, Modifiers modifiers, bool isInternal)
{
CustomAttributeBuilder customAttributeBuilder;
if (isInternal)
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers, Types.Boolean }), new object[] { modifiers, isInternal });
}
else
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers }), new object[] { modifiers });
}
pb.SetCustomAttribute(customAttributeBuilder);
}
internal static void SetModifiers(TypeBuilder tb, Modifiers modifiers, bool isInternal)
{
CustomAttributeBuilder customAttributeBuilder;
if (isInternal)
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers, Types.Boolean }), new object[] { modifiers, isInternal });
}
else
{
customAttributeBuilder = new CustomAttributeBuilder(typeofModifiersAttribute.GetConstructor(new Type[] { typeofModifiers }), new object[] { modifiers });
}
tb.SetCustomAttribute(customAttributeBuilder);
}
internal static void SetNameSig(MethodBase mb, string name, string sig)
{
CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(typeofNameSigAttribute.GetConstructor(new Type[] { Types.String, Types.String }), new object[] { name, sig });
MethodBuilder method = mb as MethodBuilder;
if(method != null)
{
method.SetCustomAttribute(customAttributeBuilder);
}
else
{
((ConstructorBuilder)mb).SetCustomAttribute(customAttributeBuilder);
}
}
internal static void SetNameSig(FieldBuilder fb, string name, string sig)
{
CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(typeofNameSigAttribute.GetConstructor(new Type[] { Types.String, Types.String }), new object[] { name, sig });
fb.SetCustomAttribute(customAttributeBuilder);
}
internal static void SetNameSig(PropertyBuilder pb, string name, string sig)
{
CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(typeofNameSigAttribute.GetConstructor(new Type[] { Types.String, Types.String }), new object[] { name, sig });
pb.SetCustomAttribute(customAttributeBuilder);
}
internal static byte[] FreezeDryType(Type type)
{
System.IO.MemoryStream mem = new System.IO.MemoryStream();
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(mem, System.Text.UTF8Encoding.UTF8);
bw.Write((short)1);
bw.Write(type.FullName);
bw.Write((short)0);
return mem.ToArray();
}
internal static void SetInnerClass(TypeBuilder typeBuilder, string innerClass, Modifiers modifiers)
{
Type[] argTypes = new Type[] { Types.String, typeofModifiers };
object[] args = new object[] { innerClass, modifiers };
ConstructorInfo ci = typeofInnerClassAttribute.GetConstructor(argTypes);
CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(ci, args);
typeBuilder.SetCustomAttribute(customAttributeBuilder);
}
internal static void SetSourceFile(TypeBuilder typeBuilder, string filename)
{
if(sourceFileAttribute == null)
{
sourceFileAttribute = typeofSourceFileAttribute.GetConstructor(new Type[] { Types.String });
}
typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(sourceFileAttribute, new object[] { filename }));
}
internal static void SetSourceFile(ModuleBuilder moduleBuilder, string filename)
{
if(sourceFileAttribute == null)
{
sourceFileAttribute = typeofSourceFileAttribute.GetConstructor(new Type[] { Types.String });
}
moduleBuilder.SetCustomAttribute(new CustomAttributeBuilder(sourceFileAttribute, new object[] { filename }));
}
internal static void SetLineNumberTable(MethodBase mb, IKVM.Attributes.LineNumberTableAttribute.LineNumberWriter writer)
{
object arg;
ConstructorInfo con;
if(writer.Count == 1)
{
if(lineNumberTableAttribute2 == null)
{
lineNumberTableAttribute2 = typeofLineNumberTableAttribute.GetConstructor(new Type[] { Types.UInt16 });
}
con = lineNumberTableAttribute2;
arg = (ushort)writer.LineNo;
}
else
{
if(lineNumberTableAttribute1 == null)
{
lineNumberTableAttribute1 = typeofLineNumberTableAttribute.GetConstructor(new Type[] { JVM.Import(typeof(byte[])) });
}
con = lineNumberTableAttribute1;
arg = writer.ToArray();
}
if(mb is ConstructorBuilder)
{
((ConstructorBuilder)mb).SetCustomAttribute(new CustomAttributeBuilder(con, new object[] { arg }));
}
else
{
((MethodBuilder)mb).SetCustomAttribute(new CustomAttributeBuilder(con, new object[] { arg }));
}
}
internal static void SetEnclosingMethodAttribute(TypeBuilder tb, string className, string methodName, string methodSig)
{
if(enclosingMethodAttribute == null)
{
enclosingMethodAttribute = typeofEnclosingMethodAttribute.GetConstructor(new Type[] { Types.String, Types.String, Types.String });
}
tb.SetCustomAttribute(new CustomAttributeBuilder(enclosingMethodAttribute, new object[] { className, methodName, methodSig }));
}
internal static void SetSignatureAttribute(TypeBuilder tb, string signature)
{
if(signatureAttribute == null)
{
signatureAttribute = typeofSignatureAttribute.GetConstructor(new Type[] { Types.String });
}
tb.SetCustomAttribute(new CustomAttributeBuilder(signatureAttribute, new object[] { signature }));
}
internal static void SetSignatureAttribute(FieldBuilder fb, string signature)
{
if(signatureAttribute == null)
{
signatureAttribute = typeofSignatureAttribute.GetConstructor(new Type[] { Types.String });
}
fb.SetCustomAttribute(new CustomAttributeBuilder(signatureAttribute, new object[] { signature }));
}
internal static void SetSignatureAttribute(MethodBase mb, string signature)
{
if(signatureAttribute == null)
{
signatureAttribute = typeofSignatureAttribute.GetConstructor(new Type[] { Types.String });
}
if(mb is ConstructorBuilder)
{
((ConstructorBuilder)mb).SetCustomAttribute(new CustomAttributeBuilder(signatureAttribute, new object[] { signature }));
}
else
{
((MethodBuilder)mb).SetCustomAttribute(new CustomAttributeBuilder(signatureAttribute, new object[] { signature }));
}
}
internal static void SetParamArrayAttribute(ParameterBuilder pb)
{
if(paramArrayAttribute == null)
{
paramArrayAttribute = new CustomAttributeBuilder(JVM.Import(typeof(ParamArrayAttribute)).GetConstructor(Type.EmptyTypes), new object[0]);
}
pb.SetCustomAttribute(paramArrayAttribute);
}
#endif // STATIC_COMPILER
internal static NameSigAttribute GetNameSig(MemberInfo member)
{
#if !STATIC_COMPILER && !STUB_GENERATOR
object[] attr = member.GetCustomAttributes(typeof(NameSigAttribute), false);
return attr.Length == 1 ? (NameSigAttribute)attr[0] : null;
#else
foreach(CustomAttributeData cad in CustomAttributeData.__GetCustomAttributes(member, typeofNameSigAttribute, false))
{
IList<CustomAttributeTypedArgument> args = cad.ConstructorArguments;
return new NameSigAttribute((string)args[0].Value, (string)args[1].Value);
}
return null;
#endif
}
internal static T[] DecodeArray<T>(CustomAttributeTypedArgument arg)
{
IList<CustomAttributeTypedArgument> elems = (IList<CustomAttributeTypedArgument>)arg.Value;
T[] arr = new T[elems.Count];
for(int i = 0; i < arr.Length; i++)
{
arr[i] = (T)elems[i].Value;
}
return arr;
}
internal static ImplementsAttribute GetImplements(Type type)
{
#if !STATIC_COMPILER && !STUB_GENERATOR
object[] attribs = type.GetCustomAttributes(typeof(ImplementsAttribute), false);
return attribs.Length == 1 ? (ImplementsAttribute)attribs[0] : null;
#else
foreach(CustomAttributeData cad in CustomAttributeData.__GetCustomAttributes(type, typeofImplementsAttribute, false))
{
IList<CustomAttributeTypedArgument> args = cad.ConstructorArguments;
return new ImplementsAttribute(DecodeArray<string>(args[0]));
}
return null;
#endif
}
internal static ThrowsAttribute GetThrows(MethodBase mb)