IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
AttributeHelper.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2015 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26using System.ComponentModel;
27using System.Diagnostics;
29
30using IKVM.Attributes;
32using IKVM.ByteCode.Buffers;
33using IKVM.ByteCode.Decoding;
34using IKVM.ByteCode.Encoding;
35
36#if IMPORTER || EXPORTER
37using IKVM.Reflection;
39
40using Type = IKVM.Reflection.Type;
41#else
42using System.Reflection;
43using System.Reflection.Emit;
44#endif
45
46#if IMPORTER
48#endif
49
50namespace IKVM.Runtime
51{
52
57 {
58
59 readonly RuntimeContext context;
60
61#if IMPORTER
62
63 CustomAttributeBuilder compilerGeneratedAttribute;
64 CustomAttributeBuilder ghostInterfaceAttribute;
65 CustomAttributeBuilder deprecatedAttribute;
66 CustomAttributeBuilder editorBrowsableNever;
67 ConstructorInfo implementsAttribute;
68 ConstructorInfo throwsAttribute;
69 ConstructorInfo sourceFileAttribute;
70 ConstructorInfo lineNumberTableAttribute1;
71 ConstructorInfo lineNumberTableAttribute2;
72 ConstructorInfo enclosingMethodAttribute;
73 ConstructorInfo signatureAttribute;
74 ConstructorInfo methodParametersAttribute;
75 ConstructorInfo runtimeVisibleTypeAnnotationsAttribute;
76 ConstructorInfo constantPoolAttribute;
77 CustomAttributeBuilder paramArrayAttribute;
78 ConstructorInfo nonNestedInnerClassAttribute;
79 ConstructorInfo nonNestedOuterClassAttribute;
80
81 Type typeofModifiers;
82 Type typeofSourceFileAttribute;
83 Type typeofLineNumberTableAttribute;
84
85#endif
86
87 Type typeofRemappedClassAttribute;
88 Type typeofRemappedTypeAttribute;
89 Type typeofModifiersAttribute;
90 Type typeofRemappedInterfaceMethodAttribute;
91 Type typeofNameSigAttribute;
92 Type typeofJavaModuleAttribute;
93 Type typeofSignatureAttribute;
94 Type typeofInnerClassAttribute;
95 Type typeofImplementsAttribute;
96 Type typeofGhostInterfaceAttribute;
97 Type typeofExceptionIsUnsafeForMappingAttribute;
98 Type typeofThrowsAttribute;
99 Type typeofHideFromJavaAttribute;
100 Type typeofHideFromJavaFlags;
101 Type typeofNoPackagePrefixAttribute;
102 Type typeofAnnotationAttributeAttribute;
103 Type typeofNonNestedInnerClassAttribute;
104 Type typeofNonNestedOuterClassAttribute;
105 Type typeofEnclosingMethodAttribute;
106 Type typeofMethodParametersAttribute;
107 Type typeofRuntimeVisibleTypeAnnotationsAttribute;
108 Type typeofConstantPoolAttribute;
109 Type typeofDebuggableAttribute;
110 CustomAttributeBuilder hideFromJavaAttribute;
111 CustomAttributeBuilder hideFromReflection;
112 ConstructorInfo debuggableAttribute;
113
114#if IMPORTER
115
116 Type TypeOfModifiers => typeofModifiers ??= LoadType(typeof(Modifiers));
117
118 Type TypeOfSourceFileAttribute => typeofSourceFileAttribute ??= LoadType(typeof(IKVM.Attributes.SourceFileAttribute));
119
120 Type TypeOfLineNumberTableAttribute => typeofLineNumberTableAttribute ??= LoadType(typeof(IKVM.Attributes.LineNumberTableAttribute));
121
122#endif
123
124 Type TypeOfRemappedClassAttribute => typeofRemappedClassAttribute ??= LoadType(typeof(RemappedClassAttribute));
125
126 Type TypeOfRemappedTypeAttribute => typeofRemappedTypeAttribute ??= LoadType(typeof(RemappedTypeAttribute));
127
128 Type TypeOfModifiersAttribute => typeofModifiersAttribute ??= LoadType(typeof(ModifiersAttribute));
129
130 Type TypeOfRemappedInterfaceMethodAttribute => typeofRemappedInterfaceMethodAttribute ??= LoadType(typeof(RemappedInterfaceMethodAttribute));
131
132 Type TypeOfNameSigAttribute => typeofNameSigAttribute ??= LoadType(typeof(NameSigAttribute));
133
134 Type TypeOfJavaModuleAttribute => typeofJavaModuleAttribute ??= LoadType(typeof(JavaModuleAttribute));
135
136 Type TypeOfSignatureAttribute => typeofSignatureAttribute ??= LoadType(typeof(IKVM.Attributes.SignatureAttribute));
137
138 Type TypeOfInnerClassAttribute => typeofInnerClassAttribute ??= LoadType(typeof(InnerClassAttribute));
139
140 Type TypeOfImplementsAttribute => typeofImplementsAttribute ??= LoadType(typeof(ImplementsAttribute));
141
142 Type TypeOfGhostInterfaceAttribute => typeofGhostInterfaceAttribute ??= LoadType(typeof(GhostInterfaceAttribute));
143
144 Type TypeOfExceptionIsUnsafeForMappingAttribute => typeofExceptionIsUnsafeForMappingAttribute ??= LoadType(typeof(ExceptionIsUnsafeForMappingAttribute));
145
146 Type TypeOfThrowsAttribute => typeofThrowsAttribute ??= LoadType(typeof(ThrowsAttribute));
147
148 Type TypeOfHideFromJavaAttribute => typeofHideFromJavaAttribute ??= LoadType(typeof(HideFromJavaAttribute));
149
150 Type TypeOfHideFromJavaFlags => typeofHideFromJavaFlags ??= LoadType(typeof(HideFromJavaFlags));
151
152 Type TypeOfNoPackagePrefixAttribute => typeofNoPackagePrefixAttribute ??= LoadType(typeof(NoPackagePrefixAttribute));
153
154 Type TypeOfAnnotationAttributeAttribute => typeofAnnotationAttributeAttribute ??= LoadType(typeof(AnnotationAttributeAttribute));
155
156 Type TypeOfNonNestedInnerClassAttribute => typeofNonNestedInnerClassAttribute ??= LoadType(typeof(NonNestedInnerClassAttribute));
157
158 Type TypeOfNonNestedOuterClassAttribute => typeofNonNestedOuterClassAttribute ??= LoadType(typeof(NonNestedOuterClassAttribute));
159
160 Type TypeOfEnclosingMethodAttribute => typeofEnclosingMethodAttribute ??= LoadType(typeof(IKVM.Attributes.EnclosingMethodAttribute));
161
162 Type TypeOfMethodParametersAttribute => typeofMethodParametersAttribute ??= LoadType(typeof(IKVM.Attributes.MethodParametersAttribute));
163
164 Type TypeOfRuntimeVisibleTypeAnnotationsAttribute => typeofRuntimeVisibleTypeAnnotationsAttribute ??= LoadType(typeof(IKVM.Attributes.RuntimeVisibleTypeAnnotationsAttribute));
165
166 Type TypeOfConstantPoolAttribute => typeofConstantPoolAttribute ??= LoadType(typeof(ConstantPoolAttribute));
167
168 Type TypeOfDebuggableAttribute => typeofDebuggableAttribute ??= context.Resolver.ResolveCoreType(typeof(DebuggableAttribute).FullName).AsReflection();
169
170 CustomAttributeBuilder HideFromJavaAttributeBuilder => hideFromJavaAttribute ??= new CustomAttributeBuilder(TypeOfHideFromJavaAttribute.GetConstructor(Type.EmptyTypes), Array.Empty<object>());
171
172 CustomAttributeBuilder HideFromReflectionBuilder => hideFromReflection ??= new CustomAttributeBuilder(TypeOfHideFromJavaAttribute.GetConstructor(new[] { TypeOfHideFromJavaFlags }), new object[] { HideFromJavaFlags.Reflection | HideFromJavaFlags.StackTrace | HideFromJavaFlags.StackWalk });
173
179 Type LoadType(System.Type t)
180 {
181 return context.Resolver.ResolveRuntimeType(t.FullName).AsReflection();
182 }
183
189 {
190 this.context = context ?? throw new ArgumentNullException(nameof(context));
191 }
192
193#if IMPORTER
194
195 object ParseValue(RuntimeClassLoader loader, RuntimeJavaType tw, string val)
196 {
197 if (tw == context.JavaBase.TypeOfJavaLangString)
198 {
199 return val;
200 }
201 else if (tw.IsUnloadable)
202 {
204 }
205 else if (tw.TypeAsTBD.IsEnum)
206 {
207 return EnumHelper.Parse(context, tw.TypeAsTBD, val);
208 }
209 else if (tw.TypeAsTBD == context.Types.Type)
210 {
211 var valtw = loader.TryLoadClassByName(val);
212 if (valtw != null)
213 return valtw.TypeAsBaseType;
214
215 return context.StaticCompiler.Universe.GetType(val, true);
216 }
217 else if (tw == context.PrimitiveJavaTypeFactory.BOOLEAN)
218 {
219 return bool.Parse(val);
220 }
221 else if (tw == context.PrimitiveJavaTypeFactory.BYTE)
222 {
223 return (byte)sbyte.Parse(val);
224 }
225 else if (tw == context.PrimitiveJavaTypeFactory.CHAR)
226 {
227 return char.Parse(val);
228 }
229 else if (tw == context.PrimitiveJavaTypeFactory.SHORT)
230 {
231 return short.Parse(val);
232 }
233 else if (tw == context.PrimitiveJavaTypeFactory.INT)
234 {
235 return int.Parse(val);
236 }
237 else if (tw == context.PrimitiveJavaTypeFactory.FLOAT)
238 {
239 return float.Parse(val);
240 }
241 else if (tw == context.PrimitiveJavaTypeFactory.LONG)
242 {
243 return long.Parse(val);
244 }
245 else if (tw == context.PrimitiveJavaTypeFactory.DOUBLE)
246 {
247 return double.Parse(val);
248 }
249 else
250 {
251 throw new NotImplementedException();
252 }
253 }
254
255 internal void SetCustomAttribute(RuntimeClassLoader loader, TypeBuilder tb, IKVM.Tools.Importer.MapXml.Attribute attr)
256 {
257 tb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
258 }
259
260 internal void SetCustomAttribute(RuntimeClassLoader loader, FieldBuilder fb, IKVM.Tools.Importer.MapXml.Attribute attr)
261 {
262 fb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
263 }
264
265 internal void SetCustomAttribute(RuntimeClassLoader loader, ParameterBuilder pb, IKVM.Tools.Importer.MapXml.Attribute attr)
266 {
267 pb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
268 }
269
270 internal void SetCustomAttribute(RuntimeClassLoader loader, MethodBuilder mb, IKVM.Tools.Importer.MapXml.Attribute attr)
271 {
272 mb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
273 }
274
275 internal void SetCustomAttribute(RuntimeClassLoader loader, PropertyBuilder pb, IKVM.Tools.Importer.MapXml.Attribute attr)
276 {
277 pb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
278 }
279
280 internal void SetCustomAttribute(RuntimeClassLoader loader, AssemblyBuilder ab, IKVM.Tools.Importer.MapXml.Attribute attr)
281 {
282 ab.SetCustomAttribute(CreateCustomAttribute(loader, attr));
283 }
284
285 void GetAttributeArgsAndTypes(RuntimeClassLoader loader, IKVM.Tools.Importer.MapXml.Attribute attr, out Type[] argTypes, out object[] args)
286 {
287 // TODO add error handling
288 var twargs = loader.ArgJavaTypeListFromSig(attr.Sig, LoadMode.Link);
289 argTypes = new Type[twargs.Length];
290 args = new object[argTypes.Length];
291 for (int i = 0; i < twargs.Length; i++)
292 {
293 argTypes[i] = twargs[i].TypeAsSignatureType;
294 RuntimeJavaType tw = twargs[i];
295 if (tw == context.JavaBase.TypeOfJavaLangObject)
296 {
297 tw = loader.FieldTypeWrapperFromSig(attr.Params[i].Sig, LoadMode.Link);
298 }
299 if (tw.IsArray)
300 {
301 Array arr = Array.CreateInstance(Type.__GetSystemType(Type.GetTypeCode(tw.ElementTypeWrapper.TypeAsArrayType)), attr.Params[i].Elements.Length);
302 for (int j = 0; j < arr.Length; j++)
303 {
304 arr.SetValue(ParseValue(loader, tw.ElementTypeWrapper, attr.Params[i].Elements[j].Value), j);
305 }
306 args[i] = arr;
307 }
308 else
309 {
310 args[i] = ParseValue(loader, tw, attr.Params[i].Value);
311 }
312 }
313 }
314
315 CustomAttributeBuilder CreateCustomAttribute(RuntimeClassLoader loader, IKVM.Tools.Importer.MapXml.Attribute attr)
316 {
317 // TODO add error handling
318 Type[] argTypes;
319 object[] args;
320 GetAttributeArgsAndTypes(loader, attr, out argTypes, out args);
321 if (attr.Type != null)
322 {
323 var t = context.Resolver.ResolveCoreType(attr.Type).AsReflection();
324 var ci = t.GetConstructor(argTypes);
325 if (ci == null)
326 throw new InvalidOperationException($"Constructor missing: {attr.Type}::<init>{attr.Sig}");
327
328 PropertyInfo[] namedProperties;
329 object[] propertyValues;
330 if (attr.Properties != null)
331 {
332 namedProperties = new PropertyInfo[attr.Properties.Length];
333 propertyValues = new object[attr.Properties.Length];
334 for (int i = 0; i < namedProperties.Length; i++)
335 {
336 namedProperties[i] = t.GetProperty(attr.Properties[i].Name);
337 propertyValues[i] = ParseValue(loader, loader.FieldTypeWrapperFromSig(attr.Properties[i].Sig, LoadMode.Link), attr.Properties[i].Value);
338 }
339 }
340 else
341 {
342 namedProperties = new PropertyInfo[0];
343 propertyValues = new object[0];
344 }
345 FieldInfo[] namedFields;
346 object[] fieldValues;
347 if (attr.Fields != null)
348 {
349 namedFields = new FieldInfo[attr.Fields.Length];
350 fieldValues = new object[attr.Fields.Length];
351 for (int i = 0; i < namedFields.Length; i++)
352 {
353 namedFields[i] = t.GetField(attr.Fields[i].Name);
354 fieldValues[i] = ParseValue(loader, loader.FieldTypeWrapperFromSig(attr.Fields[i].Sig, LoadMode.Link), attr.Fields[i].Value);
355 }
356 }
357 else
358 {
359 namedFields = new FieldInfo[0];
360 fieldValues = new object[0];
361 }
362 return new CustomAttributeBuilder(ci, args, namedProperties, propertyValues, namedFields, fieldValues);
363 }
364 else
365 {
366 if (attr.Properties != null)
367 {
368 throw new NotImplementedException("Setting property values on Java attributes is not implemented");
369 }
370 RuntimeJavaType t = loader.LoadClassByName(attr.Class);
371 FieldInfo[] namedFields;
372 object[] fieldValues;
373 if (attr.Fields != null)
374 {
375 namedFields = new FieldInfo[attr.Fields.Length];
376 fieldValues = new object[attr.Fields.Length];
377 for (int i = 0; i < namedFields.Length; i++)
378 {
379 RuntimeJavaField fw = t.GetFieldWrapper(attr.Fields[i].Name, attr.Fields[i].Sig);
380 fw.Link();
381 namedFields[i] = fw.GetField();
382 fieldValues[i] = ParseValue(loader, loader.FieldTypeWrapperFromSig(attr.Fields[i].Sig, LoadMode.Link), attr.Fields[i].Value);
383 }
384 }
385 else
386 {
387 namedFields = new FieldInfo[0];
388 fieldValues = new object[0];
389 }
390 var mw = t.GetMethod("<init>", attr.Sig, false);
391 if (mw == null)
392 {
393 throw new InvalidOperationException(string.Format("Constructor missing: {0}::<init>{1}", attr.Class, attr.Sig));
394 }
395 mw.Link();
396 ConstructorInfo ci = (mw.GetMethod() as ConstructorInfo) ?? ((MethodInfo)mw.GetMethod()).__AsConstructorInfo();
397 return new CustomAttributeBuilder(ci, args, namedFields, fieldValues);
398 }
399 }
400
401 CustomAttributeBuilder GetEditorBrowsableNever()
402 {
403 if (editorBrowsableNever == null)
404 {
405 var typeofEditorBrowsableAttribute = context.Resolver.ResolveCoreType(typeof(EditorBrowsableAttribute).FullName).AsReflection();
406 var typeofEditorBrowsableState = context.Resolver.ResolveCoreType(typeof(EditorBrowsableState).FullName).AsReflection();
407 var ctor = (ConstructorInfo)typeofEditorBrowsableAttribute.__CreateMissingMethod(ConstructorInfo.ConstructorName, CallingConventions.Standard | CallingConventions.HasThis, null, default, new Type[] { typeofEditorBrowsableState }, null);
408 editorBrowsableNever = CustomAttributeBuilder.__FromBlob(ctor, new byte[] { 01, 00, 01, 00, 00, 00, 00, 00 });
409 }
410
411 return editorBrowsableNever;
412 }
413
414 internal void SetCompilerGenerated(TypeBuilder tb)
415 {
416 compilerGeneratedAttribute ??= new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(CompilerGeneratedAttribute).FullName).AsReflection().GetConstructor(Type.EmptyTypes), Array.Empty<object>());
417 tb.SetCustomAttribute(compilerGeneratedAttribute);
418 }
419
420 internal void SetCompilerGenerated(MethodBuilder mb)
421 {
422 compilerGeneratedAttribute ??= new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(CompilerGeneratedAttribute).FullName).AsReflection().GetConstructor(Type.EmptyTypes), Array.Empty<object>());
423 mb.SetCustomAttribute(compilerGeneratedAttribute);
424 }
425
426 internal void SetEditorBrowsableNever(TypeBuilder tb)
427 {
428 tb.SetCustomAttribute(GetEditorBrowsableNever());
429 }
430
431 internal void SetEditorBrowsableNever(MethodBuilder mb)
432 {
433 mb.SetCustomAttribute(GetEditorBrowsableNever());
434 }
435
436 internal void SetEditorBrowsableNever(PropertyBuilder pb)
437 {
438 pb.SetCustomAttribute(GetEditorBrowsableNever());
439 }
440
441 internal void SetDeprecatedAttribute(MethodBuilder mb)
442 {
443 if (deprecatedAttribute == null)
444 deprecatedAttribute = new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<object>());
445
446 mb.SetCustomAttribute(deprecatedAttribute);
447 }
448
449 internal void SetDeprecatedAttribute(TypeBuilder tb)
450 {
451 if (deprecatedAttribute == null)
452 {
453 deprecatedAttribute = new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<object>());
454 }
455
456 tb.SetCustomAttribute(deprecatedAttribute);
457 }
458
459 internal void SetDeprecatedAttribute(FieldBuilder fb)
460 {
461 if (deprecatedAttribute == null)
462 deprecatedAttribute = new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<object>());
463
464 fb.SetCustomAttribute(deprecatedAttribute);
465 }
466
467 internal void SetDeprecatedAttribute(PropertyBuilder pb)
468 {
469 if (deprecatedAttribute == null)
470 {
471 deprecatedAttribute = new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<object>());
472 }
473 pb.SetCustomAttribute(deprecatedAttribute);
474 }
475
476 internal void SetThrowsAttribute(MethodBuilder mb, string[] exceptions)
477 {
478 if (exceptions != null && exceptions.Length != 0)
479 {
480 throwsAttribute ??= TypeOfThrowsAttribute.GetConstructor(new Type[] { context.Resolver.ResolveCoreType(typeof(string).FullName).MakeArrayType().AsReflection() });
481 exceptions = UnicodeUtil.EscapeInvalidSurrogates(exceptions);
482 mb.SetCustomAttribute(new CustomAttributeBuilder(throwsAttribute, new object[] { exceptions }));
483 }
484 }
485
486 internal void SetGhostInterface(TypeBuilder typeBuilder)
487 {
488 ghostInterfaceAttribute ??= new CustomAttributeBuilder(TypeOfGhostInterfaceAttribute.GetConstructor(Type.EmptyTypes), new object[0]);
489 typeBuilder.SetCustomAttribute(ghostInterfaceAttribute);
490 }
491
492 internal void SetNonNestedInnerClass(TypeBuilder typeBuilder, string className)
493 {
494 nonNestedInnerClassAttribute ??= TypeOfNonNestedInnerClassAttribute.GetConstructor(new Type[] { context.Types.String });
495 typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(nonNestedInnerClassAttribute, new object[] { UnicodeUtil.EscapeInvalidSurrogates(className) }));
496 }
497
498 internal void SetNonNestedOuterClass(TypeBuilder typeBuilder, string className)
499 {
500 nonNestedOuterClassAttribute ??= TypeOfNonNestedOuterClassAttribute.GetConstructor(new Type[] { context.Types.String });
501 typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(nonNestedOuterClassAttribute, new object[] { UnicodeUtil.EscapeInvalidSurrogates(className) }));
502 }
503
504#endif // IMPORTER
505
506 internal void HideFromReflection(MethodBuilder mb)
507 {
508 mb.SetCustomAttribute(HideFromReflectionBuilder);
509 }
510
511 internal void HideFromReflection(FieldBuilder fb)
512 {
513 fb.SetCustomAttribute(HideFromReflectionBuilder);
514 }
515
516 internal void HideFromReflection(PropertyBuilder pb)
517 {
518 pb.SetCustomAttribute(HideFromReflectionBuilder);
519 }
520
521 internal void HideFromJava(TypeBuilder typeBuilder)
522 {
523 typeBuilder.SetCustomAttribute(HideFromJavaAttributeBuilder);
524 }
525
526 internal void HideFromJava(MethodBuilder mb)
527 {
528 mb.SetCustomAttribute(HideFromJavaAttributeBuilder);
529 }
530
531 internal void HideFromJava(MethodBuilder mb, HideFromJavaFlags flags)
532 {
533 CustomAttributeBuilder cab = new CustomAttributeBuilder(TypeOfHideFromJavaAttribute.GetConstructor(new Type[] { TypeOfHideFromJavaFlags }), new object[] { flags });
534 mb.SetCustomAttribute(cab);
535 }
536
537 internal void HideFromJava(FieldBuilder fb)
538 {
539 fb.SetCustomAttribute(HideFromJavaAttributeBuilder);
540 }
541
542#if IMPORTER
543
544 internal void HideFromJava(PropertyBuilder pb)
545 {
546 pb.SetCustomAttribute(HideFromJavaAttributeBuilder);
547 }
548
549#endif
550
551 internal bool IsHideFromJava(Type type)
552 {
553 return type.IsDefined(TypeOfHideFromJavaAttribute, false) || (type.IsNested && (type.DeclaringType.IsDefined(TypeOfHideFromJavaAttribute, false) || type.Name.StartsWith("__<", StringComparison.Ordinal)));
554 }
555
556 internal bool IsHideFromJava(MemberInfo mi)
557 {
558 return (GetHideFromJavaFlags(mi) & HideFromJavaFlags.Code) != 0;
559 }
560
561 internal HideFromJavaFlags GetHideFromJavaFlags(MemberInfo mi)
562 {
563 // NOTE all privatescope fields and methods are "hideFromJava"
564 // because Java cannot deal with the potential name clashes
565 var fi = mi as FieldInfo;
566 if (fi != null && (fi.Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.PrivateScope)
567 return HideFromJavaFlags.All;
568
569 var mb = mi as MethodBase;
570 if (mb != null && (mb.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.PrivateScope)
571 return HideFromJavaFlags.All;
572 if (mi.Name.StartsWith("__<", StringComparison.Ordinal))
573 return HideFromJavaFlags.All;
574
575#if !IMPORTER && !EXPORTER
576
577 var attr = mi.GetCustomAttributes(typeofHideFromJavaAttribute, false);
578 if (attr.Length == 1)
579 return ((HideFromJavaAttribute)attr[0]).Flags;
580
581#else
582 var attr = CustomAttributeData.__GetCustomAttributes(mi, TypeOfHideFromJavaAttribute, false);
583 if (attr.Count == 1)
584 {
585 var args = attr[0].ConstructorArguments;
586 if (args.Count == 1)
587 return (HideFromJavaFlags)args[0].Value;
588
589 return HideFromJavaFlags.All;
590 }
591#endif
592
593 return HideFromJavaFlags.None;
594 }
595
596#if IMPORTER
597
598 internal void SetImplementsAttribute(TypeBuilder typeBuilder, RuntimeJavaType[] ifaceWrappers)
599 {
600 var interfaces = new string[ifaceWrappers.Length];
601 for (int i = 0; i < interfaces.Length; i++)
602 interfaces[i] = UnicodeUtil.EscapeInvalidSurrogates(ifaceWrappers[i].Name);
603
604 if (implementsAttribute == null)
605 implementsAttribute = TypeOfImplementsAttribute.GetConstructor(new Type[] { context.Resolver.ResolveCoreType(typeof(string).FullName).MakeArrayType().AsReflection() });
606
607 typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(implementsAttribute, new object[] { interfaces }));
608 }
609
610#endif
611
612 internal bool IsGhostInterface(Type type)
613 {
614 return type.IsDefined(TypeOfGhostInterfaceAttribute, false);
615 }
616
617 internal bool IsRemappedType(Type type)
618 {
619 return type.IsDefined(TypeOfRemappedTypeAttribute, false);
620 }
621
622 internal bool IsExceptionIsUnsafeForMapping(Type type)
623 {
624 return type.IsDefined(TypeOfExceptionIsUnsafeForMappingAttribute, false);
625 }
626
627 internal ModifiersAttribute GetModifiersAttribute(MemberInfo member)
628 {
629#if !IMPORTER && !EXPORTER
630 var attr = member.GetCustomAttributes(typeof(ModifiersAttribute), false);
631 return attr.Length == 1 ? (ModifiersAttribute)attr[0] : null;
632#else
633 var attr = CustomAttributeData.__GetCustomAttributes(member, TypeOfModifiersAttribute, false);
634 if (attr.Count == 1)
635 {
636 var args = attr[0].ConstructorArguments;
637 if (args.Count == 2)
638 return new ModifiersAttribute((Modifiers)args[0].Value, (bool)args[1].Value);
639
640 return new ModifiersAttribute((Modifiers)args[0].Value);
641 }
642
643 return null;
644#endif
645 }
646
647 internal ExModifiers GetModifiers(MethodBase mb, bool assemblyIsPrivate)
648 {
649 var attr = GetModifiersAttribute(mb);
650 if (attr != null)
651 return new ExModifiers(attr.Modifiers, attr.IsInternal);
652
653 Modifiers modifiers = 0;
654
655 if (mb.IsPublic)
656 {
657 modifiers |= Modifiers.Public;
658 }
659 else if (mb.IsPrivate)
660 {
661 modifiers |= Modifiers.Private;
662 }
663 else if (mb.IsFamily || mb.IsFamilyOrAssembly)
664 {
665 modifiers |= Modifiers.Protected;
666 }
667 else if (assemblyIsPrivate)
668 {
669 modifiers |= Modifiers.Private;
670 }
671
672 // NOTE Java doesn't support non-virtual methods, but we set the Final modifier for
673 // non-virtual methods to approximate the semantics
674 if ((mb.IsFinal || (!mb.IsVirtual && ((modifiers & Modifiers.Private) == 0))) && !mb.IsStatic && !mb.IsConstructor)
675 {
676 modifiers |= Modifiers.Final;
677 }
678
679 if (mb.IsAbstract)
680 {
681 modifiers |= Modifiers.Abstract;
682 }
683 else
684 {
685 // Some .NET interfaces (like System._AppDomain) have synchronized methods,
686 // Java doesn't allow synchronized on an abstract methods, so we ignore it for
687 // abstract methods.
688 if ((mb.GetMethodImplementationFlags() & MethodImplAttributes.Synchronized) != 0)
689 {
690 modifiers |= Modifiers.Synchronized;
691 }
692 }
693
694 if (mb.IsStatic)
695 {
696 modifiers |= Modifiers.Static;
697 }
698
699 if ((mb.Attributes & MethodAttributes.PinvokeImpl) != 0)
700 {
701 modifiers |= Modifiers.Native;
702 }
703
704 var parameters = mb.GetParameters();
705 if (parameters.Length > 0 && parameters[parameters.Length - 1].IsDefined(context.Resolver.ResolveCoreType(typeof(ParamArrayAttribute).FullName).AsReflection(), false))
706 modifiers |= Modifiers.VarArgs;
707
708 return new ExModifiers(modifiers, false);
709 }
710
711 internal ExModifiers GetModifiers(FieldInfo fi, bool assemblyIsPrivate)
712 {
713 var attr = GetModifiersAttribute(fi);
714 if (attr != null)
715 return new ExModifiers(attr.Modifiers, attr.IsInternal);
716
717 Modifiers modifiers = 0;
718 if (fi.IsPublic)
719 {
720 modifiers |= Modifiers.Public;
721 }
722 else if (fi.IsPrivate)
723 {
724 modifiers |= Modifiers.Private;
725 }
726 else if (fi.IsFamily || fi.IsFamilyOrAssembly)
727 {
728 modifiers |= Modifiers.Protected;
729 }
730 else if (assemblyIsPrivate)
731 {
732 modifiers |= Modifiers.Private;
733 }
734
735 if (fi.IsInitOnly || fi.IsLiteral)
736 {
737 modifiers |= Modifiers.Final;
738 }
739
740 if (fi.IsNotSerialized)
741 {
742 modifiers |= Modifiers.Transient;
743 }
744
745 if (fi.IsStatic)
746 {
747 modifiers |= Modifiers.Static;
748 }
749
750 if (Array.IndexOf(fi.GetRequiredCustomModifiers(), context.Types.IsVolatile) != -1)
751 {
752 modifiers |= Modifiers.Volatile;
753 }
754
755 return new ExModifiers(modifiers, false);
756 }
757
758 internal void SetDebuggingModes(AssemblyBuilder assemblyBuilder, DebuggableAttribute.DebuggingModes modes)
759 {
760 debuggableAttribute ??= TypeOfDebuggableAttribute.GetConstructor(new[] { TypeOfDebuggableAttribute.GetNestedType(nameof(DebuggableAttribute.DebuggingModes)) });
761 assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(debuggableAttribute, new object[] { modes }));
762 }
763
764#if IMPORTER
765
766 internal void SetModifiers(MethodBuilder mb, Modifiers modifiers, bool isInternal)
767 {
768 CustomAttributeBuilder customAttributeBuilder;
769 if (isInternal)
770 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers, context.Types.Boolean }), new object[] { modifiers, isInternal });
771 else
772 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers }), new object[] { modifiers });
773
774 mb.SetCustomAttribute(customAttributeBuilder);
775 }
776
777 internal void SetModifiers(FieldBuilder fb, Modifiers modifiers, bool isInternal)
778 {
779 CustomAttributeBuilder customAttributeBuilder;
780 if (isInternal)
781 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers, context.Types.Boolean }), new object[] { modifiers, isInternal });
782 else
783 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers }), new object[] { modifiers });
784
785 fb.SetCustomAttribute(customAttributeBuilder);
786 }
787
788 internal void SetModifiers(PropertyBuilder pb, Modifiers modifiers, bool isInternal)
789 {
790 CustomAttributeBuilder customAttributeBuilder;
791 if (isInternal)
792 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers, context.Types.Boolean }), new object[] { modifiers, isInternal });
793 else
794 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers }), new object[] { modifiers });
795
796 pb.SetCustomAttribute(customAttributeBuilder);
797 }
798
799 internal void SetModifiers(TypeBuilder tb, Modifiers modifiers, bool isInternal)
800 {
801 CustomAttributeBuilder customAttributeBuilder;
802 if (isInternal)
803 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers, context.Types.Boolean }), new object[] { modifiers, isInternal });
804 else
805 customAttributeBuilder = new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(new Type[] { TypeOfModifiers }), new object[] { modifiers });
806
807 tb.SetCustomAttribute(customAttributeBuilder);
808 }
809
810 internal void SetNameSig(MethodBuilder mb, string name, string sig)
811 {
812 var customAttributeBuilder = new CustomAttributeBuilder(TypeOfNameSigAttribute.GetConstructor(new Type[] { context.Types.String, context.Types.String }), new object[] { UnicodeUtil.EscapeInvalidSurrogates(name), UnicodeUtil.EscapeInvalidSurrogates(sig) });
813 mb.SetCustomAttribute(customAttributeBuilder);
814 }
815
816 internal void SetInnerClass(TypeBuilder typeBuilder, string innerClass, Modifiers modifiers)
817 {
818 var argTypes = new Type[] { context.Types.String, TypeOfModifiers };
819 var args = new object[] { UnicodeUtil.EscapeInvalidSurrogates(innerClass), modifiers };
820 var ci = TypeOfInnerClassAttribute.GetConstructor(argTypes);
821 var customAttributeBuilder = new CustomAttributeBuilder(ci, args);
822 typeBuilder.SetCustomAttribute(customAttributeBuilder);
823 }
824
825 internal void SetSourceFile(TypeBuilder typeBuilder, string filename)
826 {
827 sourceFileAttribute ??= TypeOfSourceFileAttribute.GetConstructor(new Type[] { context.Types.String });
828 typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(sourceFileAttribute, new object[] { filename }));
829 }
830
831 internal void SetSourceFile(ModuleBuilder moduleBuilder, string filename)
832 {
833 sourceFileAttribute ??= TypeOfSourceFileAttribute.GetConstructor(new Type[] { context.Types.String });
834 moduleBuilder.SetCustomAttribute(new CustomAttributeBuilder(sourceFileAttribute, new object[] { filename }));
835 }
836
837 internal void SetLineNumberTable(MethodBuilder mb, IKVM.Attributes.LineNumberTableAttribute.LineNumberWriter writer)
838 {
839 object arg;
840 ConstructorInfo con;
841 if (writer.Count == 1)
842 {
843 lineNumberTableAttribute2 ??= TypeOfLineNumberTableAttribute.GetConstructor(new Type[] { context.Types.UInt16 });
844 con = lineNumberTableAttribute2;
845 arg = (ushort)writer.LineNo;
846 }
847 else
848 {
849 lineNumberTableAttribute1 ??= TypeOfLineNumberTableAttribute.GetConstructor(new Type[] { context.Resolver.ResolveCoreType(typeof(byte).FullName).MakeArrayType().AsReflection() });
850 con = lineNumberTableAttribute1;
851 arg = writer.ToArray();
852 }
853 mb.SetCustomAttribute(new CustomAttributeBuilder(con, new object[] { arg }));
854 }
855
856 internal void SetEnclosingMethodAttribute(TypeBuilder tb, string className, string methodName, string methodSig)
857 {
858 if (enclosingMethodAttribute == null)
859 {
860 enclosingMethodAttribute = TypeOfEnclosingMethodAttribute.GetConstructor(new Type[] { context.Types.String, context.Types.String, context.Types.String });
861 }
862 tb.SetCustomAttribute(new CustomAttributeBuilder(enclosingMethodAttribute,
863 new object[] { UnicodeUtil.EscapeInvalidSurrogates(className), UnicodeUtil.EscapeInvalidSurrogates(methodName), UnicodeUtil.EscapeInvalidSurrogates(methodSig) }));
864 }
865
866 internal void SetSignatureAttribute(TypeBuilder tb, string signature)
867 {
868 signatureAttribute ??= TypeOfSignatureAttribute.GetConstructor(new Type[] { context.Types.String });
869 tb.SetCustomAttribute(new CustomAttributeBuilder(signatureAttribute, new object[] { UnicodeUtil.EscapeInvalidSurrogates(signature) }));
870 }
871
872 internal void SetSignatureAttribute(FieldBuilder fb, string signature)
873 {
874 signatureAttribute ??= TypeOfSignatureAttribute.GetConstructor(new Type[] { context.Types.String });
875 fb.SetCustomAttribute(new CustomAttributeBuilder(signatureAttribute, new object[] { UnicodeUtil.EscapeInvalidSurrogates(signature) }));
876 }
877
878 internal void SetSignatureAttribute(MethodBuilder mb, string signature)
879 {
880 signatureAttribute ??= TypeOfSignatureAttribute.GetConstructor(new Type[] { context.Types.String });
881 mb.SetCustomAttribute(new CustomAttributeBuilder(signatureAttribute, new object[] { UnicodeUtil.EscapeInvalidSurrogates(signature) }));
882 }
883
884 internal void SetMethodParametersAttribute(MethodBuilder mb, Modifiers[] modifiers)
885 {
886 methodParametersAttribute ??= TypeOfMethodParametersAttribute.GetConstructor(new[] { TypeOfModifiers.MakeArrayType() });
887 mb.SetCustomAttribute(new CustomAttributeBuilder(methodParametersAttribute, new[] { modifiers }));
888 }
889
890 internal void SetRuntimeVisibleTypeAnnotationsAttribute(TypeBuilder tb, ref readonly TypeAnnotationTable table)
891 {
892 var builder = new BlobBuilder();
893 var encoder = new TypeAnnotationTableEncoder(builder);
894 table.WriteTo(ref encoder);
895
896 runtimeVisibleTypeAnnotationsAttribute ??= TypeOfRuntimeVisibleTypeAnnotationsAttribute.GetConstructor([context.Types.Byte.MakeArrayType()]);
897 tb.SetCustomAttribute(new CustomAttributeBuilder(runtimeVisibleTypeAnnotationsAttribute, [builder.ToArray()]));
898 }
899
900 internal void SetRuntimeVisibleTypeAnnotationsAttribute(FieldBuilder fb, ref readonly TypeAnnotationTable table)
901 {
902 var builder = new BlobBuilder();
903 var encoder = new TypeAnnotationTableEncoder(builder);
904 table.WriteTo(ref encoder);
905
906 runtimeVisibleTypeAnnotationsAttribute ??= TypeOfRuntimeVisibleTypeAnnotationsAttribute.GetConstructor([context.Types.Byte.MakeArrayType()]);
907 fb.SetCustomAttribute(new CustomAttributeBuilder(runtimeVisibleTypeAnnotationsAttribute, [builder.ToArray()]));
908 }
909
910 internal void SetRuntimeVisibleTypeAnnotationsAttribute(MethodBuilder mb, ref readonly TypeAnnotationTable table)
911 {
912 var builder = new BlobBuilder();
913 var encoder = new TypeAnnotationTableEncoder(builder);
914 table.WriteTo(ref encoder);
915
916 runtimeVisibleTypeAnnotationsAttribute ??= TypeOfRuntimeVisibleTypeAnnotationsAttribute.GetConstructor([context.Types.Byte.MakeArrayType()]);
917 mb.SetCustomAttribute(new CustomAttributeBuilder(runtimeVisibleTypeAnnotationsAttribute, [builder.ToArray()]));
918 }
919
920 internal void SetConstantPoolAttribute(TypeBuilder tb, object[] constantPool)
921 {
922 constantPoolAttribute ??= TypeOfConstantPoolAttribute.GetConstructor([context.Types.Object.MakeArrayType()]);
923 tb.SetCustomAttribute(new CustomAttributeBuilder(constantPoolAttribute, [constantPool]));
924 }
925
926 internal void SetParamArrayAttribute(ParameterBuilder pb)
927 {
928 paramArrayAttribute ??= new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ParamArrayAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<object>());
929 pb.SetCustomAttribute(paramArrayAttribute);
930 }
931
932#endif // IMPORTER
933
934 internal NameSigAttribute GetNameSig(MemberInfo member)
935 {
936#if !IMPORTER && !EXPORTER
937 var attr = member.GetCustomAttributes(typeof(NameSigAttribute), false);
938 return attr.Length == 1 ? (NameSigAttribute)attr[0] : null;
939#else
940 foreach (var cad in CustomAttributeData.__GetCustomAttributes(member, TypeOfNameSigAttribute, false))
941 {
942 var args = cad.ConstructorArguments;
943 return new NameSigAttribute((string)args[0].Value, (string)args[1].Value);
944 }
945
946 return null;
947#endif
948 }
949
950 internal T[] DecodeArray<T>(CustomAttributeTypedArgument arg)
951 {
952 var elems = (IList<CustomAttributeTypedArgument>)arg.Value;
953 var arr = new T[elems.Count];
954 for (int i = 0; i < arr.Length; i++)
955 arr[i] = (T)elems[i].Value;
956
957 return arr;
958 }
959
960 internal ImplementsAttribute GetImplements(Type type)
961 {
962#if !IMPORTER && !EXPORTER
963 var attribs = type.GetCustomAttributes(typeof(ImplementsAttribute), false);
964 return attribs.Length == 1 ? (ImplementsAttribute)attribs[0] : null;
965#else
966 foreach (var cad in CustomAttributeData.__GetCustomAttributes(type, TypeOfImplementsAttribute, false))
967 {
968 var args = cad.ConstructorArguments;
969 return new ImplementsAttribute(DecodeArray<string>(args[0]));
970 }
971
972 return null;
973#endif
974 }
975
976 internal ThrowsAttribute GetThrows(MethodBase mb)
977 {
978#if !IMPORTER && !EXPORTER
979 var attribs = mb.GetCustomAttributes(typeof(ThrowsAttribute), false);
980 return attribs.Length == 1 ? (ThrowsAttribute)attribs[0] : null;
981#else
982 foreach (var cad in CustomAttributeData.__GetCustomAttributes(mb, TypeOfThrowsAttribute, false))
983 {
984 var args = cad.ConstructorArguments;
985 if (args[0].ArgumentType == context.Types.String.MakeArrayType())
986 {
987 return new ThrowsAttribute(DecodeArray<string>(args[0]));
988 }
989 else if (args[0].ArgumentType == context.Types.Type.MakeArrayType())
990 {
991 return new ThrowsAttribute(DecodeArray<Type>(args[0]));
992 }
993 else
994 {
995 return new ThrowsAttribute((Type)args[0].Value);
996 }
997 }
998
999 return null;
1000#endif
1001 }
1002
1003 internal string[] GetNonNestedInnerClasses(Type t)
1004 {
1005#if !IMPORTER && !EXPORTER
1006 var attribs = t.GetCustomAttributes(typeof(NonNestedInnerClassAttribute), false);
1007 var classes = new string[attribs.Length];
1008 for (int i = 0; i < attribs.Length; i++)
1009 classes[i] = ((NonNestedInnerClassAttribute)attribs[i]).InnerClassName;
1010
1011 return classes;
1012#else
1013 var list = new List<string>();
1014 foreach (var cad in CustomAttributeData.__GetCustomAttributes(t, TypeOfNonNestedInnerClassAttribute, false))
1015 {
1016 var args = cad.ConstructorArguments;
1017 list.Add(UnicodeUtil.UnescapeInvalidSurrogates((string)args[0].Value));
1018 }
1019
1020 return list.ToArray();
1021#endif
1022 }
1023
1024 internal string GetNonNestedOuterClasses(Type t)
1025 {
1026#if !IMPORTER && !EXPORTER
1027 var attribs = t.GetCustomAttributes(typeof(NonNestedOuterClassAttribute), false);
1028 return attribs.Length == 1 ? ((NonNestedOuterClassAttribute)attribs[0]).OuterClassName : null;
1029#else
1030 foreach (var cad in CustomAttributeData.__GetCustomAttributes(t, TypeOfNonNestedOuterClassAttribute, false))
1031 {
1032 var args = cad.ConstructorArguments;
1033 return UnicodeUtil.UnescapeInvalidSurrogates((string)args[0].Value);
1034 }
1035 return null;
1036#endif
1037 }
1038
1039 internal IKVM.Attributes.SignatureAttribute GetSignature(MemberInfo member)
1040 {
1041#if !IMPORTER && !EXPORTER
1042 var attribs = member.GetCustomAttributes(typeof(IKVM.Attributes.SignatureAttribute), false);
1043 return attribs.Length == 1 ? (IKVM.Attributes.SignatureAttribute)attribs[0] : null;
1044#else
1045 foreach (var cad in CustomAttributeData.__GetCustomAttributes(member, TypeOfSignatureAttribute, false))
1046 {
1047 var args = cad.ConstructorArguments;
1048 return new IKVM.Attributes.SignatureAttribute((string)args[0].Value);
1049 }
1050
1051 return null;
1052#endif
1053 }
1054
1055 internal IKVM.Attributes.MethodParametersAttribute GetMethodParameters(MethodBase method)
1056 {
1057#if !IMPORTER && !EXPORTER
1058 var attribs = method.GetCustomAttributes(typeof(IKVM.Attributes.MethodParametersAttribute), false);
1059 return attribs.Length == 1 ? (IKVM.Attributes.MethodParametersAttribute)attribs[0] : null;
1060#else
1061 foreach (var cad in CustomAttributeData.__GetCustomAttributes(method, TypeOfMethodParametersAttribute, false))
1062 {
1063 var args = cad.ConstructorArguments;
1064 return new IKVM.Attributes.MethodParametersAttribute(DecodeArray<Modifiers>(args[0]));
1065 }
1066 return null;
1067#endif
1068 }
1069
1070 internal object[] GetConstantPool(Type type)
1071 {
1072#if !IMPORTER && !EXPORTER
1073 var attribs = type.GetCustomAttributes(typeof(ConstantPoolAttribute), false);
1074 return attribs.Length == 1 ? ((ConstantPoolAttribute)attribs[0]).constantPool : null;
1075#else
1076 foreach (var cad in CustomAttributeData.__GetCustomAttributes(type, TypeOfConstantPoolAttribute, false))
1077 return ConstantPoolAttribute.Decompress(DecodeArray<object>(cad.ConstructorArguments[0]));
1078
1079 return null;
1080#endif
1081 }
1082
1083 internal byte[] GetRuntimeVisibleTypeAnnotations(MemberInfo member)
1084 {
1085#if !IMPORTER && !EXPORTER
1086 object[] attribs = member.GetCustomAttributes(typeof(IKVM.Attributes.RuntimeVisibleTypeAnnotationsAttribute), false);
1087 return attribs.Length == 1 ? ((IKVM.Attributes.RuntimeVisibleTypeAnnotationsAttribute)attribs[0]).data : null;
1088#else
1089 foreach (CustomAttributeData cad in CustomAttributeData.__GetCustomAttributes(member, TypeOfRuntimeVisibleTypeAnnotationsAttribute, false))
1090 {
1091 return DecodeArray<byte>(cad.ConstructorArguments[0]);
1092 }
1093
1094 return null;
1095#endif
1096 }
1097
1098 internal InnerClassAttribute GetInnerClass(Type type)
1099 {
1100#if !IMPORTER && !EXPORTER
1101 object[] attribs = type.GetCustomAttributes(typeof(InnerClassAttribute), false);
1102 return attribs.Length == 1 ? (InnerClassAttribute)attribs[0] : null;
1103#else
1104 foreach (CustomAttributeData cad in CustomAttributeData.__GetCustomAttributes(type, TypeOfInnerClassAttribute, false))
1105 {
1106 IList<CustomAttributeTypedArgument> args = cad.ConstructorArguments;
1107 return new InnerClassAttribute((string)args[0].Value, (Modifiers)args[1].Value);
1108 }
1109 return null;
1110#endif
1111 }
1112
1113 internal RemappedInterfaceMethodAttribute[] GetRemappedInterfaceMethods(Type type)
1114 {
1115#if !IMPORTER && !EXPORTER
1116 var attr = type.GetCustomAttributes(typeof(RemappedInterfaceMethodAttribute), false);
1117 var attr1 = new RemappedInterfaceMethodAttribute[attr.Length];
1118 Array.Copy(attr, attr1, attr.Length);
1119 return attr1;
1120#else
1121 var attrs = new List<RemappedInterfaceMethodAttribute>();
1122 foreach (var cad in CustomAttributeData.__GetCustomAttributes(type, TypeOfRemappedInterfaceMethodAttribute, false))
1123 {
1124 var args = cad.ConstructorArguments;
1125 attrs.Add(new RemappedInterfaceMethodAttribute((string)args[0].Value, (string)args[1].Value, DecodeArray<string>(args[2])));
1126 }
1127
1128 return attrs.ToArray();
1129#endif
1130 }
1131
1132 internal RemappedTypeAttribute GetRemappedType(Type type)
1133 {
1134#if !IMPORTER && !EXPORTER
1135 var attribs = type.GetCustomAttributes(typeof(RemappedTypeAttribute), false);
1136 return attribs.Length == 1 ? (RemappedTypeAttribute)attribs[0] : null;
1137#else
1138 foreach (var cad in CustomAttributeData.__GetCustomAttributes(type, TypeOfRemappedTypeAttribute, false))
1139 {
1140 var args = cad.ConstructorArguments;
1141 return new RemappedTypeAttribute((Type)args[0].Value);
1142 }
1143
1144 return null;
1145#endif
1146 }
1147
1148 internal RemappedClassAttribute[] GetRemappedClasses(Assembly coreAssembly)
1149 {
1150#if !IMPORTER && !EXPORTER
1151 var attr = coreAssembly.GetCustomAttributes(typeof(RemappedClassAttribute), false);
1152 var attr1 = new RemappedClassAttribute[attr.Length];
1153 Array.Copy(attr, attr1, attr.Length);
1154 return attr1;
1155#else
1156 var attrs = new List<RemappedClassAttribute>();
1157 foreach (var cad in CustomAttributeData.__GetCustomAttributes(coreAssembly, TypeOfRemappedClassAttribute, false))
1158 {
1159 var args = cad.ConstructorArguments;
1160 attrs.Add(new RemappedClassAttribute((string)args[0].Value, (Type)args[1].Value));
1161 }
1162
1163 return attrs.ToArray();
1164#endif
1165 }
1166
1167 internal string GetAnnotationAttributeType(Type type)
1168 {
1169#if !IMPORTER && !EXPORTER
1170 var attr = type.GetCustomAttributes(typeof(AnnotationAttributeAttribute), false);
1171 if (attr.Length == 1)
1172 return ((AnnotationAttributeAttribute)attr[0]).AttributeType;
1173
1174 return null;
1175#else
1176 foreach (var cad in CustomAttributeData.__GetCustomAttributes(type, TypeOfAnnotationAttributeAttribute, false))
1177 return UnicodeUtil.UnescapeInvalidSurrogates((string)cad.ConstructorArguments[0].Value);
1178
1179 return null;
1180#endif
1181 }
1182
1183 internal AssemblyName[] GetInternalsVisibleToAttributes(Assembly assembly)
1184 {
1185 var list = new List<AssemblyName>();
1186 foreach (var cad in CustomAttributeData.GetCustomAttributes(assembly))
1187 {
1188 if (cad.Constructor.DeclaringType == context.Resolver.ResolveCoreType(typeof(InternalsVisibleToAttribute).FullName).AsReflection())
1189 {
1190 try
1191 {
1192 list.Add(new AssemblyName((string)cad.ConstructorArguments[0].Value));
1193 }
1194 catch
1195 {
1196 // HACK since there is no list of exception that the AssemblyName constructor can throw, we simply catch all
1197 }
1198 }
1199 }
1200
1201 return list.ToArray();
1202 }
1203
1204 internal bool IsJavaModule(Module mod)
1205 {
1206 return mod.IsDefined(TypeOfJavaModuleAttribute, false);
1207 }
1208
1209 internal object[] GetJavaModuleAttributes(Module mod)
1210 {
1211#if !IMPORTER && !EXPORTER
1212 return mod.GetCustomAttributes(TypeOfJavaModuleAttribute, false);
1213#else
1214 List<JavaModuleAttribute> attrs = new List<JavaModuleAttribute>();
1215 foreach (CustomAttributeData cad in CustomAttributeData.__GetCustomAttributes(mod, TypeOfJavaModuleAttribute, false))
1216 {
1217 IList<CustomAttributeTypedArgument> args = cad.ConstructorArguments;
1218 if (args.Count == 0)
1219 {
1220 attrs.Add(new JavaModuleAttribute());
1221 }
1222 else
1223 {
1224 attrs.Add(new JavaModuleAttribute(DecodeArray<string>(args[0])));
1225 }
1226 }
1227 return attrs.ToArray();
1228#endif
1229 }
1230
1231 internal bool IsNoPackagePrefix(Type type)
1232 {
1233 return type.IsDefined(TypeOfNoPackagePrefixAttribute, false) || type.Assembly.IsDefined(TypeOfNoPackagePrefixAttribute, false);
1234 }
1235
1236 internal bool HasEnclosingMethodAttribute(Type type)
1237 {
1238 return type.IsDefined(TypeOfEnclosingMethodAttribute, false);
1239 }
1240
1241 internal IKVM.Attributes.EnclosingMethodAttribute GetEnclosingMethodAttribute(Type type)
1242 {
1243#if !IMPORTER && !EXPORTER
1244 var attr = type.GetCustomAttributes(typeof(IKVM.Attributes.EnclosingMethodAttribute), false);
1245 if (attr.Length == 1)
1246 return ((IKVM.Attributes.EnclosingMethodAttribute)attr[0]).SetClassName(context, type);
1247
1248 return null;
1249#else
1250 foreach (var cad in CustomAttributeData.__GetCustomAttributes(type, TypeOfEnclosingMethodAttribute, false))
1251 return new IKVM.Attributes.EnclosingMethodAttribute((string)cad.ConstructorArguments[0].Value, (string)cad.ConstructorArguments[1].Value, (string)cad.ConstructorArguments[2].Value).SetClassName(context, type);
1252
1253 return null;
1254#endif
1255 }
1256
1257#if IMPORTER
1258
1259 internal void SetRemappedClass(AssemblyBuilder assemblyBuilder, string name, Type shadowType)
1260 {
1261 var remappedClassAttribute = TypeOfRemappedClassAttribute.GetConstructor(new Type[] { context.Types.String, context.Types.Type });
1262 assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(remappedClassAttribute, new object[] { name, shadowType }));
1263 }
1264
1265 internal void SetRemappedType(TypeBuilder typeBuilder, Type shadowType)
1266 {
1267 var remappedTypeAttribute = TypeOfRemappedTypeAttribute.GetConstructor(new Type[] { context.Types.Type });
1268 typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(remappedTypeAttribute, new object[] { shadowType }));
1269 }
1270
1271 internal void SetRemappedInterfaceMethod(TypeBuilder typeBuilder, string name, string mappedTo, string[] throws)
1272 {
1273 var cab = new CustomAttributeBuilder(TypeOfRemappedInterfaceMethodAttribute.GetConstructor(new Type[] { context.Types.String, context.Types.String, context.Types.String.MakeArrayType() }), new object[] { name, mappedTo, throws });
1274 typeBuilder.SetCustomAttribute(cab);
1275 }
1276
1277 internal void SetExceptionIsUnsafeForMapping(TypeBuilder typeBuilder)
1278 {
1279 var cab = new CustomAttributeBuilder(TypeOfExceptionIsUnsafeForMappingAttribute.GetConstructor(Type.EmptyTypes), Array.Empty<object>());
1280 typeBuilder.SetCustomAttribute(cab);
1281 }
1282
1283#endif
1284
1285 internal void SetRuntimeCompatibilityAttribute(AssemblyBuilder assemblyBuilder)
1286 {
1287 var runtimeCompatibilityAttribute = context.Resolver.ResolveCoreType(typeof(RuntimeCompatibilityAttribute).FullName).AsReflection();
1288 assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(runtimeCompatibilityAttribute.GetConstructor(Type.EmptyTypes), Array.Empty<object>(), new PropertyInfo[] { runtimeCompatibilityAttribute.GetProperty("WrapNonExceptionThrows") }, new object[] { true }, Array.Empty<FieldInfo>(), Array.Empty<object>()));
1289 }
1290
1291 internal void SetInternalsVisibleToAttribute(AssemblyBuilder assemblyBuilder, string assemblyName)
1292 {
1293 var internalsVisibleToAttribute = context.Resolver.ResolveCoreType(typeof(InternalsVisibleToAttribute).FullName).AsReflection();
1294 var cab = new CustomAttributeBuilder(internalsVisibleToAttribute.GetConstructor(new Type[] { context.Types.String }), new object[] { assemblyName });
1295 assemblyBuilder.SetCustomAttribute(cab);
1296 }
1297
1298 }
1299
1300}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.Assembly Assembly
IKVM.Reflection.AssemblyName AssemblyName
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.FieldInfo FieldInfo
IKVM.Reflection.MemberInfo MemberInfo
IKVM.Reflection.PropertyInfo PropertyInfo
IKVM.Reflection.MethodInfo MethodInfo
IKVM.Reflection.MethodBase MethodBase
Whenever the VM or compiler generates a helper class/method/field, it should be marked with this cust...
Provides methods to help in generating IKVM attributes.
AttributeHelper(RuntimeContext context)
Initializes a new instance.
Runtime support for a class loader.
Maintains services relevant to an instane of the IKVM runtime.
@ HideFromReflection
Member should be hidden from Java reflection.
static DiagnosticEvent MapFileTypeNotFound(string arg0, Exception? exception=null, DiagnosticLocation location=default)
The 'MapFileTypeNotFound' diagnostic.