Skip to content

Commit

Permalink
[GR-11216] Create classloaders for all types.
Browse files Browse the repository at this point in the history
PullRequest: graal/1972
  • Loading branch information
vjovanov committed Aug 11, 2018
2 parents a4070ec + f9ec0db commit 9b276ba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import com.oracle.svm.core.jdk.JDK8OrEarlier;
import com.oracle.svm.core.jdk.JDK9OrLater;
import com.oracle.svm.core.jdk.Resources;
import com.oracle.svm.core.jdk.Target_java_lang_ClassLoader;
import com.oracle.svm.core.jdk.Target_java_lang_Module;
import com.oracle.svm.core.meta.SharedType;
import com.oracle.svm.core.snippets.KnownIntrinsics;
Expand Down Expand Up @@ -213,6 +214,11 @@ public final class DynamicHub implements JavaKind.FormatWithToString, AnnotatedE
*/
private Object annotationsEncoding;

/**
* Classloader used for loading this class during image-build time.
*/
private final Target_java_lang_ClassLoader classloader;

/**
* Bits used for instance-of checks. A bit is set for each type, which an object with this HUB
* is an instance of.
Expand All @@ -236,7 +242,8 @@ public final class DynamicHub implements JavaKind.FormatWithToString, AnnotatedE
@Alias private static java.security.ProtectionDomain allPermDomain;

@Platforms(Platform.HOSTED_ONLY.class)
public DynamicHub(String name, boolean isLocalClass, DynamicHub superType, DynamicHub componentHub, String sourceFileName, boolean isStatic, boolean isSynthetic) {
public DynamicHub(String name, boolean isLocalClass, DynamicHub superType, DynamicHub componentHub, String sourceFileName, boolean isStatic, boolean isSynthetic,
Target_java_lang_ClassLoader classLoader) {
/* Class names must be interned strings according to the Java spec. */
this.name = name.intern();
this.isLocalClass = isLocalClass;
Expand All @@ -247,6 +254,7 @@ public DynamicHub(String name, boolean isLocalClass, DynamicHub superType, Dynam
this.annotatedSuperInfo = AnnotatedSuperInfo.forEmpty();
this.isStatic = isStatic;
this.isSynthetic = isSynthetic;
this.classloader = classLoader;
}

@Platforms(Platform.HOSTED_ONLY.class)
Expand Down Expand Up @@ -486,16 +494,6 @@ public Enum<?>[] getEnumConstantsShared() {
return enumConstants;
}

@Substitute
private Object getClassLoader() {
/*
* null is an allowed result value, denoting that the class was loaded by the system class
* loader. This means we simulate a system where all classes are loaded by the system class
* loader.
*/
return null;
}

@Substitute
private InputStream getResourceAsStream(String resourceName) {
final String path = resolveName(getName(), resourceName);
Expand Down Expand Up @@ -525,9 +523,12 @@ private String resolveName(String baseName, String resourceName) {
}
}

@KeepOriginal
private native ClassLoader getClassLoader();

@Substitute
private Object getClassLoader0() {
return null;
private ClassLoader getClassLoader0() {
return KnownIntrinsics.unsafeCast(classloader, ClassLoader.class);
}

@KeepOriginal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,27 @@ public static class ClassLoaderSupport {
public static ClassLoaderSupport getInstance() {
return ImageSingletons.lookup(ClassLoaderSupport.class);
}

public Target_java_lang_ClassLoader getOrCreate(ClassLoader classLoader) {
createClassLoaders(classLoader);
return classLoaders.get(classLoader);
}

public void createClassLoaders(ClassLoader loader) {
if (loader == null) {
return;
}
Map<ClassLoader, Target_java_lang_ClassLoader> loaders = ClassLoaderSupport.getInstance().classLoaders;
if (!loaders.containsKey(loader)) {
ClassLoader parent = loader.getParent();
if (parent != null) {
createClassLoaders(parent);
loaders.put(loader, new Target_java_lang_ClassLoader(loaders.get(parent)));
} else {
loaders.put(loader, new Target_java_lang_ClassLoader());
}
}
}
}

@Platforms(Platform.HOSTED_ONLY.class)//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
*/
package com.oracle.svm.hosted;

import java.util.Map;

import org.graalvm.nativeimage.Feature;
import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.jdk.JavaLangSubstitutions.ClassLoaderSupport;
import com.oracle.svm.core.jdk.Target_java_lang_ClassLoader;

@AutomaticFeature
public class ClassLoaderFeature implements Feature {
Expand All @@ -40,30 +37,17 @@ public void afterRegistration(AfterRegistrationAccess access) {
ImageSingletons.add(ClassLoaderSupport.class, new ClassLoaderSupport());
}

private void createClassLoaders(ClassLoader classLoader) {
Map<ClassLoader, Target_java_lang_ClassLoader> classLoaders = ClassLoaderSupport.getInstance().classLoaders;
if (!classLoaders.containsKey(classLoader)) {
ClassLoader parent = classLoader.getParent();
if (parent != null) {
createClassLoaders(parent);
classLoaders.put(classLoader, new Target_java_lang_ClassLoader(classLoaders.get(parent)));
} else {
classLoaders.put(classLoader, new Target_java_lang_ClassLoader());
}
}
}

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
createClassLoaders(ClassLoader.getSystemClassLoader());
ClassLoaderSupport.getInstance().createClassLoaders(ClassLoader.getSystemClassLoader());
ClassLoaderSupport.getInstance().systemClassLoader = ClassLoaderSupport.getInstance().classLoaders.get(ClassLoader.getSystemClassLoader());
}

@Override
public void duringSetup(DuringSetupAccess access) {
access.registerObjectReplacer(object -> {
if (object instanceof ClassLoader) {
createClassLoaders((ClassLoader) object);
ClassLoaderSupport.getInstance().createClassLoaders((ClassLoader) object);
return ClassLoaderSupport.getInstance().classLoaders.get(object);
}
return object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import com.oracle.svm.core.graal.meta.SubstrateForeignCallLinkage;
import com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider;
import com.oracle.svm.core.hub.DynamicHub;
import com.oracle.svm.core.jdk.JavaLangSubstitutions.ClassLoaderSupport;
import com.oracle.svm.core.jdk.Target_java_lang_ClassLoader;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.meta.HostedType;
import com.oracle.svm.hosted.phases.AnalysisGraphBuilderPhase;
Expand Down Expand Up @@ -197,9 +199,12 @@ private DynamicHub createHub(AnalysisType type) {
if (type.isArray()) {
componentHub = dynamicHub(type.getComponentType());
}
boolean isStatic = Modifier.isStatic(type.getJavaClass().getModifiers());
boolean isSynthetic = type.getJavaClass().isSynthetic();
return new DynamicHub(type.toClassName(), type.isLocal(), superHub, componentHub, type.getSourceFileName(), isStatic, isSynthetic);
Class<?> javaClass = type.getJavaClass();
boolean isStatic = Modifier.isStatic(javaClass.getModifiers());
boolean isSynthetic = javaClass.isSynthetic();

Target_java_lang_ClassLoader hubClassLoader = ClassLoaderSupport.getInstance().getOrCreate(javaClass.getClassLoader());
return new DynamicHub(type.toClassName(), type.isLocal(), superHub, componentHub, type.getSourceFileName(), isStatic, isSynthetic, hubClassLoader);
}

public static boolean isUnknownClass(ResolvedJavaType resolvedJavaType) {
Expand Down

0 comments on commit 9b276ba

Please sign in to comment.