IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeManagedByteCodeJavaType.RemappedJavaType.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;
26
27using IKVM.Attributes;
28
29#if IMPORTER || EXPORTER
30using IKVM.Reflection;
32
33using Type = IKVM.Reflection.Type;
34#else
35using System.Reflection;
36#endif
37
38namespace IKVM.Runtime
39{
40
42 {
43
44 internal sealed class RemappedJavaType : RuntimeManagedByteCodeJavaType
45 {
46
47 readonly Type remappedType;
48
56 internal RemappedJavaType(RuntimeContext context, string name, Type type) :
57 base(context, name, type)
58 {
59 var attr = Context.AttributeHelper.GetRemappedType(type) ?? throw new InvalidOperationException();
60 remappedType = attr.Type;
61 }
62
63 internal override Type TypeAsTBD => remappedType;
64
65 internal override bool IsRemapped => true;
66
67 protected override void LazyPublishMethods()
68 {
69 const BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
70
71 var list = new List<RuntimeJavaMethod>();
72
73 foreach (var ctor in type.GetConstructors(bindingFlags))
74 AddMethod(list, ctor);
75
76 foreach (var method in type.GetMethods(bindingFlags))
77 AddMethod(list, method);
78
79 // if we're a remapped interface, we need to get the methods from the real interface
80 if (remappedType.IsInterface)
81 {
82 var nestedHelper = type.GetNestedType("__Helper", BindingFlags.Public | BindingFlags.Static);
83 foreach (var m in Context.AttributeHelper.GetRemappedInterfaceMethods(type))
84 {
85 var method = remappedType.GetMethod(m.MappedTo);
86 var mbHelper = method;
87 var modifiers = Context.AttributeHelper.GetModifiers(method, false);
88 var flags = MemberFlags.None;
89
90 GetNameSigFromMethodBase(method, out var name, out var sig, out var retType, out var paramTypes, ref flags);
91 if (nestedHelper != null)
92 {
93 mbHelper = nestedHelper.GetMethod(m.Name);
94 if (mbHelper == null)
95 mbHelper = method;
96 }
97
98 var mw = new RemappedJavaMethod(this, m.Name, sig, method, retType, paramTypes, modifiers, false, mbHelper, null);
99 mw.SetDeclaredExceptions(m.Throws);
100 list.Add(mw);
101 }
102 }
103
104 SetMethods(list.ToArray());
105 }
106
107 private void AddMethod(List<RuntimeJavaMethod> list, MethodBase method)
108 {
109 var flags = Context.AttributeHelper.GetHideFromJavaFlags(method);
110 if ((flags & HideFromJavaFlags.Code) == 0 && (remappedType.IsSealed || !method.Name.StartsWith("instancehelper_")) && (!remappedType.IsSealed || method.IsStatic))
111 list.Add(CreateRemappedMethodWrapper(method, flags));
112 }
113
114 protected override void LazyPublishFields()
115 {
116 var list = new List<RuntimeJavaField>();
117 var fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
118 foreach (var field in fields)
119 {
120 HideFromJavaFlags hideFromJavaFlags = Context.AttributeHelper.GetHideFromJavaFlags(field);
121 if ((hideFromJavaFlags & HideFromJavaFlags.Code) == 0)
122 list.Add(CreateFieldWrapper(field, hideFromJavaFlags));
123 }
124 SetFields(list.ToArray());
125 }
126
127 RuntimeJavaMethod CreateRemappedMethodWrapper(MethodBase mb, HideFromJavaFlags hideFromJavaflags)
128 {
129 var modifiers = Context.AttributeHelper.GetModifiers(mb, false);
130 var flags = MemberFlags.None;
131 GetNameSigFromMethodBase(mb, out var name, out var sig, out var retType, out var paramTypes, ref flags);
132
133 var mbHelper = mb as MethodInfo;
134 var hideFromReflection = mbHelper != null && (hideFromJavaflags & HideFromJavaFlags.Reflection) != 0;
135 MethodInfo mbNonvirtualHelper = null;
136 if (!mb.IsStatic && !mb.IsConstructor)
137 {
138 ParameterInfo[] parameters = mb.GetParameters();
139 Type[] argTypes = new Type[parameters.Length + 1];
140 argTypes[0] = remappedType;
141 for (int i = 0; i < parameters.Length; i++)
142 {
143 argTypes[i + 1] = parameters[i].ParameterType;
144 }
145 MethodInfo helper = type.GetMethod("instancehelper_" + mb.Name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, argTypes, null);
146 if (helper != null)
147 {
148 mbHelper = helper;
149 }
150 mbNonvirtualHelper = type.GetMethod("nonvirtualhelper/" + mb.Name, BindingFlags.NonPublic | BindingFlags.Static, null, argTypes, null);
151 }
152
153 return new RemappedJavaMethod(this, name, sig, mb, retType, paramTypes, modifiers, hideFromReflection, mbHelper, mbNonvirtualHelper);
154 }
155 }
156
157 }
158
159}
IKVM.Reflection.Type Type
IKVM.Reflection.MethodInfo MethodInfo
IKVM.Reflection.ParameterInfo ParameterInfo
IKVM.Reflection.MethodBase MethodBase
Maintains services relevant to an instane of the IKVM runtime.
AttributeHelper AttributeHelper
Gets the AttributeHelper associated with this instance of the runtime.
Represents a runtime Java type derived from a .NET assembly which was the result of the IKVM compiler...
MemberFlags
Describes various options applied to a member.