IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IKVM.Tools.Exporter.ExportImpl Class Reference

Main entry point for the application. More...

Public Member Functions

 ExportImpl (ExportOptions options, IDiagnosticHandler diagnostics)
 Initializes a new instance.
 
int Execute ()
 Executes the exporter.
 

Detailed Description

Main entry point for the application.

Definition at line 22 of file ExportImpl.cs.

Constructor & Destructor Documentation

◆ ExportImpl()

IKVM.Tools.Exporter.ExportImpl.ExportImpl ( ExportOptions options,
IDiagnosticHandler diagnostics )

Initializes a new instance.

Parameters
options

Definition at line 37 of file ExportImpl.cs.

38 {
39 this.options = options ?? throw new ArgumentNullException(nameof(options));
40 this.diagnostics = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics));
41 }

Member Function Documentation

◆ Execute()

int IKVM.Tools.Exporter.ExportImpl.Execute ( )

Executes the exporter.

Definition at line 46 of file ExportImpl.cs.

47 {
48 var references = new List<string>();
49 if (options.References != null)
50 foreach (var reference in options.References)
51 references.Add(reference);
52
53 var libpaths = new List<string>();
54 if (options.Libraries != null)
55 foreach (var library in options.Libraries)
56 libpaths.Add(library);
57
58 var namespaces = new List<string>();
59 if (options.Namespaces != null)
60 foreach (var ns in options.Namespaces)
61 namespaces.Add(ns);
62
63 if (File.Exists(options.Assembly) && options.NoStdLib)
64 {
65 // Add the target assembly to the references list, to allow it to be considered as "mscorlib".
66 // This allows "ikvmstub -nostdlib \...\mscorlib.dll" to work.
67 references.Add(options.Assembly);
68 }
69
70 // discover the core lib from the references
71 var coreLibName = FindCoreLibName(references, libpaths);
72 if (coreLibName == null)
73 {
74 diagnostics.CoreClassesMissing();
75 return 1;
76 }
77
78 // build universe and resolver against universe and references
79 var universe = new Universe(coreLibName);
80 var assemblyResolver = new AssemblyResolver();
81 assemblyResolver.Warning += new AssemblyResolver.WarningEvent(Resolver_Warning);
82 assemblyResolver.Init(universe, options.NoStdLib, references, libpaths);
83
84 var cache = new Dictionary<string, Assembly>();
85 foreach (var reference in references)
86 {
87 Assembly[] dummy = null;
88 if (assemblyResolver.ResolveReference(cache, ref dummy, reference) == false)
89 {
90 diagnostics.ReferenceNotFound(reference);
91 return 1;
92 }
93 }
94
95 Assembly assembly = null;
96 try
97 {
98 file = new FileInfo(options.Assembly);
99 }
100 catch (FileNotFoundException)
101 {
102 diagnostics.InputFileNotFound(options.Assembly);
103 return 1;
104 }
105 catch (Exception x)
106 {
107 diagnostics.ErrorReadingFile(options.Assembly, x.ToString());
108 return 1;
109 }
110
111 if (file != null && file.Exists)
112 {
113 assembly = assemblyResolver.LoadFile(options.Assembly);
114 }
115 else
116 {
117 assembly = assemblyResolver.LoadWithPartialName(options.Assembly);
118 }
119
120 StaticCompiler compiler;
121 RuntimeContext context;
122
123 int rc = 0;
124 if (assembly == null)
125 {
126 rc = 1;
127 diagnostics.ReferenceNotFound(options.Assembly);
128 }
129 else
130 {
131 Assembly runtimeAssembly = null;
132 Assembly baseAssembly = null;
133
134 if (options.Bootstrap)
135 {
136 var runtimeAssemblyPath = references.FirstOrDefault(i => Path.GetFileNameWithoutExtension(i) == "IKVM.Runtime");
137 if (runtimeAssemblyPath != null)
138 runtimeAssembly = assemblyResolver.LoadFile(runtimeAssemblyPath);
139
140 if (runtimeAssembly == null || runtimeAssembly.__IsMissing)
141 {
142 diagnostics.RuntimeNotFound();
143 return 1;
144 }
145
146 compiler = new StaticCompiler(universe, assemblyResolver, runtimeAssembly);
147 context = new RuntimeContext(new RuntimeContextOptions(), diagnostics, new ManagedTypeResolver(compiler, null), true, compiler);
148 context.ClassLoaderFactory.SetBootstrapClassLoader(new RuntimeBootstrapClassLoader(context));
149 }
150 else
151 {
152
153 var runtimeAssemblyPath = references.FirstOrDefault(i => Path.GetFileNameWithoutExtension(i) == "IKVM.Runtime");
154 if (runtimeAssemblyPath != null)
155 runtimeAssembly = assemblyResolver.LoadFile(runtimeAssemblyPath);
156
157 var baseAssemblyPath = references.FirstOrDefault(i => Path.GetFileNameWithoutExtension(i) == "IKVM.Java");
158 if (baseAssemblyPath != null)
159 baseAssembly = assemblyResolver.LoadFile(baseAssemblyPath);
160
161 if (runtimeAssembly == null || runtimeAssembly.__IsMissing)
162 {
163 diagnostics.RuntimeNotFound();
164 return 1;
165 }
166
167 if (baseAssembly == null || runtimeAssembly.__IsMissing)
168 {
169 diagnostics.CoreClassesMissing();
170 return 1;
171 }
172
173 compiler = new StaticCompiler(universe, assemblyResolver, runtimeAssembly);
174 context = new RuntimeContext(new RuntimeContextOptions(), diagnostics, new ManagedTypeResolver(compiler, baseAssembly), false, compiler);
175 }
176
177 if (context.AttributeHelper.IsJavaModule(assembly.ManifestModule))
178 {
179 diagnostics.ExportingImportsNotSupported();
180 return 1;
181 }
182
183 if (options.Output == null)
184 options.Output = assembly.GetName().Name + ".jar";
185
186 try
187 {
188 using (zipFile = new ZipArchive(new FileStream(options.Output, FileMode.Create), ZipArchiveMode.Create))
189 {
190 try
191 {
192 var assemblies = new List<Assembly>();
193 assemblies.Add(assembly);
194
195 if (options.Shared)
196 LoadSharedClassLoaderAssemblies(compiler, assembly, assemblies);
197
198 foreach (var asm in assemblies)
199 {
200 if (ProcessTypes(context, asm.GetTypes()) != 0)
201 {
202 rc = 1;
203 if (options.ContinueOnError == false)
204 break;
205 }
206
207 if (options.Forwarders && ProcessTypes(context, asm.ManifestModule.__GetExportedTypes()) != 0)
208 {
209 rc = 1;
210 if (options.ContinueOnError == false)
211 break;
212 }
213 }
214 }
215 catch (Exception x)
216 {
217 if (options.ContinueOnError == false)
218 diagnostics.UnknownWarning($"Assembly reflection encountered an error. Resultant JAR may be incomplete. ({x.Message})");
219
220 rc = 1;
221 }
222 }
223 }
224 catch (InvalidDataException e)
225 {
226 rc = 1;
227 diagnostics.InvalidZip(options.Output);
228 }
229 }
230
231 return rc;
232 }
Maintains services relevant to an instane of the IKVM runtime.
AttributeHelper AttributeHelper
Gets the AttributeHelper associated with this instance of the runtime.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.
List< string > Libraries
Path to directories to search for assembly references.
bool Forwarders
Whether to include forwarder types.
List< string > References
Assembly references.
bool ContinueOnError
Whether to continue execution on error.
bool NoStdLib
Whether to include the standard library references.
string Assembly
Paths to assembly to export.
string Output
Output JAR file.
bool Shared
Are we doing a shared export.
List< string > Namespaces
Set of namespaces to export.
bool Bootstrap
Whether we are to run in bootstrap mode.
void ReferenceNotFound(string arg0)
The 'ReferenceNotFound' diagnostic.
void InputFileNotFound(string arg0)
The 'InputFileNotFound' diagnostic.
void ErrorReadingFile(string arg0, string arg1)
The 'ErrorReadingFile' diagnostic.
void RuntimeNotFound()
The 'RuntimeNotFound' diagnostic.
void ExportingImportsNotSupported()
The 'ExportingImportsNotSupported' diagnostic.
void UnknownWarning(string arg0)
The 'UnknownWarning' diagnostic.
void CoreClassesMissing()
The 'CoreClassesMissing' diagnostic.
void InvalidZip(string name)
The 'InvalidZip' diagnostic.

The documentation for this class was generated from the following file: