2
Copyright (C) 2006, 2007, 2010 Jeroen Frijters
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.
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:
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.
24
// HACK because of historical reasons this class' source lives in ikvm/internal instead of ikvm/runtime
27
import cli.System.Reflection.Assembly;
28
import gnu.java.util.EmptyEnumeration;
29
import ikvm.lang.Internal;
30
import java.io.IOException;
31
import java.net.MalformedURLException;
33
import java.util.Enumeration;
34
import java.util.Vector;
35
import java.util.jar.Attributes;
36
import java.util.jar.Manifest;
38
public final class AssemblyClassLoader extends ClassLoader
40
// NOTE assembly is null for "generics" class loader instances
41
private final Assembly assembly;
42
private boolean packagesDefined;
44
public AssemblyClassLoader(Assembly assembly)
46
this(assembly, System.getSecurityManager());
49
// this constructor is used by the runtime to avoid the security check (by passing in null as the security manager)
50
AssemblyClassLoader(Assembly assembly, SecurityManager security)
52
super(null, security);
53
this.assembly = assembly;
56
protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
58
return LoadClass(this, assembly, name);
61
private static native Class LoadClass(ClassLoader classLoader, Assembly assembly, String name) throws ClassNotFoundException;
63
public URL getResource(String name)
65
return getResource(this, assembly, name);
68
public Enumeration getResources(String name) throws IOException
70
return getResources(this, assembly, name);
73
protected URL findResource(String name)
75
return getResource(this, assembly, name);
78
protected Enumeration findResources(String name) throws IOException
80
return getResources(this, assembly, name);
84
public static native URL getResource(ClassLoader classLoader, Assembly assembly, String name);
87
public static native Enumeration getResources(ClassLoader classLoader, Assembly assembly, String name) throws IOException;
89
private static native String GetGenericClassLoaderName(Object classLoader);
90
// also used by java.lang.LangHelper
92
public static native String[] GetPackages(Assembly assembly);
94
private static native URL GetManifest(Assembly assembly);
96
private synchronized void lazyDefinePackagesCheck()
100
packagesDefined = true;
101
lazyDefinePackages();
105
private static String getAttributeValue(Attributes.Name name, Attributes first, Attributes second)
107
String result = null;
110
result = first.getValue(name);
112
if(second != null && result == null)
114
result = second.getValue(name);
119
private Manifest getManifest()
125
URL url = GetManifest(assembly);
128
return new Manifest(url.openStream());
132
catch (MalformedURLException _)
135
catch (IOException _)
141
private void lazyDefinePackages()
145
// generic class loader (doesn't support packages)
148
URL sealBase = getCodeBase();
149
Manifest manifest = getManifest();
150
Attributes attr = null;
153
attr = manifest.getMainAttributes();
155
String[] packages = GetPackages(assembly);
156
for(int i = 0; i < packages.length; i++)
158
String name = packages[i];
159
if(super.getPackage(name) == null)
161
Attributes entryAttr = null;
164
entryAttr = manifest.getAttributes(name.replace('.', '/') + '/');
167
getAttributeValue(Attributes.Name.SPECIFICATION_TITLE, entryAttr, attr),
168
getAttributeValue(Attributes.Name.SPECIFICATION_VERSION, entryAttr, attr),
169
getAttributeValue(Attributes.Name.SPECIFICATION_VENDOR, entryAttr, attr),
170
getAttributeValue(Attributes.Name.IMPLEMENTATION_TITLE, entryAttr, attr),
171
getAttributeValue(Attributes.Name.IMPLEMENTATION_VERSION, entryAttr, attr),
172
getAttributeValue(Attributes.Name.IMPLEMENTATION_VENDOR, entryAttr, attr),
173
"true".equalsIgnoreCase(getAttributeValue(Attributes.Name.SEALED, entryAttr, attr)) ? sealBase : null);
178
protected Package getPackage(String name)
180
lazyDefinePackagesCheck();
181
return super.getPackage(name);
184
protected Package[] getPackages()
186
lazyDefinePackagesCheck();
187
return super.getPackages();
190
public String toString()
194
return assembly.get_FullName();
196
return GetGenericClassLoaderName(this);
199
private URL getCodeBase()
205
if(false) throw new cli.System.NotSupportedException();
206
return new URL(assembly.get_CodeBase());
209
catch(cli.System.NotSupportedException _)
212
catch(MalformedURLException _)
218
// return the ClassLoader for the assembly. Note that this doesn't have to be an AssemblyClassLoader.
219
public static native ClassLoader getAssemblyClassLoader(Assembly asm);