63 CustomAttributeBuilder compilerGeneratedAttribute;
64 CustomAttributeBuilder ghostInterfaceAttribute;
65 CustomAttributeBuilder deprecatedAttribute;
66 CustomAttributeBuilder editorBrowsableNever;
77 CustomAttributeBuilder paramArrayAttribute;
82 Type typeofSourceFileAttribute;
83 Type typeofLineNumberTableAttribute;
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;
116 Type TypeOfModifiers => typeofModifiers ??= LoadType(typeof(
Modifiers));
124 Type TypeOfRemappedClassAttribute => typeofRemappedClassAttribute ??= LoadType(typeof(RemappedClassAttribute));
126 Type TypeOfRemappedTypeAttribute => typeofRemappedTypeAttribute ??= LoadType(typeof(RemappedTypeAttribute));
146 Type TypeOfThrowsAttribute => typeofThrowsAttribute ??= LoadType(typeof(ThrowsAttribute));
168 Type TypeOfDebuggableAttribute => typeofDebuggableAttribute ??= context.Resolver.ResolveCoreType(typeof(DebuggableAttribute).FullName).AsReflection();
170 CustomAttributeBuilder HideFromJavaAttributeBuilder => hideFromJavaAttribute ??=
new CustomAttributeBuilder(TypeOfHideFromJavaAttribute.GetConstructor(
Type.EmptyTypes), Array.Empty<
object>());
172 CustomAttributeBuilder HideFromReflectionBuilder => hideFromReflection ??=
new CustomAttributeBuilder(TypeOfHideFromJavaAttribute.GetConstructor(
new[] { TypeOfHideFromJavaFlags }),
new object[] { HideFromJavaFlags.Reflection | HideFromJavaFlags.StackTrace | HideFromJavaFlags.StackWalk });
181 return context.Resolver.ResolveRuntimeType(t.FullName).AsReflection();
190 this.context = context ??
throw new ArgumentNullException(nameof(context));
197 if (tw == context.JavaBase.TypeOfJavaLangString)
201 else if (tw.IsUnloadable)
205 else if (tw.TypeAsTBD.IsEnum)
207 return EnumHelper.Parse(context, tw.TypeAsTBD, val);
209 else if (tw.TypeAsTBD == context.Types.Type)
211 var valtw = loader.TryLoadClassByName(val);
213 return valtw.TypeAsBaseType;
215 return context.StaticCompiler.Universe.GetType(val,
true);
217 else if (tw == context.PrimitiveJavaTypeFactory.BOOLEAN)
219 return bool.Parse(val);
221 else if (tw == context.PrimitiveJavaTypeFactory.BYTE)
223 return (
byte)sbyte.Parse(val);
225 else if (tw == context.PrimitiveJavaTypeFactory.CHAR)
227 return char.Parse(val);
229 else if (tw == context.PrimitiveJavaTypeFactory.SHORT)
231 return short.Parse(val);
233 else if (tw == context.PrimitiveJavaTypeFactory.INT)
235 return int.Parse(val);
237 else if (tw == context.PrimitiveJavaTypeFactory.FLOAT)
239 return float.Parse(val);
241 else if (tw == context.PrimitiveJavaTypeFactory.LONG)
243 return long.Parse(val);
245 else if (tw == context.PrimitiveJavaTypeFactory.DOUBLE)
247 return double.Parse(val);
251 throw new NotImplementedException();
257 tb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
262 fb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
267 pb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
272 mb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
277 pb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
282 ab.SetCustomAttribute(CreateCustomAttribute(loader, attr));
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++)
293 argTypes[i] = twargs[i].TypeAsSignatureType;
294 RuntimeJavaType tw = twargs[i];
295 if (tw == context.JavaBase.TypeOfJavaLangObject)
297 tw = loader.FieldTypeWrapperFromSig(attr.Params[i].Sig,
LoadMode.Link);
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++)
304 arr.SetValue(ParseValue(loader, tw.ElementTypeWrapper, attr.Params[i].Elements[j].Value), j);
310 args[i] = ParseValue(loader, tw, attr.Params[i].Value);
320 GetAttributeArgsAndTypes(loader, attr, out argTypes, out args);
321 if (attr.Type !=
null)
323 var t = context.Resolver.ResolveCoreType(attr.Type).AsReflection();
324 var ci = t.GetConstructor(argTypes);
326 throw new InvalidOperationException($
"Constructor missing: {attr.Type}::<init>{attr.Sig}");
329 object[] propertyValues;
330 if (attr.Properties !=
null)
332 namedProperties =
new PropertyInfo[attr.Properties.Length];
333 propertyValues =
new object[attr.Properties.Length];
334 for (
int i = 0; i < namedProperties.Length; i++)
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);
343 propertyValues =
new object[0];
346 object[] fieldValues;
347 if (attr.Fields !=
null)
349 namedFields =
new FieldInfo[attr.Fields.Length];
350 fieldValues =
new object[attr.Fields.Length];
351 for (
int i = 0; i < namedFields.Length; i++)
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);
360 fieldValues =
new object[0];
362 return new CustomAttributeBuilder(ci, args, namedProperties, propertyValues, namedFields, fieldValues);
366 if (attr.Properties !=
null)
368 throw new NotImplementedException(
"Setting property values on Java attributes is not implemented");
370 RuntimeJavaType t = loader.LoadClassByName(attr.Class);
372 object[] fieldValues;
373 if (attr.Fields !=
null)
375 namedFields =
new FieldInfo[attr.Fields.Length];
376 fieldValues =
new object[attr.Fields.Length];
377 for (
int i = 0; i < namedFields.Length; i++)
379 RuntimeJavaField fw = t.GetFieldWrapper(attr.Fields[i].Name, attr.Fields[i].Sig);
381 namedFields[i] = fw.GetField();
382 fieldValues[i] = ParseValue(loader, loader.FieldTypeWrapperFromSig(attr.Fields[i].Sig,
LoadMode.Link), attr.Fields[i].Value);
388 fieldValues =
new object[0];
390 var mw = t.GetMethod(
"<init>", attr.Sig,
false);
393 throw new InvalidOperationException(
string.Format(
"Constructor missing: {0}::<init>{1}", attr.Class, attr.Sig));
397 return new CustomAttributeBuilder(ci, args, namedFields, fieldValues);
401 CustomAttributeBuilder GetEditorBrowsableNever()
403 if (editorBrowsableNever ==
null)
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 });
411 return editorBrowsableNever;
414 internal void SetCompilerGenerated(TypeBuilder tb)
416 compilerGeneratedAttribute ??=
new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(CompilerGeneratedAttribute).FullName).AsReflection().GetConstructor(
Type.EmptyTypes), Array.Empty<
object>());
417 tb.SetCustomAttribute(compilerGeneratedAttribute);
420 internal void SetCompilerGenerated(MethodBuilder mb)
422 compilerGeneratedAttribute ??=
new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(CompilerGeneratedAttribute).FullName).AsReflection().GetConstructor(
Type.EmptyTypes), Array.Empty<
object>());
423 mb.SetCustomAttribute(compilerGeneratedAttribute);
426 internal void SetEditorBrowsableNever(TypeBuilder tb)
428 tb.SetCustomAttribute(GetEditorBrowsableNever());
431 internal void SetEditorBrowsableNever(MethodBuilder mb)
433 mb.SetCustomAttribute(GetEditorBrowsableNever());
436 internal void SetEditorBrowsableNever(PropertyBuilder pb)
438 pb.SetCustomAttribute(GetEditorBrowsableNever());
441 internal void SetDeprecatedAttribute(MethodBuilder mb)
443 if (deprecatedAttribute ==
null)
444 deprecatedAttribute =
new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<
object>());
446 mb.SetCustomAttribute(deprecatedAttribute);
449 internal void SetDeprecatedAttribute(TypeBuilder tb)
451 if (deprecatedAttribute ==
null)
453 deprecatedAttribute =
new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<
object>());
456 tb.SetCustomAttribute(deprecatedAttribute);
459 internal void SetDeprecatedAttribute(FieldBuilder fb)
461 if (deprecatedAttribute ==
null)
462 deprecatedAttribute =
new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<
object>());
464 fb.SetCustomAttribute(deprecatedAttribute);
467 internal void SetDeprecatedAttribute(PropertyBuilder pb)
469 if (deprecatedAttribute ==
null)
471 deprecatedAttribute =
new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ObsoleteAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<
object>());
473 pb.SetCustomAttribute(deprecatedAttribute);
476 internal void SetThrowsAttribute(MethodBuilder mb,
string[] exceptions)
478 if (exceptions !=
null && exceptions.Length != 0)
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 }));
486 internal void SetGhostInterface(TypeBuilder typeBuilder)
488 ghostInterfaceAttribute ??=
new CustomAttributeBuilder(TypeOfGhostInterfaceAttribute.GetConstructor(
Type.EmptyTypes),
new object[0]);
489 typeBuilder.SetCustomAttribute(ghostInterfaceAttribute);
492 internal void SetNonNestedInnerClass(TypeBuilder typeBuilder,
string className)
494 nonNestedInnerClassAttribute ??= TypeOfNonNestedInnerClassAttribute.GetConstructor(
new Type[] { context.Types.String });
495 typeBuilder.SetCustomAttribute(
new CustomAttributeBuilder(nonNestedInnerClassAttribute,
new object[] { UnicodeUtil.EscapeInvalidSurrogates(className) }));
498 internal void SetNonNestedOuterClass(TypeBuilder typeBuilder,
string className)
500 nonNestedOuterClassAttribute ??= TypeOfNonNestedOuterClassAttribute.GetConstructor(
new Type[] { context.Types.String });
501 typeBuilder.SetCustomAttribute(
new CustomAttributeBuilder(nonNestedOuterClassAttribute,
new object[] { UnicodeUtil.EscapeInvalidSurrogates(className) }));
508 mb.SetCustomAttribute(HideFromReflectionBuilder);
513 fb.SetCustomAttribute(HideFromReflectionBuilder);
518 pb.SetCustomAttribute(HideFromReflectionBuilder);
521 internal void HideFromJava(TypeBuilder typeBuilder)
523 typeBuilder.SetCustomAttribute(HideFromJavaAttributeBuilder);
526 internal void HideFromJava(MethodBuilder mb)
528 mb.SetCustomAttribute(HideFromJavaAttributeBuilder);
533 CustomAttributeBuilder cab =
new CustomAttributeBuilder(TypeOfHideFromJavaAttribute.GetConstructor(
new Type[] { TypeOfHideFromJavaFlags }),
new object[] { flags });
534 mb.SetCustomAttribute(cab);
537 internal void HideFromJava(FieldBuilder fb)
539 fb.SetCustomAttribute(HideFromJavaAttributeBuilder);
544 internal void HideFromJava(PropertyBuilder pb)
546 pb.SetCustomAttribute(HideFromJavaAttributeBuilder);
551 internal bool IsHideFromJava(
Type type)
553 return type.IsDefined(TypeOfHideFromJavaAttribute,
false) || (type.IsNested && (type.DeclaringType.IsDefined(TypeOfHideFromJavaAttribute,
false) || type.Name.StartsWith(
"__<", StringComparison.Ordinal)));
566 if (fi !=
null && (fi.Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.PrivateScope)
570 if (mb !=
null && (mb.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.PrivateScope)
572 if (mi.Name.StartsWith(
"__<", StringComparison.Ordinal))
575#if !IMPORTER && !EXPORTER
577 var attr = mi.GetCustomAttributes(typeofHideFromJavaAttribute,
false);
578 if (attr.Length == 1)
582 var attr = CustomAttributeData.__GetCustomAttributes(mi, TypeOfHideFromJavaAttribute,
false);
585 var args = attr[0].ConstructorArguments;
598 internal void SetImplementsAttribute(TypeBuilder typeBuilder, RuntimeJavaType[] ifaceWrappers)
600 var interfaces =
new string[ifaceWrappers.Length];
601 for (
int i = 0; i < interfaces.Length; i++)
602 interfaces[i] = UnicodeUtil.EscapeInvalidSurrogates(ifaceWrappers[i].Name);
604 if (implementsAttribute ==
null)
605 implementsAttribute = TypeOfImplementsAttribute.GetConstructor(
new Type[] { context.Resolver.ResolveCoreType(typeof(
string).FullName).MakeArrayType().AsReflection() });
607 typeBuilder.SetCustomAttribute(
new CustomAttributeBuilder(implementsAttribute,
new object[] { interfaces }));
612 internal bool IsGhostInterface(
Type type)
614 return type.IsDefined(TypeOfGhostInterfaceAttribute,
false);
617 internal bool IsRemappedType(
Type type)
619 return type.IsDefined(TypeOfRemappedTypeAttribute,
false);
622 internal bool IsExceptionIsUnsafeForMapping(
Type type)
624 return type.IsDefined(TypeOfExceptionIsUnsafeForMappingAttribute,
false);
629#if !IMPORTER && !EXPORTER
633 var attr = CustomAttributeData.__GetCustomAttributes(member, TypeOfModifiersAttribute,
false);
636 var args = attr[0].ConstructorArguments;
647 internal ExModifiers GetModifiers(
MethodBase mb,
bool assemblyIsPrivate)
649 var attr = GetModifiersAttribute(mb);
651 return new ExModifiers(attr.Modifiers, attr.IsInternal);
659 else if (mb.IsPrivate)
663 else if (mb.IsFamily || mb.IsFamilyOrAssembly)
667 else if (assemblyIsPrivate)
674 if ((mb.IsFinal || (!mb.IsVirtual && ((modifiers &
Modifiers.Private) == 0))) && !mb.IsStatic && !mb.IsConstructor)
688 if ((mb.GetMethodImplementationFlags() & MethodImplAttributes.Synchronized) != 0)
699 if ((mb.Attributes & MethodAttributes.PinvokeImpl) != 0)
704 var parameters = mb.GetParameters();
705 if (parameters.Length > 0 && parameters[parameters.Length - 1].IsDefined(context.Resolver.ResolveCoreType(typeof(ParamArrayAttribute).FullName).AsReflection(),
false))
708 return new ExModifiers(modifiers,
false);
711 internal ExModifiers GetModifiers(
FieldInfo fi,
bool assemblyIsPrivate)
713 var attr = GetModifiersAttribute(fi);
715 return new ExModifiers(attr.Modifiers, attr.IsInternal);
722 else if (fi.IsPrivate)
726 else if (fi.IsFamily || fi.IsFamilyOrAssembly)
730 else if (assemblyIsPrivate)
735 if (fi.IsInitOnly || fi.IsLiteral)
740 if (fi.IsNotSerialized)
750 if (Array.IndexOf(fi.GetRequiredCustomModifiers(), context.Types.IsVolatile) != -1)
755 return new ExModifiers(modifiers,
false);
758 internal void SetDebuggingModes(AssemblyBuilder assemblyBuilder, DebuggableAttribute.DebuggingModes modes)
760 debuggableAttribute ??= TypeOfDebuggableAttribute.GetConstructor(
new[] { TypeOfDebuggableAttribute.GetNestedType(nameof(DebuggableAttribute.DebuggingModes)) });
761 assemblyBuilder.SetCustomAttribute(
new CustomAttributeBuilder(debuggableAttribute,
new object[] { modes }));
766 internal void SetModifiers(MethodBuilder mb,
Modifiers modifiers,
bool isInternal)
768 CustomAttributeBuilder customAttributeBuilder;
770 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers, context.Types.Boolean }),
new object[] { modifiers, isInternal });
772 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers }),
new object[] { modifiers });
774 mb.SetCustomAttribute(customAttributeBuilder);
777 internal void SetModifiers(FieldBuilder fb,
Modifiers modifiers,
bool isInternal)
779 CustomAttributeBuilder customAttributeBuilder;
781 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers, context.Types.Boolean }),
new object[] { modifiers, isInternal });
783 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers }),
new object[] { modifiers });
785 fb.SetCustomAttribute(customAttributeBuilder);
788 internal void SetModifiers(PropertyBuilder pb,
Modifiers modifiers,
bool isInternal)
790 CustomAttributeBuilder customAttributeBuilder;
792 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers, context.Types.Boolean }),
new object[] { modifiers, isInternal });
794 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers }),
new object[] { modifiers });
796 pb.SetCustomAttribute(customAttributeBuilder);
799 internal void SetModifiers(TypeBuilder tb,
Modifiers modifiers,
bool isInternal)
801 CustomAttributeBuilder customAttributeBuilder;
803 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers, context.Types.Boolean }),
new object[] { modifiers, isInternal });
805 customAttributeBuilder =
new CustomAttributeBuilder(TypeOfModifiersAttribute.GetConstructor(
new Type[] { TypeOfModifiers }),
new object[] { modifiers });
807 tb.SetCustomAttribute(customAttributeBuilder);
810 internal void SetNameSig(MethodBuilder mb,
string name,
string sig)
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);
816 internal void SetInnerClass(TypeBuilder typeBuilder,
string innerClass,
Modifiers modifiers)
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);
825 internal void SetSourceFile(TypeBuilder typeBuilder,
string filename)
827 sourceFileAttribute ??= TypeOfSourceFileAttribute.GetConstructor(
new Type[] { context.Types.String });
828 typeBuilder.SetCustomAttribute(
new CustomAttributeBuilder(sourceFileAttribute,
new object[] { filename }));
831 internal void SetSourceFile(ModuleBuilder moduleBuilder,
string filename)
833 sourceFileAttribute ??= TypeOfSourceFileAttribute.GetConstructor(
new Type[] { context.Types.String });
834 moduleBuilder.SetCustomAttribute(
new CustomAttributeBuilder(sourceFileAttribute,
new object[] { filename }));
841 if (writer.Count == 1)
843 lineNumberTableAttribute2 ??= TypeOfLineNumberTableAttribute.GetConstructor(
new Type[] { context.Types.UInt16 });
844 con = lineNumberTableAttribute2;
845 arg = (ushort)writer.LineNo;
849 lineNumberTableAttribute1 ??= TypeOfLineNumberTableAttribute.GetConstructor(
new Type[] { context.Resolver.ResolveCoreType(typeof(
byte).FullName).MakeArrayType().AsReflection() });
850 con = lineNumberTableAttribute1;
851 arg = writer.ToArray();
853 mb.SetCustomAttribute(
new CustomAttributeBuilder(con,
new object[] { arg }));
856 internal void SetEnclosingMethodAttribute(TypeBuilder tb,
string className,
string methodName,
string methodSig)
858 if (enclosingMethodAttribute ==
null)
860 enclosingMethodAttribute = TypeOfEnclosingMethodAttribute.GetConstructor(
new Type[] { context.Types.String, context.Types.String, context.Types.String });
862 tb.SetCustomAttribute(
new CustomAttributeBuilder(enclosingMethodAttribute,
863 new object[] { UnicodeUtil.EscapeInvalidSurrogates(className), UnicodeUtil.EscapeInvalidSurrogates(methodName), UnicodeUtil.EscapeInvalidSurrogates(methodSig) }));
866 internal void SetSignatureAttribute(TypeBuilder tb,
string signature)
868 signatureAttribute ??= TypeOfSignatureAttribute.GetConstructor(
new Type[] { context.Types.String });
869 tb.SetCustomAttribute(
new CustomAttributeBuilder(signatureAttribute,
new object[] { UnicodeUtil.EscapeInvalidSurrogates(signature) }));
872 internal void SetSignatureAttribute(FieldBuilder fb,
string signature)
874 signatureAttribute ??= TypeOfSignatureAttribute.GetConstructor(
new Type[] { context.Types.String });
875 fb.SetCustomAttribute(
new CustomAttributeBuilder(signatureAttribute,
new object[] { UnicodeUtil.EscapeInvalidSurrogates(signature) }));
878 internal void SetSignatureAttribute(MethodBuilder mb,
string signature)
880 signatureAttribute ??= TypeOfSignatureAttribute.GetConstructor(
new Type[] { context.Types.String });
881 mb.SetCustomAttribute(
new CustomAttributeBuilder(signatureAttribute,
new object[] { UnicodeUtil.EscapeInvalidSurrogates(signature) }));
884 internal void SetMethodParametersAttribute(MethodBuilder mb,
Modifiers[] modifiers)
886 methodParametersAttribute ??= TypeOfMethodParametersAttribute.GetConstructor(
new[] { TypeOfModifiers.MakeArrayType() });
887 mb.SetCustomAttribute(
new CustomAttributeBuilder(methodParametersAttribute,
new[] { modifiers }));
890 internal void SetRuntimeVisibleTypeAnnotationsAttribute(TypeBuilder tb,
ref readonly TypeAnnotationTable table)
892 var builder =
new BlobBuilder();
893 var encoder =
new TypeAnnotationTableEncoder(builder);
894 table.WriteTo(
ref encoder);
896 runtimeVisibleTypeAnnotationsAttribute ??= TypeOfRuntimeVisibleTypeAnnotationsAttribute.GetConstructor([context.Types.Byte.MakeArrayType()]);
897 tb.SetCustomAttribute(
new CustomAttributeBuilder(runtimeVisibleTypeAnnotationsAttribute, [builder.ToArray()]));
900 internal void SetRuntimeVisibleTypeAnnotationsAttribute(FieldBuilder fb,
ref readonly TypeAnnotationTable table)
902 var builder =
new BlobBuilder();
903 var encoder =
new TypeAnnotationTableEncoder(builder);
904 table.WriteTo(
ref encoder);
906 runtimeVisibleTypeAnnotationsAttribute ??= TypeOfRuntimeVisibleTypeAnnotationsAttribute.GetConstructor([context.Types.Byte.MakeArrayType()]);
907 fb.SetCustomAttribute(
new CustomAttributeBuilder(runtimeVisibleTypeAnnotationsAttribute, [builder.ToArray()]));
910 internal void SetRuntimeVisibleTypeAnnotationsAttribute(MethodBuilder mb,
ref readonly TypeAnnotationTable table)
912 var builder =
new BlobBuilder();
913 var encoder =
new TypeAnnotationTableEncoder(builder);
914 table.WriteTo(
ref encoder);
916 runtimeVisibleTypeAnnotationsAttribute ??= TypeOfRuntimeVisibleTypeAnnotationsAttribute.GetConstructor([context.Types.Byte.MakeArrayType()]);
917 mb.SetCustomAttribute(
new CustomAttributeBuilder(runtimeVisibleTypeAnnotationsAttribute, [builder.ToArray()]));
920 internal void SetConstantPoolAttribute(TypeBuilder tb,
object[] constantPool)
922 constantPoolAttribute ??= TypeOfConstantPoolAttribute.GetConstructor([context.Types.Object.MakeArrayType()]);
923 tb.SetCustomAttribute(
new CustomAttributeBuilder(constantPoolAttribute, [constantPool]));
926 internal void SetParamArrayAttribute(ParameterBuilder pb)
928 paramArrayAttribute ??=
new CustomAttributeBuilder(context.Resolver.ResolveCoreType(typeof(ParamArrayAttribute).FullName).GetConstructor([]).AsReflection(), Array.Empty<
object>());
929 pb.SetCustomAttribute(paramArrayAttribute);
936#if !IMPORTER && !EXPORTER
940 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(member, TypeOfNameSigAttribute,
false))
942 var args = cad.ConstructorArguments;
950 internal T[] DecodeArray<T>(CustomAttributeTypedArgument arg)
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;
962#if !IMPORTER && !EXPORTER
966 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(type, TypeOfImplementsAttribute,
false))
968 var args = cad.ConstructorArguments;
976 internal ThrowsAttribute GetThrows(
MethodBase mb)
978#if !IMPORTER && !EXPORTER
979 var attribs = mb.GetCustomAttributes(typeof(ThrowsAttribute),
false);
980 return attribs.Length == 1 ? (ThrowsAttribute)attribs[0] : null;
982 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(mb, TypeOfThrowsAttribute,
false))
984 var args = cad.ConstructorArguments;
985 if (args[0].ArgumentType == context.Types.String.MakeArrayType())
987 return new ThrowsAttribute(DecodeArray<string>(args[0]));
989 else if (args[0].ArgumentType == context.Types.Type.MakeArrayType())
991 return new ThrowsAttribute(DecodeArray<Type>(args[0]));
995 return new ThrowsAttribute((
Type)args[0].Value);
1003 internal string[] GetNonNestedInnerClasses(
Type t)
1005#if !IMPORTER && !EXPORTER
1007 var classes =
new string[attribs.Length];
1008 for (
int i = 0; i < attribs.Length; i++)
1013 var list =
new List<string>();
1014 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(t, TypeOfNonNestedInnerClassAttribute,
false))
1016 var args = cad.ConstructorArguments;
1017 list.Add(UnicodeUtil.UnescapeInvalidSurrogates((
string)args[0].Value));
1020 return list.ToArray();
1024 internal string GetNonNestedOuterClasses(
Type t)
1026#if !IMPORTER && !EXPORTER
1030 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(t, TypeOfNonNestedOuterClassAttribute,
false))
1032 var args = cad.ConstructorArguments;
1033 return UnicodeUtil.UnescapeInvalidSurrogates((
string)args[0].Value);
1041#if !IMPORTER && !EXPORTER
1045 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(member, TypeOfSignatureAttribute,
false))
1047 var args = cad.ConstructorArguments;
1057#if !IMPORTER && !EXPORTER
1061 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(method, TypeOfMethodParametersAttribute,
false))
1063 var args = cad.ConstructorArguments;
1070 internal object[] GetConstantPool(
Type type)
1072#if !IMPORTER && !EXPORTER
1076 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(type, TypeOfConstantPoolAttribute,
false))
1083 internal byte[] GetRuntimeVisibleTypeAnnotations(
MemberInfo member)
1085#if !IMPORTER && !EXPORTER
1089 foreach (CustomAttributeData cad
in CustomAttributeData.__GetCustomAttributes(member, TypeOfRuntimeVisibleTypeAnnotationsAttribute,
false))
1091 return DecodeArray<byte>(cad.ConstructorArguments[0]);
1100#if !IMPORTER && !EXPORTER
1104 foreach (CustomAttributeData cad
in CustomAttributeData.__GetCustomAttributes(type, TypeOfInnerClassAttribute,
false))
1106 IList<CustomAttributeTypedArgument> args = cad.ConstructorArguments;
1115#if !IMPORTER && !EXPORTER
1118 Array.Copy(attr, attr1, attr.Length);
1121 var attrs =
new List<RemappedInterfaceMethodAttribute>();
1122 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(type, TypeOfRemappedInterfaceMethodAttribute,
false))
1124 var args = cad.ConstructorArguments;
1128 return attrs.ToArray();
1132 internal RemappedTypeAttribute GetRemappedType(
Type type)
1134#if !IMPORTER && !EXPORTER
1135 var attribs = type.GetCustomAttributes(typeof(RemappedTypeAttribute),
false);
1136 return attribs.Length == 1 ? (RemappedTypeAttribute)attribs[0] : null;
1138 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(type, TypeOfRemappedTypeAttribute,
false))
1140 var args = cad.ConstructorArguments;
1141 return new RemappedTypeAttribute((
Type)args[0].Value);
1148 internal RemappedClassAttribute[] GetRemappedClasses(
Assembly coreAssembly)
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);
1156 var attrs =
new List<RemappedClassAttribute>();
1157 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(coreAssembly, TypeOfRemappedClassAttribute,
false))
1159 var args = cad.ConstructorArguments;
1160 attrs.Add(
new RemappedClassAttribute((
string)args[0].Value, (
Type)args[1].Value));
1163 return attrs.ToArray();
1167 internal string GetAnnotationAttributeType(
Type type)
1169#if !IMPORTER && !EXPORTER
1171 if (attr.Length == 1)
1176 foreach (var cad
in CustomAttributeData.__GetCustomAttributes(type, TypeOfAnnotationAttributeAttribute,
false))
1177 return UnicodeUtil.UnescapeInvalidSurrogates((
string)cad.ConstructorArguments[0].Value);
1185 var list =
new List<AssemblyName>();
1186 foreach (var cad
in CustomAttributeData.GetCustomAttributes(assembly))
1188 if (cad.Constructor.DeclaringType == context.Resolver.ResolveCoreType(typeof(InternalsVisibleToAttribute).FullName).AsReflection())
1192 list.Add(
new AssemblyName((
string)cad.ConstructorArguments[0].Value));
1201 return list.ToArray();
1204 internal bool IsJavaModule(
Module mod)
1206 return mod.IsDefined(TypeOfJavaModuleAttribute,
false);
1209 internal object[] GetJavaModuleAttributes(
Module mod)
1211#if !IMPORTER && !EXPORTER
1212 return mod.GetCustomAttributes(TypeOfJavaModuleAttribute,
false);
1214 List<JavaModuleAttribute> attrs =
new List<JavaModuleAttribute>();
1215 foreach (CustomAttributeData cad
in CustomAttributeData.__GetCustomAttributes(mod, TypeOfJavaModuleAttribute,
false))
1217 IList<CustomAttributeTypedArgument> args = cad.ConstructorArguments;
1218 if (args.Count == 0)
1227 return attrs.ToArray();
1231 internal bool IsNoPackagePrefix(
Type type)
1233 return type.IsDefined(TypeOfNoPackagePrefixAttribute,
false) || type.Assembly.IsDefined(TypeOfNoPackagePrefixAttribute,
false);
1236 internal bool HasEnclosingMethodAttribute(
Type type)
1238 return type.IsDefined(TypeOfEnclosingMethodAttribute,
false);
1243#if !IMPORTER && !EXPORTER
1245 if (attr.Length == 1)
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);
1259 internal void SetRemappedClass(AssemblyBuilder assemblyBuilder,
string name,
Type shadowType)
1261 var remappedClassAttribute = TypeOfRemappedClassAttribute.GetConstructor(
new Type[] { context.Types.String, context.Types.Type });
1262 assemblyBuilder.SetCustomAttribute(
new CustomAttributeBuilder(remappedClassAttribute,
new object[] { name, shadowType }));
1265 internal void SetRemappedType(TypeBuilder typeBuilder,
Type shadowType)
1267 var remappedTypeAttribute = TypeOfRemappedTypeAttribute.GetConstructor(
new Type[] { context.Types.Type });
1268 typeBuilder.SetCustomAttribute(
new CustomAttributeBuilder(remappedTypeAttribute,
new object[] { shadowType }));
1271 internal void SetRemappedInterfaceMethod(TypeBuilder typeBuilder,
string name,
string mappedTo,
string[]
throws)
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);
1277 internal void SetExceptionIsUnsafeForMapping(TypeBuilder typeBuilder)
1279 var cab =
new CustomAttributeBuilder(TypeOfExceptionIsUnsafeForMappingAttribute.GetConstructor(
Type.EmptyTypes), Array.Empty<
object>());
1280 typeBuilder.SetCustomAttribute(cab);
1285 internal void SetRuntimeCompatibilityAttribute(AssemblyBuilder assemblyBuilder)
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>()));
1291 internal void SetInternalsVisibleToAttribute(AssemblyBuilder assemblyBuilder,
string assemblyName)
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);