Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#68 - Remove lambda configuration argument from AnnotationDescriptor#createUsage and DynamicClassDetails#applyAttribute #69

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,14 +176,12 @@ public DynamicFieldDetails applyAttribute(
ClassDetails type,
boolean isArray,
boolean isPlural,
Consumer<DynamicFieldDetails> configuration,
SourceModelBuildingContext context) {
return applyAttribute(
name,
new ClassTypeDetailsImpl( type, TypeDetails.Kind.CLASS ),
isArray,
isPlural,
configuration,
context
);
}
Expand All @@ -197,7 +194,6 @@ public DynamicFieldDetails applyAttribute(
TypeDetails type,
boolean isArray,
boolean isPlural,
Consumer<DynamicFieldDetails> configuration,
SourceModelBuildingContext context) {
final DynamicFieldDetails attribute = new DynamicFieldDetails(
name,
Expand All @@ -208,9 +204,6 @@ public DynamicFieldDetails applyAttribute(
isPlural,
context
);
if ( configuration != null ) {
configuration.accept( attribute );
}
addField( attribute );
return attribute;
}
Expand Down
23 changes: 1 addition & 22 deletions src/main/java/org/hibernate/models/spi/AnnotationDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,27 +95,7 @@ default <V> AttributeDescriptor<V> getAttribute(String name) {
* @param context Access to needed services
*/
default MutableAnnotationUsage<A> 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<A> createUsage(
Consumer<MutableAnnotationUsage<A>> adjuster,
SourceModelBuildingContext context) {
// create the "empty" usage
final DynamicAnnotationUsage<A> usage = new DynamicAnnotationUsage<>( this, context );

// allow configuration
if ( adjuster != null ) {
adjuster.accept( usage );
}

return usage;
return new DynamicAnnotationUsage<>( this, context );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,27 +32,12 @@ public interface MutableAnnotationTarget extends AnnotationTarget {
default <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
AnnotationDescriptor<A> 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 <A extends Annotation> MutableAnnotationUsage<A> applyAnnotationUsage(
AnnotationDescriptor<A> annotationType,
Consumer<MutableAnnotationUsage<A>> configuration,
SourceModelBuildingContext buildingContext) {
final MutableAnnotationUsage<A> existing = (MutableAnnotationUsage<A>) getAnnotationUsage( annotationType );
if ( existing != null ) {
if ( configuration != null ) {
configuration.accept( existing );
}
return existing;
}

final MutableAnnotationUsage<A> usage = annotationType.createUsage( configuration, buildingContext );
final MutableAnnotationUsage<A> usage = annotationType.createUsage( buildingContext );
addAnnotationUsage( usage );
return usage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void compositionChecks(Index index) {
@Test
void testDynamicAttributeCreation() {
final SourceModelBuildingContextImpl buildingContext = createBuildingContext( (Index) null, SimpleEntity.class );
final AnnotationUsage<Column> usage = JpaAnnotations.COLUMN.createUsage( null, buildingContext );
final AnnotationUsage<Column> usage = JpaAnnotations.COLUMN.createUsage( buildingContext );
// check the attribute defaults
assertThat( usage.getString( "name" ) ).isEqualTo( "" );
assertThat( usage.getString( "table" ) ).isEqualTo( "" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomAnnotation> created = descriptor.createUsage( null, buildingContext );
final MutableAnnotationUsage<CustomAnnotation> created = descriptor.createUsage( buildingContext );
assertThat( created.getClassDetails( "someClassValue" ) ).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,28 @@ void testSimpleBasics() {
);
assertThat( created ).isSameAs( preExisting );

entityDetails.applyAttribute(
final DynamicFieldDetails idMember = entityDetails.applyAttribute(
"id",
integerTypeDetails,
false,
false,
fieldDetails -> {
final MutableAnnotationUsage<Id> first = fieldDetails.applyAnnotationUsage(
JpaAnnotations.ID,
buildingContext
);
final MutableAnnotationUsage<Id> second = fieldDetails.applyAnnotationUsage(
JpaAnnotations.ID,
buildingContext
);
assertThat( first ).isSameAs( second );
},
buildingContext
);
final MutableAnnotationUsage<Id> first = idMember.applyAnnotationUsage(
JpaAnnotations.ID,
buildingContext
);
final MutableAnnotationUsage<Id> second = idMember.applyAnnotationUsage(
JpaAnnotations.ID,
buildingContext
);
assertThat( first ).isSameAs( second );

entityDetails.applyAttribute(
"name",
stringTypeDetails,
false,
false,
null,
buildingContext
);

Expand Down Expand Up @@ -137,7 +134,6 @@ void testSimpleEmbedded() {
stringClassDetails,
false,
false,
null,
buildingContext
);

Expand All @@ -146,7 +142,6 @@ void testSimpleEmbedded() {
stringClassDetails,
false,
false,
null,
buildingContext
);

Expand All @@ -169,24 +164,20 @@ void testSimpleEmbedded() {
integerTypeDetails,
false,
false,
(fieldDetails) -> {
final MutableAnnotationUsage<Id> idUsage = fieldDetails.applyAnnotationUsage( JpaAnnotations.ID, buildingContext );
assertThat( idUsage ).isNotNull();
},
buildingContext
);
final MutableAnnotationUsage<Id> 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<Embedded> embeddedUsage = fieldDetails.applyAnnotationUsage( JpaAnnotations.EMBEDDED, buildingContext );
assertThat( embeddedUsage ).isNotNull();
},
buildingContext
);
final MutableAnnotationUsage<Embedded> embeddedUsage = nameMember.applyAnnotationUsage( JpaAnnotations.EMBEDDED, buildingContext );
assertThat( embeddedUsage ).isNotNull();


// ASSERTIONS
Expand Down
Loading