Skip to content

Commit

Permalink
use reflection to use no op bytecode provider
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <thibault.vallin@oracle.com>
  • Loading branch information
tvallin committed Apr 10, 2024
1 parent 239fffe commit 201ae5d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
6 changes: 6 additions & 0 deletions integrations/cdi/hibernate-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
<artifactId>jakarta.transaction-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>svm</artifactId>
<version>24.0.0</version>
<scope>provided</scope>
</dependency>

<!-- Compile-scoped dependencies. -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.cdi.hibernate;

import java.util.Map;
import java.util.function.Predicate;

import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.property.access.spi.PropertyAccess;

@TargetClass(className = "org.hibernate.bytecode.internal.none.BytecodeProviderImpl", onlyWith = BytecodeProvider.SubstituteOnlyIfPresent.class)
final class BytecodeProvider {
@Substitute
@SuppressWarnings("NullAway")
public ReflectionOptimizer getReflectionOptimizer(Class<?> clazz, Map<String, PropertyAccess> propertyAccessMap) {
return null;
}

static class SubstituteOnlyIfPresent implements Predicate<String> {

@Override
public boolean test(String type) {
try {
Class<?> clazz = Class.forName(type, false, getClass().getClassLoader());
clazz.getDeclaredMethod("getReflectionOptimizer", Class.class, Map.class);
return true;
}
catch (ClassNotFoundException | NoClassDefFoundError | NoSuchMethodException ex) {
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.cdi.hibernate;

import java.util.function.Predicate;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.RecomputeFieldValue.Kind;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import org.hibernate.bytecode.spi.BytecodeProvider;

@TargetClass(className = "org.hibernate.bytecode.internal.BytecodeProviderInitiator", onlyWith = BytecodeProviderInitiator.SubstituteOnlyIfPresent.class)
final class BytecodeProviderInitiator {
@Alias
@SuppressWarnings("NullAway")
public static String BYTECODE_PROVIDER_NAME_NONE;

@Alias
@RecomputeFieldValue(kind = Kind.FromAlias)
public static String BYTECODE_PROVIDER_NAME_DEFAULT = BYTECODE_PROVIDER_NAME_NONE;

@Substitute
public static BytecodeProvider buildBytecodeProvider(String providerName) {
return new org.hibernate.bytecode.internal.none.BytecodeProviderImpl();
}

static class SubstituteOnlyIfPresent implements Predicate<String> {

@Override
public boolean test(String type) {
try {
Class<?> clazz = Class.forName(type, false, getClass().getClassLoader());
clazz.getDeclaredMethod("buildBytecodeProvider", String.class);
clazz.getField("BYTECODE_PROVIDER_NAME_NONE");
clazz.getField("BYTECODE_PROVIDER_NAME_DEFAULT");
return true;
}
catch (ClassNotFoundException | NoClassDefFoundError | NoSuchMethodException | NoSuchFieldException ex) {
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,7 @@
requires transitive jakarta.inject;
requires jakarta.persistence;
requires transitive jakarta.transaction;
requires org.graalvm.nativeimage;
requires transitive org.hibernate.orm.core;

requires static io.helidon.common.features.api;
Expand Down

0 comments on commit 201ae5d

Please sign in to comment.