IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
DefineMethodHelper.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;
25
26#if IMPORTER
27using IKVM.Reflection;
30
31using Type = IKVM.Reflection.Type;
33using ProtectionDomain = System.Object;
34#else
35using System.Reflection;
36using System.Reflection.Emit;
37#endif
38
39namespace IKVM.Runtime
40{
41
42 sealed class DefineMethodHelper
43 {
44
45 private readonly RuntimeJavaMethod mw;
46
48 {
49 this.mw = mw;
50 }
51
52 internal int ParameterCount
53 {
54 get { return mw.GetParameters().Length + (mw.HasCallerID ? 1 : 0); }
55 }
56
57 internal MethodBuilder DefineMethod(RuntimeByteCodeJavaType context, TypeBuilder tb, string name, MethodAttributes attribs)
58 {
59 return DefineMethod(context.ClassLoader.GetTypeWrapperFactory(), tb, name, attribs, null, false);
60 }
61
62 internal MethodBuilder DefineMethod(RuntimeJavaTypeFactory context, TypeBuilder tb, string name, MethodAttributes attribs)
63 {
64 return DefineMethod(context, tb, name, attribs, null, false);
65 }
66
67 internal MethodBuilder DefineMethod(RuntimeJavaTypeFactory context, TypeBuilder tb, string name, MethodAttributes attribs, Type firstParameter, bool mustBePublic)
68 {
69 // we add optional modifiers to make the signature unique
70 int firstParam = firstParameter == null ? 0 : 1;
71 RuntimeJavaType[] parameters = mw.GetParameters();
72 Type[] parameterTypes = new Type[parameters.Length + (mw.HasCallerID ? 1 : 0) + firstParam];
73 Type[][] modopt = new Type[parameterTypes.Length][];
74 if (firstParameter != null)
75 {
76 parameterTypes[0] = firstParameter;
77 modopt[0] = Type.EmptyTypes;
78 }
79 for (int i = 0; i < parameters.Length; i++)
80 {
81 parameterTypes[i + firstParam] = mustBePublic
82 ? parameters[i].TypeAsPublicSignatureType
83 : parameters[i].TypeAsSignatureType;
84 modopt[i + firstParam] = RuntimeByteCodeJavaType.GetModOpt(context, parameters[i], mustBePublic);
85 }
86 if (mw.HasCallerID)
87 {
88 parameterTypes[parameterTypes.Length - 1] = mw.DeclaringType.Context.JavaBase.TypeOfIkvmInternalCallerID.TypeAsSignatureType;
89 }
90 Type returnType = mustBePublic
91 ? mw.ReturnType.TypeAsPublicSignatureType
92 : mw.ReturnType.TypeAsSignatureType;
93 Type[] modoptReturnType = RuntimeByteCodeJavaType.GetModOpt(context, mw.ReturnType, mustBePublic);
94 return tb.DefineMethod(name, attribs, CallingConventions.Standard, returnType, null, modoptReturnType, parameterTypes, null, modopt);
95 }
96
97 internal MethodBuilder DefineConstructor(RuntimeByteCodeJavaType context, TypeBuilder tb, MethodAttributes attribs)
98 {
99 return DefineConstructor(context.ClassLoader.GetTypeWrapperFactory(), tb, attribs);
100 }
101
102 internal MethodBuilder DefineConstructor(RuntimeJavaTypeFactory context, TypeBuilder tb, MethodAttributes attribs)
103 {
104 return DefineMethod(context, tb, ConstructorInfo.ConstructorName, attribs | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
105 }
106
107 }
108
109}
java.security.ProtectionDomain ProtectionDomain
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Runtime.RuntimeByteCodeJavaType DynamicOrAotTypeWrapper
Implementation of RuntimeByteCodeJavaType that customizes output for the importer.