Skip to content
Open
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 @@ -15,6 +15,7 @@

import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl.AnnotatedFieldDescription;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.internal.util.LazyValue;

import jakarta.persistence.Embedded;
import jakarta.persistence.metamodel.Type;
Expand All @@ -30,7 +31,8 @@

class ByteBuddyEnhancementContext {

private static final ElementMatcher.Junction<MethodDescription> IS_GETTER = isGetter();
// Lazy is necessary to not break GraalVM Native Image
private static final LazyValue<ElementMatcher.Junction<MethodDescription>> IS_GETTER = new LazyValue<>(() -> isGetter());

private final EnhancementContext enhancementContext;

Expand Down Expand Up @@ -170,7 +172,7 @@ Optional<MethodDescription> resolveGetter(FieldDescription fieldDescription) {
getters = MethodGraph.Compiler.DEFAULT.compile( erasure )
.listNodes()
.asMethodList()
.filter( IS_GETTER )
.filter( IS_GETTER.getValue() )
.stream()
.collect( Collectors.toMap( MethodDescription::getActualName, Function.identity() ) );
getterByTypeMap.put( erasure, getters );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.internal.util;

import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

/**
Expand All @@ -18,17 +19,26 @@
public class LazyValue<T> {
public static final Object NULL = new Object();

private final ReentrantLock lock = new ReentrantLock();
private final Supplier<T> supplier;
private Object value;
private volatile Object value;

public LazyValue(Supplier<T> supplier) {
this.supplier = supplier;
}

public T getValue() {
if ( value == null ) {
final T obtainedValue = supplier.get();
value = Objects.requireNonNullElse( obtainedValue, NULL );
lock.lock();
try {
if (value == null) {
T obtainedValue = supplier.get();
value = Objects.requireNonNullElse(obtainedValue, NULL);
}
}
finally {
lock.unlock();
}
}

//noinspection unchecked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.ProxyConfiguration;
import org.hibernate.internal.util.LazyValue;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.NamingStrategy;
Expand All @@ -41,7 +42,8 @@ public class ByteBuddyProxyHelper implements Serializable {

private static final CoreMessageLogger LOG = messageLogger( ByteBuddyProxyHelper.class );
private static final String PROXY_NAMING_SUFFIX = "HibernateProxy";
private static final TypeDescription OBJECT = TypeDescription.ForLoadedType.of(Object.class);
// Lazy is necessary to not break GraalVM Native Image
private static final LazyValue<TypeDescription> OBJECT = new LazyValue<>(() -> TypeDescription.ForLoadedType.of(Object.class));

private final ByteBuddyState byteBuddyState;

Expand Down Expand Up @@ -96,7 +98,7 @@ private BiFunction<ByteBuddy, NamingStrategy, DynamicType.Builder<?>> proxyBuild
return (byteBuddy, namingStrategy) -> helpers.appendIgnoreAlsoAtEnd( byteBuddy
.ignore( helpers.getGroovyGetMetaClassFilter() )
.with( namingStrategy )
.subclass( interfaces.size() == 1 ? persistentClass : OBJECT, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING )
.subclass( interfaces.size() == 1 ? persistentClass : OBJECT.getValue(), ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING )
.implement( interfaces )
.method( helpers.getVirtualNotFinalizerFilter() )
.intercept( helpers.getDelegateToInterceptorDispatcherMethodDelegation() )
Expand Down