IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
ClassLoader.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-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;
25using System.Buffers;
26using System.Linq;
27using System.Runtime.InteropServices;
28
29using IKVM.ByteCode;
30using IKVM.ByteCode.Encoding;
31using IKVM.Runtime;
32
33using sun.nio.ch;
34
36{
37
38 static partial class ClassLoader
39 {
40
44 public static void registerNatives()
45 {
46
47 }
48
59 public static global::java.lang.Class defineClass0(global::java.lang.ClassLoader self, string name, byte[] b, int off, int len, global::java.security.ProtectionDomain pd)
60 {
61#if FIRST_PASS
62 throw new NotImplementedException();
63#else
64 var buf = new byte[len];
65 Array.Copy(b, off, buf, 0, len);
66 return DefineClass(self, name, ReadClass(buf), pd, null);
67#endif
68 }
69
82 public static global::java.lang.Class defineClass1(global::java.lang.ClassLoader self, string name, byte[] b, int off, int len, global::java.security.ProtectionDomain pd, string source)
83 {
84#if FIRST_PASS
85 throw new NotImplementedException();
86#else
87 var buf = new byte[len];
88 Array.Copy(b, off, buf, 0, len);
89 return DefineClass(self, name, ReadClass(buf), pd, source);
90#endif
91 }
92
104 public static unsafe global::java.lang.Class defineClass2(global::java.lang.ClassLoader self, string name, global::java.nio.ByteBuffer bb, int off, int len, global::java.security.ProtectionDomain pd, string source)
105 {
106#if FIRST_PASS
107 throw new NotImplementedException();
108#else
109 var buf = new byte[bb.remaining()];
110 bb.get(buf);
111 return DefineClass(self, name, ReadClass(buf), pd, source);
112#endif
113 }
114
115#if FIRST_PASS == false
116
122 static IKVM.ByteCode.Decoding.ClassFile ReadClass(byte[] buffer)
123 {
124 try
125 {
126 return IKVM.ByteCode.Decoding.ClassFile.Read(buffer);
127 }
128 catch (InvalidClassMagicException)
129 {
130 throw new global::java.lang.ClassFormatError("Incompatible magic value");
131 }
132 catch (ByteCodeException e)
133 {
134 throw new global::java.lang.ClassFormatError(e.Message);
135 }
136 }
137
147 static global::java.lang.Class DefineClass(global::java.lang.ClassLoader self, string name, IKVM.ByteCode.Decoding.ClassFile clazz, global::java.security.ProtectionDomain pd, string source)
148 {
149 try
150 {
151 var runtimeClassLoader = JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(self);
152 var classFile = new IKVM.Runtime.ClassFile(JVM.Context, JVM.Context.Diagnostics, clazz, name, runtimeClassLoader.ClassFileParseOptions, null);
153 if (name != null && classFile.Name != name)
154 throw new global::java.lang.NoClassDefFoundError(name + " (wrong name: " + classFile.Name + ")");
155
156 var type = runtimeClassLoader.DefineClass(classFile, pd);
157 if (type == null)
158 throw new InternalException();
159
160 return type.ClassObject;
161 }
163 {
164 throw x.ToJava();
165 }
166 }
167
168#endif
169
175 public static void resolveClass0(global::java.lang.ClassLoader self, global::java.lang.Class clazz)
176 {
177
178 }
179
186 public static global::java.lang.Class findBootstrapClass(global::java.lang.ClassLoader self, string name)
187 {
188#if FIRST_PASS
189 throw new NotImplementedException();
190#else
191 try
192 {
193 return JVM.Context.ClassLoaderFactory.GetBootstrapClassLoader().TryLoadClassByName(name)?.ClassObject;
194 }
196 {
197 throw x.ToJava();
198 }
199#endif
200 }
201
208 public static global::java.lang.Class findLoadedClass0(global::java.lang.ClassLoader self, string name)
209 {
210#if FIRST_PASS
211 throw new NotImplementedException();
212#else
213 return name != null ? (JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(self).FindLoadedClass(name)?.ClassObject) : null;
214#endif
215 }
216
222 public static global::java.net.URL getBootstrapResource(string name)
223 {
224#if FIRST_PASS
225 throw new NotImplementedException();
226#else
227 return JVM.Context.ClassLoaderFactory.GetBootstrapClassLoader().GetResources(name).FirstOrDefault();
228#endif
229 }
230
236 public static global::java.util.Enumeration getBootstrapResources(string name)
237 {
238#if FIRST_PASS
239 throw new NotImplementedException();
240#else
241 return new global::ikvm.runtime.EnumerationWrapper(JVM.Context.ClassLoaderFactory.GetBootstrapClassLoader().GetResources(name));
242#endif
243 }
244
249 public static object retrieveDirectives()
250 {
251 return IKVM.Runtime.Assertions.RetrieveDirectives();
252 }
253
254 }
255
256}
static global::java.lang.Class defineClass1(global::java.lang.ClassLoader self, string name, byte[] b, int off, int len, global::java.security.ProtectionDomain pd, string source)
Implements the native method 'defineClass1'.
static global::java.lang.Class defineClass0(global::java.lang.ClassLoader self, string name, byte[] b, int off, int len, global::java.security.ProtectionDomain pd)
Implements the native method 'defineClass0'.
static global::java.lang.Class findLoadedClass0(global::java.lang.ClassLoader self, string name)
Implements the native method 'findLoadedClass0'.
static unsafe global::java.lang.Class defineClass2(global::java.lang.ClassLoader self, string name, global::java.nio.ByteBuffer bb, int off, int len, global::java.security.ProtectionDomain pd, string source)
Implements the native method 'defineClass2'.
static void registerNatives()
Implements the native method 'registerNatives'.
static object retrieveDirectives()
Implements the native method 'retrieveDirectives'.
static global::java.lang.Class findBootstrapClass(global::java.lang.ClassLoader self, string name)
Implements the native method 'findBootstrapClass'.
static global::java.util.Enumeration getBootstrapResources(string name)
Implements the native method 'getBootstrapResources'.
static global::java.net.URL getBootstrapResource(string name)
Implements the native method 'getBootstrapResource'.
static void resolveClass0(global::java.lang.ClassLoader self, global::java.lang.Class clazz)
Implements the native method 'resolveClass0'.
Represents an internal error that occurred within IKVM.
Main state of the running JVM.
.NET exception that corresponds to a Java exception.
IDiagnosticHandler Diagnostics
Gets the IDiagnosticHandler where events should be sent.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.