IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeManagedJavaType.DelegateJavaMethod.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.Diagnostics;
26
27using IKVM.Attributes;
28
29#if IMPORTER || EXPORTER
30using IKVM.Reflection;
32
33using Type = IKVM.Reflection.Type;
34#else
35using System.Reflection;
36using System.Reflection.Emit;
37#endif
38
39namespace IKVM.Runtime
40{
41
42 sealed partial class RuntimeManagedJavaType
43 {
44
45 sealed class DelegateJavaMethod : RuntimeJavaMethod
46 {
47
48 readonly ConstructorInfo delegateConstructor;
49 readonly DelegateInnerClassJavaType iface;
50
56 internal DelegateJavaMethod(RuntimeJavaType declaringType, DelegateInnerClassJavaType iface) :
57 base(declaringType, "<init>", "(" + iface.SigName + ")V", null, declaringType.Context.PrimitiveJavaTypeFactory.VOID, new RuntimeJavaType[] { iface }, Modifiers.Public, MemberFlags.Intrinsic)
58 {
59 this.delegateConstructor = declaringType.TypeAsTBD.GetConstructor(new Type[] { DeclaringType.Context.Types.Object, DeclaringType.Context.Types.IntPtr });
60 this.iface = iface;
61 }
62
63#if EMITTERS
64
65 internal override bool EmitIntrinsic(EmitIntrinsicContext context)
66 {
67 var targetType = context.GetStackTypeWrapper(0, 0);
68 if (targetType.IsUnloadable || targetType.IsInterface)
69 return false;
70
71 // we know that a DelegateInnerClassTypeWrapper has only one method
72 Debug.Assert(iface.GetMethods().Length == 1);
73
74 var mw = targetType.GetMethod(GetDelegateInvokeStubName(DeclaringType.TypeAsTBD), iface.GetMethods()[0].Signature, true);
75 if (mw == null || mw.IsStatic || !mw.IsPublic)
76 {
77 context.Emitter.Emit(OpCodes.Ldftn, CreateErrorStub(context, targetType, mw == null || mw.IsStatic));
78 context.Emitter.Emit(OpCodes.Newobj, delegateConstructor);
79 return true;
80 }
81
82 // TODO linking here is not safe
83 mw.Link();
84 context.Emitter.Emit(OpCodes.Dup);
85 context.Emitter.Emit(OpCodes.Ldvirtftn, mw.GetMethod());
86 context.Emitter.Emit(OpCodes.Newobj, delegateConstructor);
87 return true;
88 }
89
90 MethodInfo CreateErrorStub(EmitIntrinsicContext context, RuntimeJavaType targetType, bool isAbstract)
91 {
92 var invoke = delegateConstructor.DeclaringType.GetMethod("Invoke");
93
94 var parameters = invoke.GetParameters();
95 var parameterTypes = new Type[parameters.Length + 1];
96 parameterTypes[0] = DeclaringType.Context.Types.Object;
97 for (int i = 0; i < parameters.Length; i++)
98 parameterTypes[i + 1] = parameters[i].ParameterType;
99
100 var mb = context.Context.DefineDelegateInvokeErrorStub(invoke.ReturnType, parameterTypes);
101 var ilgen = DeclaringType.Context.CodeEmitterFactory.Create(mb);
102 ilgen.EmitThrow(isAbstract ? "java.lang.AbstractMethodError" : "java.lang.IllegalAccessError", targetType.Name + ".Invoke" + iface.GetMethods()[0].Signature);
103 ilgen.DoEmit();
104 return mb;
105 }
106
107 internal override void EmitNewobj(CodeEmitter ilgen)
108 {
109 ilgen.Emit(OpCodes.Ldtoken, delegateConstructor.DeclaringType);
110 ilgen.Emit(OpCodes.Call, DeclaringType.Context.CompilerFactory.GetTypeFromHandleMethod);
111 ilgen.Emit(OpCodes.Ldstr, GetDelegateInvokeStubName(DeclaringType.TypeAsTBD));
112 ilgen.Emit(OpCodes.Ldstr, iface.GetMethods()[0].Signature);
113 ilgen.Emit(OpCodes.Call, DeclaringType.Context.ByteCodeHelperMethods.DynamicCreateDelegate);
114 ilgen.Emit(OpCodes.Castclass, delegateConstructor.DeclaringType);
115 }
116
117 internal override void EmitCall(CodeEmitter ilgen)
118 {
119 // This is a bit of a hack. We bind the existing delegate to a new delegate to be able to reuse the DynamicCreateDelegate error
120 // handling. This leaks out to the user because Delegate.Target will return the target delegate instead of the bound object.
121
122 // create the target delegate
123 EmitNewobj(ilgen);
124
125 // invoke the constructor, binding the delegate to the target delegate
126 ilgen.Emit(OpCodes.Dup);
127 ilgen.Emit(OpCodes.Ldvirtftn, DeclaringType.Context.MethodHandleUtil.GetDelegateInvokeMethod(delegateConstructor.DeclaringType));
128 ilgen.Emit(OpCodes.Call, delegateConstructor);
129 }
130
131#endif
132
133 }
134
135 }
136
137}
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.MethodInfo MethodInfo
CodeEmitter Create(MethodBuilder mb)
Creates a new instance.
CodeEmitterFactory CodeEmitterFactory
Gets the CodeEmitterFactory associated with this instance of the runtime.
Types Types
Gets the Types associated with this instance of the runtime.
MethodHandleUtil MethodHandleUtil
Gets the MethodHandleUtil associated with this instance of the runtime.
ByteCodeHelperMethods ByteCodeHelperMethods
Gets the ByteCodeHelperMethods associated with this instance of the runtime.
CompilerFactory CompilerFactory
Gets the CompilerFactory associated with this instance of the runtime.
MemberFlags
Describes various options applied to a member.