Skip to content

Commit

Permalink
Consistently use DynamicHubCompanion directly from DynamicHub.
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-hofer committed Jan 14, 2025
1 parent c9cfe19 commit 18248a5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.util.Arrays;
import java.util.Map;

import com.oracle.svm.core.graal.meta.KnownOffsets;
import jdk.graal.compiler.core.common.NumUtil;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.word.LocationIdentity;
import org.graalvm.word.UnsignedWord;
Expand All @@ -48,6 +46,7 @@
import com.oracle.svm.core.allocationprofile.AllocationSite;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.configure.ConfigurationFile;
import com.oracle.svm.core.graal.meta.KnownOffsets;
import com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider;
import com.oracle.svm.core.graal.nodes.NewPodInstanceNode;
import com.oracle.svm.core.graal.nodes.NewStoredContinuationNode;
Expand Down Expand Up @@ -75,6 +74,7 @@
import jdk.graal.compiler.api.replacements.Snippet.NonNullParameter;
import jdk.graal.compiler.api.replacements.Snippet.VarargsParameter;
import jdk.graal.compiler.core.common.GraalOptions;
import jdk.graal.compiler.core.common.NumUtil;
import jdk.graal.compiler.core.common.spi.ForeignCallDescriptor;
import jdk.graal.compiler.core.common.spi.MetaAccessExtensionProvider;
import jdk.graal.compiler.core.common.type.StampFactory;
Expand Down Expand Up @@ -346,7 +346,7 @@ private static DynamicHub slowPathHubOrUnsafeInstantiationError(DynamicHub hub)
throw new InstantiationException("Cannot allocate objects of special hybrid types: " + DynamicHub.toClass(hub).getTypeName());
} else {
if (hub.canUnsafeInstantiateAsInstanceSlowPath()) {
hub.getCompanion().setUnsafeAllocate();
hub.setCanUnsafeAllocate();
return hub;
} else {
if (MissingRegistrationUtils.throwMissingRegistrationErrors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
import com.oracle.svm.core.imagelayer.DynamicImageLayerInfo;
import com.oracle.svm.core.jdk.JDK21OrEarlier;
import com.oracle.svm.core.jdk.JDKLatest;
import com.oracle.svm.core.jdk.ProtectionDomainSupport;
import com.oracle.svm.core.jdk.Resources;
import com.oracle.svm.core.meta.SharedType;
import com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils;
Expand Down Expand Up @@ -170,6 +171,9 @@ public final class DynamicHub implements AnnotatedElement, java.lang.reflect.Typ
@Substitute //
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];

/** Marker value for {@link DynamicHubCompanion#classLoader}. */
static final Object NO_CLASS_LOADER = new Object();

@Platforms(Platform.HOSTED_ONLY.class) //
private final Class<?> hostedJavaClass;

Expand Down Expand Up @@ -395,8 +399,9 @@ public DynamicHub(Class<?> hostedJavaClass, String name, byte hubType, Reference

this.flags = flags;

this.companion = new DynamicHubCompanion(hostedJavaClass, hostedJavaClass.getModule(), superType, sourceFileName,
modifiers, classLoader, nestHost, simpleBinaryName, declaringClass, signature);
Object loader = PredefinedClassesSupport.isPredefined(hostedJavaClass) ? NO_CLASS_LOADER : classLoader;
this.companion = DynamicHubCompanion.createHosted(hostedJavaClass.getModule(), superType, sourceFileName,
modifiers, loader, nestHost, simpleBinaryName, declaringClass, signature);
}

/**
Expand Down Expand Up @@ -430,10 +435,10 @@ public static DynamicHub allocate(String name, DynamicHub superHub, DynamicHub c
// GR-59683
Module module = null;

DynamicHubCompanion companion = new DynamicHubCompanion(classLoader, module, superHub, sourceFileName, modifiers, nestHost, simpleBinaryName, declaringClass, signature);
DynamicHubCompanion companion = DynamicHubCompanion.createAtRuntime(module, superHub, sourceFileName, modifiers, classLoader, nestHost, simpleBinaryName, declaringClass, signature);

/* Always allow unsafe allocation for classes that were loaded at run-time. */
companion.setUnsafeAllocate();
companion.canUnsafeAllocate = true;

// GR-59687: Correct size and content for vtable
int vTableEntries = 0x100;
Expand Down Expand Up @@ -840,18 +845,26 @@ public boolean isInstantiated() {
}

public boolean canUnsafeInstantiateAsInstanceFastPath() {
return companion.canUnsafeAllocate();
return canUnsafeAllocate();
}

public boolean canUnsafeInstantiateAsInstanceSlowPath() {
if (ClassForNameSupport.canUnsafeInstantiateAsInstance(this)) {
companion.setUnsafeAllocate();
setCanUnsafeAllocate();
return true;
} else {
return false;
}
}

public boolean canUnsafeAllocate() {
return companion.canUnsafeAllocate;
}

public void setCanUnsafeAllocate() {
companion.canUnsafeAllocate = true;
}

public boolean isProxyClass() {
return isFlagSet(flags, IS_PROXY_CLASS_BIT);
}
Expand Down Expand Up @@ -1010,18 +1023,21 @@ public InputStream getResourceAsStream(String resourceName) {

@Substitute
public ClassLoader getClassLoader() {
return companion.getClassLoader();
Object loader = companion.classLoader;
VMError.guarantee(loader != NO_CLASS_LOADER);
return (ClassLoader) loader;
}

@KeepOriginal
private native ClassLoader getClassLoader0();

public boolean isLoaded() {
return companion.hasClassLoader();
return companion.classLoader != NO_CLASS_LOADER;
}

void setClassLoaderAtRuntime(ClassLoader loader) {
companion.setClassLoader(loader);
VMError.guarantee(companion.classLoader == NO_CLASS_LOADER && loader != NO_CLASS_LOADER);
companion.classLoader = loader;
}

@KeepOriginal
Expand Down Expand Up @@ -1613,7 +1629,10 @@ public String getPackageName() {
if (SubstrateUtil.HOSTED) { // avoid eager initialization in image heap
return computePackageName();
}
return companion.getPackageName(this);
if (companion.packageName == null) {
companion.packageName = computePackageName();
}
return companion.packageName;
}

private boolean isHybrid() {
Expand Down Expand Up @@ -1681,7 +1700,10 @@ public Object[] getSigners() {

@Substitute
public ProtectionDomain getProtectionDomain() {
return companion.getProtectionDomain();
if (companion.protectionDomain == null) {
companion.protectionDomain = ProtectionDomainSupport.allPermDomain();
}
return companion.protectionDomain;
}

@Substitute
Expand All @@ -1690,7 +1712,8 @@ private ProtectionDomain protectionDomain() {
}

void setProtectionDomainAtRuntime(ProtectionDomain protectionDomain) {
companion.setProtectionDomain(protectionDomain);
VMError.guarantee(companion.protectionDomain == null && protectionDomain != null);
companion.protectionDomain = protectionDomain;
}

@Substitute
Expand Down Expand Up @@ -1940,7 +1963,10 @@ private Class<?>[] getNestMembers0() {

@Substitute
private ClassRepository getGenericInfo() {
return companion.getGenericInfo(this);
if (companion.genericInfo == null) {
companion.genericInfo = computeGenericInfo();
}
return (companion.genericInfo != ClassRepository.NONE) ? companion.genericInfo : null;
}

ClassRepository computeGenericInfo() {
Expand Down Expand Up @@ -2061,11 +2087,11 @@ private static Constructor<?>[] filterConstructors(Constructor<?>... constructor
}

public void setJrfEventConfiguration(Object configuration) {
companion.setJfrEventConfiguration(configuration);
companion.jfrEventConfiguration = configuration;
}

public Object getJfrEventConfiguration() {
return companion.getJfrEventConfiguration();
return companion.jfrEventConfiguration;
}

public boolean isReached() {
Expand All @@ -2075,7 +2101,7 @@ public boolean isReached() {
private static final class ReflectionDataAccessors {
@SuppressWarnings("unused")
private static SoftReference<Target_java_lang_Class_ReflectionData<?>> getReflectionData(DynamicHub that) {
return that.companion.getReflectionData();
return that.companion.reflectionData;
}
}

Expand All @@ -2089,21 +2115,21 @@ private static int getClassRedefinedCount(DynamicHub that) {
private static final class ClassLoaderAccessors {
@SuppressWarnings("unused")
private static ClassLoader getClassLoader(DynamicHub that) {
return that.companion.getClassLoader();
return that.getClassLoader();
}
}

private static final class AnnotationDataAccessors {
@SuppressWarnings("unused")
private static Target_java_lang_Class_AnnotationData getAnnotationData(DynamicHub that) {
return that.companion.getAnnotationData();
return that.companion.annotationData;
}
}

private static final class AnnotationTypeAccessors {
@SuppressWarnings("unused")
private static AnnotationType getAnnotationType(DynamicHub that) {
return that.companion.getAnnotationType();
return that.companion.annotationType;
}
}

Expand All @@ -2117,12 +2143,12 @@ private static Constructor<?> getCachedConstructor(DynamicHub that) {
* initialized. We eagerly initialize the class to conform with JCK tests.
*/
that.ensureInitialized();
return that.companion.getCachedConstructor();
return that.companion.cachedConstructor;
}

@SuppressWarnings("unused")
private static void setCachedConstructor(DynamicHub that, Constructor<?> value) {
that.companion.setCachedConstructor(value);
that.companion.cachedConstructor = value;
}
}

Expand Down
Loading

0 comments on commit 18248a5

Please sign in to comment.