IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Boxer.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2011-2014 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
26namespace IKVM.Runtime
27{
28
29 class Boxer
30 {
31
32 readonly RuntimeJavaType javaLangByte;
33 readonly RuntimeJavaMethod byteValue;
34 readonly RuntimeJavaMethod valueOfByte;
35 readonly RuntimeJavaType javaLangBoolean;
36 readonly RuntimeJavaMethod booleanValue;
37 readonly RuntimeJavaMethod valueOfBoolean;
38 readonly RuntimeJavaType javaLangShort;
39 readonly RuntimeJavaMethod shortValue;
40 readonly RuntimeJavaMethod valueOfShort;
41 readonly RuntimeJavaType javaLangCharacter;
42 readonly RuntimeJavaMethod charValue;
43 readonly RuntimeJavaMethod valueOfCharacter;
44 readonly RuntimeJavaType javaLangInteger;
45 readonly RuntimeJavaMethod intValue;
46 readonly RuntimeJavaMethod valueOfInteger;
47 readonly RuntimeJavaType javaLangFloat;
48 readonly RuntimeJavaMethod floatValue;
49 readonly RuntimeJavaMethod valueOfFloat;
50 readonly RuntimeJavaType javaLangLong;
51 readonly RuntimeJavaMethod longValue;
52 readonly RuntimeJavaMethod valueOfLong;
53 readonly RuntimeJavaType javaLangDouble;
54 readonly RuntimeJavaMethod doubleValue;
55 readonly RuntimeJavaMethod valueOfDouble;
56
61 public Boxer(RuntimeContext context)
62 {
63 RuntimeClassLoader bootClassLoader = context.ClassLoaderFactory.GetBootstrapClassLoader();
64 javaLangByte = bootClassLoader.TryLoadClassByName("java.lang.Byte");
65 byteValue = javaLangByte.GetMethod("byteValue", "()B", false);
66 byteValue.Link();
67 valueOfByte = javaLangByte.GetMethod("valueOf", "(B)Ljava.lang.Byte;", false);
68 valueOfByte.Link();
69 javaLangBoolean = bootClassLoader.TryLoadClassByName("java.lang.Boolean");
70 booleanValue = javaLangBoolean.GetMethod("booleanValue", "()Z", false);
71 booleanValue.Link();
72 valueOfBoolean = javaLangBoolean.GetMethod("valueOf", "(Z)Ljava.lang.Boolean;", false);
73 valueOfBoolean.Link();
74 javaLangShort = bootClassLoader.TryLoadClassByName("java.lang.Short");
75 shortValue = javaLangShort.GetMethod("shortValue", "()S", false);
76 shortValue.Link();
77 valueOfShort = javaLangShort.GetMethod("valueOf", "(S)Ljava.lang.Short;", false);
78 valueOfShort.Link();
79 javaLangCharacter = bootClassLoader.TryLoadClassByName("java.lang.Character");
80 charValue = javaLangCharacter.GetMethod("charValue", "()C", false);
81 charValue.Link();
82 valueOfCharacter = javaLangCharacter.GetMethod("valueOf", "(C)Ljava.lang.Character;", false);
83 valueOfCharacter.Link();
84 javaLangInteger = bootClassLoader.TryLoadClassByName("java.lang.Integer");
85 intValue = javaLangInteger.GetMethod("intValue", "()I", false);
86 intValue.Link();
87 valueOfInteger = javaLangInteger.GetMethod("valueOf", "(I)Ljava.lang.Integer;", false);
88 valueOfInteger.Link();
89 javaLangFloat = bootClassLoader.TryLoadClassByName("java.lang.Float");
90 floatValue = javaLangFloat.GetMethod("floatValue", "()F", false);
91 floatValue.Link();
92 valueOfFloat = javaLangFloat.GetMethod("valueOf", "(F)Ljava.lang.Float;", false);
93 valueOfFloat.Link();
94 javaLangLong = bootClassLoader.TryLoadClassByName("java.lang.Long");
95 longValue = javaLangLong.GetMethod("longValue", "()J", false);
96 longValue.Link();
97 valueOfLong = javaLangLong.GetMethod("valueOf", "(J)Ljava.lang.Long;", false);
98 valueOfLong.Link();
99 javaLangDouble = bootClassLoader.TryLoadClassByName("java.lang.Double");
100 doubleValue = javaLangDouble.GetMethod("doubleValue", "()D", false);
101 doubleValue.Link();
102 valueOfDouble = javaLangDouble.GetMethod("valueOf", "(D)Ljava.lang.Double;", false);
103 valueOfDouble.Link();
104 }
105
106 internal void EmitUnbox(CodeEmitter ilgen, RuntimeJavaType tw, bool cast)
107 {
108 if (tw == ilgen.Context.PrimitiveJavaTypeFactory.BYTE)
109 {
110 if (cast)
111 {
112 javaLangByte.EmitCheckcast(ilgen);
113 }
114 byteValue.EmitCall(ilgen);
115 }
116 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.BOOLEAN)
117 {
118 if (cast)
119 {
120 javaLangBoolean.EmitCheckcast(ilgen);
121 }
122 booleanValue.EmitCall(ilgen);
123 }
124 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.SHORT)
125 {
126 if (cast)
127 {
128 javaLangShort.EmitCheckcast(ilgen);
129 }
130 shortValue.EmitCall(ilgen);
131 }
132 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.CHAR)
133 {
134 if (cast)
135 {
136 javaLangCharacter.EmitCheckcast(ilgen);
137 }
138 charValue.EmitCall(ilgen);
139 }
140 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.INT)
141 {
142 if (cast)
143 {
144 javaLangInteger.EmitCheckcast(ilgen);
145 }
146 intValue.EmitCall(ilgen);
147 }
148 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.FLOAT)
149 {
150 if (cast)
151 {
152 javaLangFloat.EmitCheckcast(ilgen);
153 }
154 floatValue.EmitCall(ilgen);
155 }
156 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.LONG)
157 {
158 if (cast)
159 {
160 javaLangLong.EmitCheckcast(ilgen);
161 }
162 longValue.EmitCall(ilgen);
163 }
164 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.DOUBLE)
165 {
166 if (cast)
167 {
168 javaLangDouble.EmitCheckcast(ilgen);
169 }
170 doubleValue.EmitCall(ilgen);
171 }
172 else
173 {
174 throw new InvalidOperationException();
175 }
176 }
177
178 internal void EmitBox(CodeEmitter ilgen, RuntimeJavaType tw)
179 {
180 if (tw == ilgen.Context.PrimitiveJavaTypeFactory.BYTE)
181 {
182 valueOfByte.EmitCall(ilgen);
183 }
184 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.BOOLEAN)
185 {
186 valueOfBoolean.EmitCall(ilgen);
187 }
188 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.SHORT)
189 {
190 valueOfShort.EmitCall(ilgen);
191 }
192 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.CHAR)
193 {
194 valueOfCharacter.EmitCall(ilgen);
195 }
196 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.INT)
197 {
198 valueOfInteger.EmitCall(ilgen);
199 }
200 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.FLOAT)
201 {
202 valueOfFloat.EmitCall(ilgen);
203 }
204 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.LONG)
205 {
206 valueOfLong.EmitCall(ilgen);
207 }
208 else if (tw == ilgen.Context.PrimitiveJavaTypeFactory.DOUBLE)
209 {
210 valueOfDouble.EmitCall(ilgen);
211 }
212 else
213 {
214 throw new InvalidOperationException();
215 }
216 }
217
218 }
219
220}
Boxer(RuntimeContext context)
Initializes a new instance.
Definition Boxer.cs:61
RuntimeContext Context
Gets the RuntimeContext that hosts this code emitter.
Runtime support for a class loader.
Maintains services relevant to an instane of the IKVM runtime.
RuntimePrimitiveJavaTypeFactory PrimitiveJavaTypeFactory
Gets the RuntimePrimitiveJavaTypeFactory associated with this instance of the runtime.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.