From f9afd3dcb75cbc4df1e9106c7b13a7d45e1e40ca Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 30 Sep 2022 20:27:44 +0200 Subject: [PATCH] HHH-13439 Encode Hibernate ORM version into build time enhanced entities --- .../internal/bytebuddy/EnhancerImpl.java | 19 ++++++++++++++ .../bytecode/enhance/spi/EnhancementInfo.java | 25 +++++++++++++++++++ .../basic/BasicEnhancementTest.java | 14 +++++++++++ 3 files changed, 58 insertions(+) create mode 100644 hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/EnhancementInfo.java diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java index 934878f0d0..2aedd227dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java @@ -22,11 +22,13 @@ import jakarta.persistence.Access; import jakarta.persistence.AccessType; import jakarta.persistence.Transient; +import org.hibernate.Version; import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker; import org.hibernate.bytecode.enhance.internal.tracker.DirtyTracker; import org.hibernate.bytecode.enhance.spi.CollectionTracker; import org.hibernate.bytecode.enhance.spi.EnhancementContext; import org.hibernate.bytecode.enhance.spi.EnhancementException; +import org.hibernate.bytecode.enhance.spi.EnhancementInfo; import org.hibernate.bytecode.enhance.spi.Enhancer; import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.bytecode.enhance.spi.UnloadedField; @@ -68,6 +70,21 @@ import net.bytebuddy.pool.TypePool; public class EnhancerImpl implements Enhancer { private static final CoreMessageLogger log = CoreLogging.messageLogger( Enhancer.class ); + private static final Annotation HIBERNATE_VERSION_ANNOTATION; + + static { + HIBERNATE_VERSION_ANNOTATION = new EnhancementInfo() { + @Override + public String version() { + return Version.getVersionString(); + } + + @Override + public Class annotationType() { + return EnhancementInfo.class; + } + }; + } protected final ByteBuddyEnhancementContext enhancementContext; private final ByteBuddyState byteBuddyState; @@ -160,6 +177,8 @@ public class EnhancerImpl implements Enhancer { return null; } + builder = builder.annotateType( HIBERNATE_VERSION_ANNOTATION ); + if ( enhancementContext.isEntityClass( managedCtClass ) ) { log.debugf( "Enhancing [%s] as Entity", managedCtClass.getName() ); builder = builder.implement( ManagedEntity.class ) diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/EnhancementInfo.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/EnhancementInfo.java new file mode 100644 index 0000000000..7875a5f451 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/EnhancementInfo.java @@ -0,0 +1,25 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.bytecode.enhance.spi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Provides basic information about the enhancement done to a class. + */ +@Target( ElementType.TYPE ) +@Retention( RetentionPolicy.RUNTIME ) +public @interface EnhancementInfo { + + /** + * The Hibernate version used for enhancement. + */ + String version(); +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java index 81189cce1f..b5fc8c19ff 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java @@ -6,9 +6,13 @@ */ package org.hibernate.orm.test.bytecode.enhancement.basic; +import org.hibernate.Version; +import org.hibernate.bytecode.enhance.spi.EnhancementInfo; import org.hibernate.engine.spi.ManagedEntity; import org.hibernate.engine.spi.PersistentAttributeInterceptable; +import org.hibernate.orm.test.legacy.Simple; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; import org.junit.Assert; @@ -23,6 +27,7 @@ import java.util.List; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; @@ -56,6 +61,15 @@ public class BasicEnhancementTest { assertSame( managedEntity, managedEntity.$$_hibernate_getPreviousManagedEntity() ); } + @Test + @TestForIssue(jiraKey = "HHH-13439") + public void enhancementInfoTest() { + EnhancementInfo info = SimpleEntity.class.getAnnotation( EnhancementInfo.class ); + assertNotNull( "EnhancementInfo was not applied", info ); + + assertEquals( Version.getVersionString(), info.version() ); + } + @Test public void basicInterceptableTest() { SimpleEntity entity = new SimpleEntity();