Skip to content

Commit 63a20be

Browse files
committed
Optimize Hibernate native footprint by making ByteBuddy unreachable
As a workaround before a proper solution the Spring team is trying to find with the Hibernate team in a future version of Hibernate, this commit introduces 2 GraalVM substitutions that should allow to remove ByteBuddy reachability with Hibernate 6.3. Closes gh-29549
1 parent acd34df commit 63a20be

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.jpa.vendor;
18+
19+
import java.util.Map;
20+
21+
import com.oracle.svm.core.annotate.Substitute;
22+
import com.oracle.svm.core.annotate.TargetClass;
23+
import org.hibernate.bytecode.spi.ReflectionOptimizer;
24+
import org.hibernate.property.access.spi.PropertyAccess;
25+
26+
/**
27+
* Hibernate 6.3+ substitution designed to leniently return {@code null}, as authorized by the API, to avoid throwing an
28+
* {@code HibernateException}.
29+
* TODO Ask Hibernate team to fix this as it looks like a bug
30+
*
31+
* @author Sebastien Deleuze
32+
* @since 6.1
33+
*/
34+
@TargetClass(className = "org.hibernate.bytecode.internal.none.BytecodeProviderImpl")
35+
final class Target_BytecodeProvider {
36+
37+
@Substitute
38+
public ReflectionOptimizer getReflectionOptimizer(Class<?> clazz, Map<String, PropertyAccess> propertyAccessMap) {
39+
return null;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.jpa.vendor;
18+
19+
import com.oracle.svm.core.annotate.Alias;
20+
import com.oracle.svm.core.annotate.RecomputeFieldValue;
21+
import com.oracle.svm.core.annotate.Substitute;
22+
import com.oracle.svm.core.annotate.TargetClass;
23+
import org.hibernate.bytecode.spi.BytecodeProvider;
24+
25+
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind;
26+
27+
/**
28+
* Hibernate substitution designed to prevent ByteBuddy reachability on native, and to enforce the
29+
* usage of {@code org.hibernate.bytecode.internal.none.BytecodeProviderImpl} with Hibernate 6.3+.
30+
* TODO Collaborate with Hibernate team on a substitution-less alternative that does not require a custom list of StandardServiceInitiator
31+
*
32+
* @author Sebastien Deleuze
33+
* @since 6.1
34+
*/
35+
@TargetClass(className = "org.hibernate.bytecode.internal.BytecodeProviderInitiator")
36+
final class Target_BytecodeProviderInitiator {
37+
38+
@Alias
39+
public static String BYTECODE_PROVIDER_NAME_NONE;
40+
41+
@Alias
42+
@RecomputeFieldValue(kind = Kind.FromAlias)
43+
public static String BYTECODE_PROVIDER_NAME_DEFAULT = BYTECODE_PROVIDER_NAME_NONE;
44+
45+
@Substitute
46+
public static BytecodeProvider buildBytecodeProvider(String providerName) {
47+
return new org.hibernate.bytecode.internal.none.BytecodeProviderImpl();
48+
}
49+
}

src/checkstyle/checkstyle-suppressions.xml

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484

8585
<!-- spring-orm -->
8686
<suppress files="jpa[\\/]vendor[\\/]Database" checks="JavadocVariable|JavadocStyle"/>
87+
<suppress files="Target_BytecodeProviderInitiator" checks="HideUtilityClassConstructorCheck"/>
8788

8889
<!-- spring-tx -->
8990
<suppress files="TransactionSystemException" checks="MutableException"/>

0 commit comments

Comments
 (0)