IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Handler.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.IO;
26using System.Reflection;
27
28using IKVM.Runtime;
29
30#if NETCOREAPP
31using System.Runtime.Loader;
32#endif
33
35{
36
37 static class Handler
38 {
39
40 public static byte[] GenerateStub(global::java.lang.Class c)
41 {
42#if FIRST_PASS
43 throw new NotImplementedException();
44#else
45 using var mem = new MemoryStream();
46 JVM.Context.StubGenerator.Write(mem, RuntimeJavaType.FromClass(c), true, true, true, true, false);
47 return mem.ToArray();
48#endif
49 }
50
51 public static Stream ReadResourceFromAssemblyImpl(Assembly asm, string resource)
52 {
53#if FIRST_PASS
54 throw new NotImplementedException();
55#else
56 // chop off the leading slash
57 resource = resource.Substring(1);
58 var mangledName = JVM.MangleResourceName(resource);
59 var info = asm.GetManifestResourceInfo(mangledName);
60 if (info != null && info.FileName != null)
61 return asm.GetManifestResourceStream(mangledName);
62
63 var s = asm.GetManifestResourceStream(mangledName);
64 if (s == null)
65 {
66 JVM.Context.Diagnostics.GenericClassLoadingWarning($"Resource \"{resource}\" not found in {asm.FullName}");
67 throw new FileNotFoundException("resource " + resource + " not found in assembly " + asm.FullName);
68 }
69
70 switch (s.ReadByte())
71 {
72 case 0:
73 JVM.Context.Diagnostics.GenericClassLoadingInfo($"Reading resource \"{resource}\" from {asm.FullName}");
74 return s;
75 case 1:
76 JVM.Context.Diagnostics.GenericClassLoadingInfo($"Reading compressed resource \"{resource}\" from {asm.FullName}");
77 return new System.IO.Compression.DeflateStream(s, System.IO.Compression.CompressionMode.Decompress, false);
78 default:
79 JVM.Context.Diagnostics.GenericClassLoadingError($"Resource \"{resource}\" in {asm.FullName} has an unsupported encoding");
80 throw new IOException("Unsupported resource encoding for resource " + resource + " found in assembly " + asm.FullName);
81 }
82#endif
83 }
84
85 public static object LoadClassFromAssembly(Assembly asm, string className)
86 {
87#if FIRST_PASS
88 throw new NotImplementedException();
89#else
90 var tw = JVM.Context.AssemblyClassLoaderFactory.FromAssembly(asm).TryLoadClassByName(className);
91 if (tw != null)
92 return tw.ClassObject;
93
94 return null;
95#endif
96 }
97
98 public static Assembly LoadAssembly(string name)
99 {
100#if NETFRAMEWORK
101 return Assembly.Load(name);
102#else
103 return AssemblyLoadContext.GetLoadContext(typeof(Handler).Assembly).LoadFromAssemblyName(new AssemblyName(name));
104#endif
105 }
106
107 public static global::java.lang.ClassLoader GetGenericClassLoaderById(int id)
108 {
109#if FIRST_PASS
110 throw new NotImplementedException();
111#else
112 return JVM.Context.ClassLoaderFactory.GetGenericClassLoaderById(id).GetJavaClassLoader();
113#endif
114 }
115
116 }
117
118}
IKVM.Reflection.Assembly Assembly
IKVM.Reflection.AssemblyName AssemblyName
static Stream ReadResourceFromAssemblyImpl(Assembly asm, string resource)
Definition Handler.cs:51
static object LoadClassFromAssembly(Assembly asm, string className)
Definition Handler.cs:85
static global::java.lang.ClassLoader GetGenericClassLoaderById(int id)
Definition Handler.cs:107
static byte[] GenerateStub(global::java.lang.Class c)
Definition Handler.cs:40
Main state of the running JVM.
RuntimeAssemblyClassLoader FromAssembly(Assembly assembly)
Obtains the RuntimeAssemblyClassLoader for the given Assembly. This method should not be used with dy...
IDiagnosticHandler Diagnostics
Gets the IDiagnosticHandler where events should be sent.
StubGenerator StubGenerator
Gets the StubGenerator associated with this instance of the runtime.
RuntimeAssemblyClassLoaderFactory AssemblyClassLoaderFactory
Gets the RuntimeAssemblyClassLoaderFactory associated with this instance of the runtime.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.
void GenericClassLoadingInfo(string arg0)
The 'GenericClassLoadingInfo' diagnostic.
void GenericClassLoadingError(string arg0)
The 'GenericClassLoadingError' diagnostic.
void GenericClassLoadingWarning(string arg0)
The 'GenericClassLoadingWarning' diagnostic.