2015-03-13 18:52:59 +00:00
|
|
|
/* Copyright (c) 2008-2015, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2009-05-26 03:36:29 +00:00
|
|
|
package avian;
|
2007-07-30 23:19:05 +00:00
|
|
|
|
2007-08-10 23:45:47 +00:00
|
|
|
import java.net.URL;
|
|
|
|
import java.net.MalformedURLException;
|
2011-03-27 00:20:45 +00:00
|
|
|
import java.io.IOException;
|
2010-12-06 00:40:50 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Enumeration;
|
2013-10-28 13:52:58 +00:00
|
|
|
import java.util.NoSuchElementException;
|
2007-08-10 23:45:47 +00:00
|
|
|
|
2007-07-30 23:19:05 +00:00
|
|
|
public class SystemClassLoader extends ClassLoader {
|
support AOT-compilation of Java 8 lambda expressions
These expressions are tricky because they rely on invokedynamic, which
normally implies runtime code generation. However, since lambdas
don't actually use the "dynamicness" of invokedynamic, we can convert
them into static calls to synthetic classes at compile time.
Since I had already written code to synthesize such classes in Java
and I didn't want to rewrite it in C++, I needed to add support for
running Java code to the bootimage generator. And since the primary
VM used by the generator is purpose-built to generate AOT-compiled
code for a specific target architecture and is not capable of
generating or running JIT-compiled code for the host architecture, I
added support for loading a second, independent, host-specific VM for
running Java code.
The rest of the patch handles the fact that each method compilation
might cause new, synthetic classes to be created, so we need to make
sure those classes and their methods are included in the final heap
and code images. This required breaking some giant code blocks out of
makeCodeImage into their own methods, which makes the diff look
scarier than it really is.
2015-09-13 02:15:46 +00:00
|
|
|
public static native ClassLoader appLoader();
|
|
|
|
|
2010-09-14 16:49:41 +00:00
|
|
|
private native VMClass findVMClass(String name)
|
2010-09-01 16:13:52 +00:00
|
|
|
throws ClassNotFoundException;
|
|
|
|
|
|
|
|
protected Class findClass(String name) throws ClassNotFoundException {
|
|
|
|
return getClass(findVMClass(name));
|
|
|
|
}
|
2007-07-30 23:19:05 +00:00
|
|
|
|
2010-09-10 21:05:29 +00:00
|
|
|
public static native Class getClass(VMClass vmClass);
|
|
|
|
|
2013-02-21 22:37:17 +00:00
|
|
|
public static native VMClass vmClass(Class jClass);
|
|
|
|
|
2010-09-14 16:49:41 +00:00
|
|
|
private native VMClass findLoadedVMClass(String name);
|
2010-09-01 16:13:52 +00:00
|
|
|
|
|
|
|
protected Class reallyFindLoadedClass(String name){
|
|
|
|
VMClass c = findLoadedVMClass(name);
|
|
|
|
return c == null ? null : getClass(c);
|
|
|
|
}
|
2007-08-10 23:45:47 +00:00
|
|
|
|
2013-02-21 22:37:17 +00:00
|
|
|
protected Class loadClass(String name, boolean resolve)
|
|
|
|
throws ClassNotFoundException
|
|
|
|
{
|
|
|
|
Class c = findLoadedClass(name);
|
|
|
|
if (c == null) {
|
|
|
|
ClassLoader parent = getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
try {
|
|
|
|
c = parent.loadClass(name);
|
|
|
|
} catch (ClassNotFoundException ok) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == null) {
|
|
|
|
c = findClass(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolve) {
|
|
|
|
resolveClass(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2011-03-26 01:14:21 +00:00
|
|
|
private native String resourceURLPrefix(String name);
|
2007-08-10 23:45:47 +00:00
|
|
|
|
|
|
|
protected URL findResource(String name) {
|
2011-03-26 01:14:21 +00:00
|
|
|
String prefix = resourceURLPrefix(name);
|
|
|
|
if (prefix != null) {
|
2007-08-10 23:45:47 +00:00
|
|
|
try {
|
2011-03-26 01:14:21 +00:00
|
|
|
return new URL(prefix + name);
|
2007-08-10 23:45:47 +00:00
|
|
|
} catch (MalformedURLException ignored) { }
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2010-12-06 00:40:50 +00:00
|
|
|
|
2014-03-19 17:21:26 +00:00
|
|
|
protected Package getPackage(String name) {
|
|
|
|
Package p = super.getPackage(name);
|
|
|
|
if (p == null) {
|
|
|
|
String source = getPackageSource(name);
|
|
|
|
if (source != null) {
|
|
|
|
// todo: load attributes from JAR manifest
|
|
|
|
definePackage(name, null, null, null, null, null, null, null);
|
2015-10-19 21:10:53 +00:00
|
|
|
} else {
|
|
|
|
definePackage(name, null, null, null, null, null, null, null);
|
2014-03-19 17:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.getPackage(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static native String getPackageSource(String name);
|
|
|
|
|
2011-03-27 00:20:45 +00:00
|
|
|
// OpenJDK's java.lang.ClassLoader.getResource makes use of
|
|
|
|
// sun.misc.Launcher to load bootstrap resources, which is not
|
|
|
|
// appropriate for the Avian build, so we override it to ensure we
|
|
|
|
// get the behavior we want. This implementation is the same as
|
|
|
|
// that of Avian's java.lang.ClassLoader.getResource.
|
|
|
|
public URL getResource(String path) {
|
|
|
|
URL url = null;
|
|
|
|
ClassLoader parent = getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
url = parent.getResource(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url == null) {
|
|
|
|
url = findResource(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// As above, we override this method to avoid inappropriate behavior
|
|
|
|
// in OpenJDK's java.lang.ClassLoader.getResources.
|
|
|
|
public Enumeration<URL> getResources(String name) throws IOException {
|
|
|
|
Collection<URL> urls = new ArrayList<URL>(5);
|
|
|
|
|
|
|
|
ClassLoader parent = getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
for (Enumeration<URL> e = parent.getResources(name);
|
|
|
|
e.hasMoreElements();)
|
|
|
|
{
|
|
|
|
urls.add(e.nextElement());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 13:53:22 +00:00
|
|
|
Enumeration<URL> urls2 = findResources(name);
|
|
|
|
while (urls2.hasMoreElements()) {
|
|
|
|
urls.add(urls2.nextElement());
|
2011-03-27 00:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Collections.enumeration(urls);
|
|
|
|
}
|
|
|
|
|
2013-10-28 13:52:58 +00:00
|
|
|
private class ResourceEnumeration implements Enumeration<URL> {
|
|
|
|
private long[] finderElementPtrPtr;
|
|
|
|
private String name, urlPrefix;
|
|
|
|
|
|
|
|
public ResourceEnumeration(String name) {
|
|
|
|
this.name = name;
|
|
|
|
finderElementPtrPtr = new long[1];
|
|
|
|
urlPrefix = nextResourceURLPrefix();
|
2010-12-06 00:40:50 +00:00
|
|
|
}
|
2013-10-28 13:52:58 +00:00
|
|
|
|
|
|
|
private native String nextResourceURLPrefix(SystemClassLoader loader,
|
|
|
|
String name, long[] finderElementPtrPtr);
|
|
|
|
|
|
|
|
private String nextResourceURLPrefix() {
|
|
|
|
return nextResourceURLPrefix(SystemClassLoader.this, name,
|
|
|
|
finderElementPtrPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasMoreElements() {
|
|
|
|
return urlPrefix != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public URL nextElement() {
|
|
|
|
if (urlPrefix == null) throw new NoSuchElementException();
|
|
|
|
URL result;
|
|
|
|
try {
|
|
|
|
result = new URL(urlPrefix + name);
|
|
|
|
} catch (MalformedURLException ignored) {
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
if (finderElementPtrPtr[0] == 0l) urlPrefix = null;
|
|
|
|
else urlPrefix = nextResourceURLPrefix();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Enumeration<URL> findResources(String name) {
|
|
|
|
return new ResourceEnumeration(name);
|
2010-12-06 00:40:50 +00:00
|
|
|
}
|
2007-07-30 23:19:05 +00:00
|
|
|
}
|