Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.NOT_FREQUENT_PROBABILITY;
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability;

import java.util.concurrent.locks.ReentrantLock;

import org.graalvm.nativeimage.IsolateThread;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.jfr.JfrTicks;
import com.oracle.svm.core.jfr.SubstrateJVM;
import com.oracle.svm.core.jfr.events.JavaMonitorEnterEvent;
import com.oracle.svm.core.thread.JavaThreads;
import com.oracle.svm.core.util.BasedOnJDKClass;
import com.oracle.svm.core.util.VMError;

import jdk.internal.misc.Unsafe;
Expand All @@ -55,6 +58,8 @@
* to store the number of lock acquisitions, enabling various optimizations.</li>
* </ul>
*/
@BasedOnJDKClass(ReentrantLock.class)
@BasedOnJDKClass(value = ReentrantLock.class, innerClass = "Sync")
public class JavaMonitor extends JavaMonitorQueuedSynchronizer {
protected long latestJfrTid;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Function;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

import com.oracle.svm.core.annotate.TargetClass;

/**
* Documents that the class is based on a JDK class without {@linkplain TargetClass substituting}
* it.
*/
@Repeatable(BasedOnJDKClass.List.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE})
@Platforms(Platform.HOSTED_ONLY.class)
public @interface BasedOnJDKClass {

/**
* @see TargetClass#value()
*/
Class<?> value() default BasedOnJDKClass.class;

/**
* @see TargetClass#className()
*/
String className() default "";

/**
* @see TargetClass#classNameProvider()
*/
Class<? extends Function<BasedOnJDKClass, String>> classNameProvider() default BasedOnJDKClass.NoClassNameProvider.class;

/**
* @see TargetClass#innerClass()
*/
String[] innerClass() default {};

interface NoClassNameProvider extends Function<BasedOnJDKClass, String> {
}

/**
* Support for making {@link BasedOnJDKClass} {@linkplain Repeatable repeatable}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE})
@Platforms(Platform.HOSTED_ONLY.class)
@interface List {
BasedOnJDKClass[] value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Predicate;

import org.graalvm.nativeimage.AnnotationAccess;
Expand Down Expand Up @@ -1043,24 +1044,30 @@ Class<?> findTargetClass(Class<?> annotatedBaseClass, TargetClass target) {
}

protected Class<?> findTargetClass(Class<?> annotatedBaseClass, TargetClass target, boolean checkOnlyWith) {
return findTargetClass(TargetClass.class, TargetClass.NoClassNameProvider.class,
annotatedBaseClass, target, target.value(), target.className(), target.classNameProvider(), target.innerClass(), checkOnlyWith ? target.onlyWith() : null);
}

protected <T> Class<?> findTargetClass(Class<T> targetClass, Class<?> noClassNameProviderClass,
Class<?> annotatedBaseClass, T target, Class<?> value, String targetClassName, Class<? extends Function<T, String>> classNameProvider, String[] innerClasses, Class<?>[] onlyWith) {

String className;
if (target.value() != TargetClass.class) {
guarantee(target.className().isEmpty(), "Both class and class name specified for substitution");
guarantee(target.classNameProvider() == TargetClass.NoClassNameProvider.class, "Both class and classNameProvider specified for substitution");
className = target.value().getName();
} else if (target.classNameProvider() != TargetClass.NoClassNameProvider.class) {
if (value != targetClass) {
guarantee(targetClassName.isEmpty(), "Both class and class name specified for substitution");
guarantee(classNameProvider == noClassNameProviderClass, "Both class and classNameProvider specified for substitution");
className = value.getName();
} else if (classNameProvider != noClassNameProviderClass) {
try {
className = ReflectionUtil.newInstance(target.classNameProvider()).apply(target);
className = ReflectionUtil.newInstance(classNameProvider).apply(target);
} catch (ReflectionUtilError ex) {
throw UserError.abort(ex.getCause(), "Cannot instantiate classNameProvider: %s. The class must have a parameterless constructor.", target.classNameProvider().getTypeName());
throw UserError.abort(ex.getCause(), "Cannot instantiate classNameProvider: %s. The class must have a parameterless constructor.", classNameProvider.getTypeName());
}
} else {
guarantee(!target.className().isEmpty(), "Neither class, className, nor classNameProvider specified for substitution");
className = target.className();
guarantee(!targetClassName.isEmpty(), "Neither class, className, nor classNameProvider specified for substitution");
className = targetClassName;
}

if (checkOnlyWith) {
for (Class<?> onlyWithClass : target.onlyWith()) {
if (onlyWith != null) {
for (Class<?> onlyWithClass : onlyWith) {
Object onlyWithProvider;
try {
onlyWithProvider = ReflectionUtil.newInstance(onlyWithClass);
Expand Down Expand Up @@ -1091,8 +1098,8 @@ protected Class<?> findTargetClass(Class<?> annotatedBaseClass, TargetClass targ
throw UserError.abort("Substitution target for %s is not loaded. Use field `onlyWith` in the `TargetClass` annotation to make substitution only active when needed.",
annotatedBaseClass.getName());
}
if (target.innerClass().length > 0) {
for (String innerClass : target.innerClass()) {
if (innerClasses.length > 0) {
for (String innerClass : innerClasses) {
Class<?> prevHolder = holder;
holder = findInnerClass(prevHolder, innerClass);
if (holder == null) {
Expand All @@ -1105,7 +1112,7 @@ protected Class<?> findTargetClass(Class<?> annotatedBaseClass, TargetClass targ
return holder;
}

private static Class<?> findInnerClass(Class<?> outerClass, String innerClassSimpleName) {
protected static Class<?> findInnerClass(Class<?> outerClass, String innerClassSimpleName) {
for (Class<?> innerClass : outerClass.getDeclaredClasses()) {
// Checkstyle: allow Class.getSimpleName
String simpleName = innerClass.getSimpleName();
Expand Down