Skip to content

Commit dcfd1a8

Browse files
committed
HHH-19798 - Graal-VM initialization issues
Signed-off-by: Jorge Bescos Gascon <[email protected]>
1 parent 5c9da44 commit dcfd1a8

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/ByteBuddyEnhancementContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl.AnnotatedFieldDescription;
1717
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
18+
import org.hibernate.internal.util.LazyValue;
1819

1920
import jakarta.persistence.Embedded;
2021
import jakarta.persistence.metamodel.Type;
@@ -30,7 +31,8 @@
3031

3132
class ByteBuddyEnhancementContext {
3233

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

3537
private final EnhancementContext enhancementContext;
3638

@@ -170,7 +172,7 @@ Optional<MethodDescription> resolveGetter(FieldDescription fieldDescription) {
170172
getters = MethodGraph.Compiler.DEFAULT.compile( erasure )
171173
.listNodes()
172174
.asMethodList()
173-
.filter( IS_GETTER )
175+
.filter( IS_GETTER.getValue() )
174176
.stream()
175177
.collect( Collectors.toMap( MethodDescription::getActualName, Function.identity() ) );
176178
getterByTypeMap.put( erasure, getters );

hibernate-core/src/main/java/org/hibernate/internal/util/LazyValue.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.internal.util;
88

99
import java.util.Objects;
10+
import java.util.concurrent.locks.ReentrantLock;
1011
import java.util.function.Supplier;
1112

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

22+
private final ReentrantLock lock = new ReentrantLock();
2123
private final Supplier<T> supplier;
22-
private Object value;
24+
private volatile Object value;
2325

2426
public LazyValue(Supplier<T> supplier) {
2527
this.supplier = supplier;
2628
}
2729

2830
public T getValue() {
2931
if ( value == null ) {
30-
final T obtainedValue = supplier.get();
31-
value = Objects.requireNonNullElse( obtainedValue, NULL );
32+
lock.lock();
33+
try {
34+
if (value == null) {
35+
T obtainedValue = supplier.get();
36+
value = Objects.requireNonNullElse(obtainedValue, NULL);
37+
}
38+
}
39+
finally {
40+
lock.unlock();
41+
}
3242
}
3343

3444
//noinspection unchecked

hibernate-core/src/main/java/org/hibernate/proxy/pojo/bytebuddy/ByteBuddyProxyHelper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.hibernate.internal.util.ReflectHelper;
2626
import org.hibernate.proxy.HibernateProxy;
2727
import org.hibernate.proxy.ProxyConfiguration;
28+
import org.hibernate.internal.util.LazyValue;
2829

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

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

4648
private final ByteBuddyState byteBuddyState;
4749

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

0 commit comments

Comments
 (0)