From a3b7516c5e2b2b69121c21494a2331b24e2d4ded Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 24 Apr 2024 11:13:19 +0200 Subject: [PATCH] #68 - Remove lambda configuration argument from `AnnotationDescriptor#createUsage` and `DynamicClassDetails#applyAttribute` --- .../internal/dynamic/DynamicClassDetails.java | 7 ---- .../models/spi/AnnotationDescriptor.java | 23 +----------- .../models/spi/MutableAnnotationTarget.java | 18 +-------- .../annotations/AnnotationUsageTests.java | 2 +- .../models/annotations/DefaultValueTests.java | 2 +- .../dynamic/SimpleDynamicModelTests.java | 37 +++++++------------ 6 files changed, 18 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/hibernate/models/internal/dynamic/DynamicClassDetails.java b/src/main/java/org/hibernate/models/internal/dynamic/DynamicClassDetails.java index 73a2cad..33101f7 100644 --- a/src/main/java/org/hibernate/models/internal/dynamic/DynamicClassDetails.java +++ b/src/main/java/org/hibernate/models/internal/dynamic/DynamicClassDetails.java @@ -9,7 +9,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.function.Consumer; import org.hibernate.models.internal.ClassDetailsSupport; import org.hibernate.models.internal.ClassTypeDetailsImpl; @@ -177,14 +176,12 @@ public DynamicFieldDetails applyAttribute( ClassDetails type, boolean isArray, boolean isPlural, - Consumer configuration, SourceModelBuildingContext context) { return applyAttribute( name, new ClassTypeDetailsImpl( type, TypeDetails.Kind.CLASS ), isArray, isPlural, - configuration, context ); } @@ -197,7 +194,6 @@ public DynamicFieldDetails applyAttribute( TypeDetails type, boolean isArray, boolean isPlural, - Consumer configuration, SourceModelBuildingContext context) { final DynamicFieldDetails attribute = new DynamicFieldDetails( name, @@ -208,9 +204,6 @@ public DynamicFieldDetails applyAttribute( isPlural, context ); - if ( configuration != null ) { - configuration.accept( attribute ); - } addField( attribute ); return attribute; } diff --git a/src/main/java/org/hibernate/models/spi/AnnotationDescriptor.java b/src/main/java/org/hibernate/models/spi/AnnotationDescriptor.java index 1aa8f87..8fa78d4 100644 --- a/src/main/java/org/hibernate/models/spi/AnnotationDescriptor.java +++ b/src/main/java/org/hibernate/models/spi/AnnotationDescriptor.java @@ -10,7 +10,6 @@ import java.lang.annotation.Repeatable; import java.util.EnumSet; import java.util.List; -import java.util.function.Consumer; import org.hibernate.models.IllegalCastException; import org.hibernate.models.UnknownAnnotationAttributeException; @@ -96,27 +95,7 @@ default AttributeDescriptor getAttribute(String name) { * @param context Access to needed services */ default MutableAnnotationUsage createUsage(SourceModelBuildingContext context) { - return createUsage( null, context ); - } - - /** - * Create a usage of this annotation with all attribute values defaulted, allowing customization prior to return. - * - * @param adjuster Callback to allow adjusting the created usage prior to return. - * @param context Access to needed services - */ - default MutableAnnotationUsage createUsage( - Consumer> adjuster, - SourceModelBuildingContext context) { - // create the "empty" usage - final DynamicAnnotationUsage usage = new DynamicAnnotationUsage<>( this, context ); - - // allow configuration - if ( adjuster != null ) { - adjuster.accept( usage ); - } - - return usage; + return new DynamicAnnotationUsage<>( this, context ); } @Override diff --git a/src/main/java/org/hibernate/models/spi/MutableAnnotationTarget.java b/src/main/java/org/hibernate/models/spi/MutableAnnotationTarget.java index 5d03e33..97af447 100644 --- a/src/main/java/org/hibernate/models/spi/MutableAnnotationTarget.java +++ b/src/main/java/org/hibernate/models/spi/MutableAnnotationTarget.java @@ -7,7 +7,6 @@ package org.hibernate.models.spi; import java.lang.annotation.Annotation; -import java.util.function.Consumer; /** * Extension of AnnotationTarget which allows manipulation of the annotations @@ -33,27 +32,12 @@ public interface MutableAnnotationTarget extends AnnotationTarget { default MutableAnnotationUsage applyAnnotationUsage( AnnotationDescriptor annotationType, SourceModelBuildingContext buildingContext) { - return applyAnnotationUsage( annotationType, null, buildingContext ); - } - - /** - * Applies a usage of the given {@code annotationType} to this target, allowing - * for configuration of the applied usage. Will return an existing usage, if one, - * or create a new usage. - */ - default MutableAnnotationUsage applyAnnotationUsage( - AnnotationDescriptor annotationType, - Consumer> configuration, - SourceModelBuildingContext buildingContext) { final MutableAnnotationUsage existing = (MutableAnnotationUsage) getAnnotationUsage( annotationType ); if ( existing != null ) { - if ( configuration != null ) { - configuration.accept( existing ); - } return existing; } - final MutableAnnotationUsage usage = annotationType.createUsage( configuration, buildingContext ); + final MutableAnnotationUsage usage = annotationType.createUsage( buildingContext ); addAnnotationUsage( usage ); return usage; } diff --git a/src/test/java/org/hibernate/models/annotations/AnnotationUsageTests.java b/src/test/java/org/hibernate/models/annotations/AnnotationUsageTests.java index 7815907..952b12b 100644 --- a/src/test/java/org/hibernate/models/annotations/AnnotationUsageTests.java +++ b/src/test/java/org/hibernate/models/annotations/AnnotationUsageTests.java @@ -158,7 +158,7 @@ private void compositionChecks(Index index) { @Test void testDynamicAttributeCreation() { final SourceModelBuildingContextImpl buildingContext = createBuildingContext( (Index) null, SimpleEntity.class ); - final AnnotationUsage usage = JpaAnnotations.COLUMN.createUsage( null, buildingContext ); + final AnnotationUsage usage = JpaAnnotations.COLUMN.createUsage( buildingContext ); // check the attribute defaults assertThat( usage.getString( "name" ) ).isEqualTo( "" ); assertThat( usage.getString( "table" ) ).isEqualTo( "" ); diff --git a/src/test/java/org/hibernate/models/annotations/DefaultValueTests.java b/src/test/java/org/hibernate/models/annotations/DefaultValueTests.java index ae89a63..caa15fc 100644 --- a/src/test/java/org/hibernate/models/annotations/DefaultValueTests.java +++ b/src/test/java/org/hibernate/models/annotations/DefaultValueTests.java @@ -69,7 +69,7 @@ private void doTest(Index index) { assertThat( annotationUsage.getClassDetails( "someClassValue" ).toJavaClass() ).isEqualTo( Entity.class ); // apparently the models code does not use this method directly, but ORM does. This is the one that was leading to NPE - final MutableAnnotationUsage created = descriptor.createUsage( null, buildingContext ); + final MutableAnnotationUsage created = descriptor.createUsage( buildingContext ); assertThat( created.getClassDetails( "someClassValue" ) ).isNull(); } diff --git a/src/test/java/org/hibernate/models/dynamic/SimpleDynamicModelTests.java b/src/test/java/org/hibernate/models/dynamic/SimpleDynamicModelTests.java index f9dd27b..591f92a 100644 --- a/src/test/java/org/hibernate/models/dynamic/SimpleDynamicModelTests.java +++ b/src/test/java/org/hibernate/models/dynamic/SimpleDynamicModelTests.java @@ -60,31 +60,28 @@ void testSimpleBasics() { ); assertThat( created ).isSameAs( preExisting ); - entityDetails.applyAttribute( + final DynamicFieldDetails idMember = entityDetails.applyAttribute( "id", integerTypeDetails, false, false, - fieldDetails -> { - final MutableAnnotationUsage first = fieldDetails.applyAnnotationUsage( - JpaAnnotations.ID, - buildingContext - ); - final MutableAnnotationUsage second = fieldDetails.applyAnnotationUsage( - JpaAnnotations.ID, - buildingContext - ); - assertThat( first ).isSameAs( second ); - }, buildingContext ); + final MutableAnnotationUsage first = idMember.applyAnnotationUsage( + JpaAnnotations.ID, + buildingContext + ); + final MutableAnnotationUsage second = idMember.applyAnnotationUsage( + JpaAnnotations.ID, + buildingContext + ); + assertThat( first ).isSameAs( second ); entityDetails.applyAttribute( "name", stringTypeDetails, false, false, - null, buildingContext ); @@ -137,7 +134,6 @@ void testSimpleEmbedded() { stringClassDetails, false, false, - null, buildingContext ); @@ -146,7 +142,6 @@ void testSimpleEmbedded() { stringClassDetails, false, false, - null, buildingContext ); @@ -169,24 +164,20 @@ void testSimpleEmbedded() { integerTypeDetails, false, false, - (fieldDetails) -> { - final MutableAnnotationUsage idUsage = fieldDetails.applyAnnotationUsage( JpaAnnotations.ID, buildingContext ); - assertThat( idUsage ).isNotNull(); - }, buildingContext ); + final MutableAnnotationUsage idUsage = idMember.applyAnnotationUsage( JpaAnnotations.ID, buildingContext ); + assertThat( idUsage ).isNotNull(); final DynamicFieldDetails nameMember = entityDetails.applyAttribute( "name", new ClassTypeDetailsImpl( nameEmbeddableDetails, TypeDetails.Kind.CLASS ), false, false, - (fieldDetails) -> { - final MutableAnnotationUsage embeddedUsage = fieldDetails.applyAnnotationUsage( JpaAnnotations.EMBEDDED, buildingContext ); - assertThat( embeddedUsage ).isNotNull(); - }, buildingContext ); + final MutableAnnotationUsage embeddedUsage = nameMember.applyAnnotationUsage( JpaAnnotations.EMBEDDED, buildingContext ); + assertThat( embeddedUsage ).isNotNull(); // ASSERTIONS