Skip to content

Remove more unneeded AnnotationTargets and DynamicAnnotationUsage cleanup #66

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

Merged
merged 2 commits into from
Apr 19, 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.Locale;

import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AnnotationTarget;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueTypeDescriptor;
Expand All @@ -31,7 +30,6 @@ public AttributeDescriptor<V> createAttributeDescriptor(
@Override
public V createValue(
AttributeDescriptor<?> attributeDescriptor,
AnnotationTarget target,
SourceModelBuildingContext context) {
final Object defaultValue = attributeDescriptor.getAttributeMethod().getDefaultValue();
if ( defaultValue == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.hibernate.models.internal.jandex.ArrayValueWrapper;
import org.hibernate.models.internal.util.CollectionHelper;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AnnotationTarget;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueExtractor;
Expand Down Expand Up @@ -55,14 +54,15 @@ public Class<List<V>> getWrappedValueType() {
@Override
public List<V> createValue(
AttributeDescriptor<?> attributeDescriptor,
AnnotationTarget target,
SourceModelBuildingContext context) {
final Object[] defaultValue = (Object[]) attributeDescriptor.getAttributeMethod().getDefaultValue();
if ( CollectionHelper.isEmpty( defaultValue ) ) {
return Collections.emptyList();
final Object defaultValue = attributeDescriptor.getAttributeMethod().getDefaultValue();
if ( defaultValue == null ) {
// a non-defaulted attribute, just return null for the baseline
return null;
}

return jdkValueWrapper.wrap( defaultValue, context );
final ValueWrapper<List<V>, Object[]> jdkWrapper = createJdkWrapper( context );
return jdkWrapper.wrap( (Object[]) defaultValue, context );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
package org.hibernate.models.internal.dynamic;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.hibernate.models.AnnotationAccessException;
import org.hibernate.models.internal.AnnotationProxy;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.MutableAnnotationUsage;
import org.hibernate.models.spi.SourceModelContext;
import org.hibernate.models.spi.SourceModelBuildingContext;

/**
* AnnotationUsage built dynamically (for dynamic models, XML mappings, etc.)
Expand All @@ -31,7 +27,7 @@ public class DynamicAnnotationUsage<A extends Annotation> implements MutableAnno

private Map<String, Object> values;

public DynamicAnnotationUsage( AnnotationDescriptor<A> annotationDescriptor, SourceModelContext context) {
public DynamicAnnotationUsage( AnnotationDescriptor<A> annotationDescriptor, SourceModelBuildingContext context) {
this( annotationDescriptor, extractBaselineValues( annotationDescriptor, context ) );
}

Expand Down Expand Up @@ -96,59 +92,14 @@ public <V> V setAttributeValue(String name, V value) {

private static <A extends Annotation> Map<String, Object> extractBaselineValues(
AnnotationDescriptor<A> annotationDescriptor,
SourceModelContext context) {
SourceModelBuildingContext context) {
final HashMap<String, Object> values = new HashMap<>();
for ( AttributeDescriptor<?> attribute : annotationDescriptor.getAttributes() ) {
values.put(
attribute.getName(),
extractDefaultValue( attribute.getAttributeMethod().getDefaultValue(), context )
attribute.getTypeDescriptor().createValue( attribute, context )
);
}
return values;
}

private static Object extractDefaultValue(Object value, SourceModelContext context) {
if ( value != null ) {
if ( value.getClass().isArray() ) {
return extractList( value, context );
}
else if ( value instanceof Class<?> clazz ) {
return context.getClassDetailsRegistry().resolveClassDetails( clazz.getName() );
}
else if ( value instanceof Annotation annotation ) {
try {
return extractAnnotation( annotation, context );
}
catch (InvocationTargetException | IllegalAccessException e) {
throw new AnnotationAccessException( "Error accessing default annotation-typed attribute", e );
}
}
}
return value;
}

private static <E> List<Object> extractList(Object value, SourceModelContext context) {
final List<Object> result = new ArrayList<>();
//noinspection unchecked
final E[] array = (E[]) value;
for ( E element : array ) {
result.add( extractDefaultValue( element, context ) );
}
return result;
}

private static DynamicAnnotationUsage<?> extractAnnotation(Annotation annotation, SourceModelContext context)
throws InvocationTargetException, IllegalAccessException {
final Class<? extends Annotation> annotationType = annotation.annotationType();
final AnnotationDescriptor<?> descriptor = context.getAnnotationDescriptorRegistry()
.getDescriptor( annotationType );
final Map<String, Object> values = new HashMap<>();
for ( AttributeDescriptor<?> attribute : descriptor.getAttributes() ) {
values.put(
attribute.getName(),
extractDefaultValue( attribute.getAttributeMethod().invoke( annotation ), context )
);
}
return new DynamicAnnotationUsage<>( descriptor, values );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueWrapper;
Expand All @@ -32,18 +33,10 @@ public List<V> wrap(AnnotationValue rawValue, SourceModelBuildingContext buildin
final List<AnnotationValue> values = rawValue.asArrayList();
assert values != null;

if ( values.isEmpty() ) {
return Collections.emptyList();
final List<V> result = new ArrayList<>( values.size() );
for ( final AnnotationValue value : values ) {
result.add( elementWrapper.wrap( value, buildingContext ) );
}

if ( values.size() == 1 ) {
return Collections.singletonList( elementWrapper.wrap( values.get(0), buildingContext ) );
}

final List<V> results = new ArrayList<>( values.size() );
values.forEach( (value) -> {
results.add( elementWrapper.wrap( value, buildingContext ) );
} );
return results;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,9 @@ public ArrayValueWrapper(ValueWrapper<V, R> elementWrapper) {

@Override
public List<V> wrap(R[] rawValues, SourceModelBuildingContext buildingContext) {
if ( CollectionHelper.isEmpty( rawValues ) ) {
return Collections.emptyList();
}

if ( rawValues.length == 1 ) {
return Collections.singletonList( elementWrapper.wrap( rawValues[0], buildingContext ) );
}

final List<V> result = new ArrayList<>( rawValues.length );
for ( int i = 0; i < rawValues.length; i++ ) {
result.add( elementWrapper.wrap( rawValues[i], buildingContext ) );
for ( final R rawValue : rawValues ) {
result.add( elementWrapper.wrap( rawValue, buildingContext ) );
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ValueTypeDescriptor<V> {
/**
* Creates a properly typed default attribute value. Generally used in creating dynamic annotations.
*/
V createValue(AttributeDescriptor<?> attributeDescriptor, AnnotationTarget target, SourceModelBuildingContext context);
V createValue(AttributeDescriptor<?> attributeDescriptor, SourceModelBuildingContext context);

/**
* Factory for creating typed {@linkplain AttributeDescriptor} references
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
import org.hibernate.models.internal.dynamic.DynamicAnnotationUsage;
import org.hibernate.models.internal.dynamic.DynamicClassDetails;
import org.hibernate.models.orm.JpaAnnotations;
import org.hibernate.models.spi.AnnotationUsage;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.MutableAnnotationUsage;

import org.junit.jupiter.api.Test;

import jakarta.persistence.ConstraintMode;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.JoinTable;
Expand Down Expand Up @@ -69,15 +72,16 @@ void testJoinTableForeignKeyDefaultValue() {

final Object foreignKey = generatorAnn.getAttributeValue( "foreignKey" );

assertThat( foreignKey ).isInstanceOf( DynamicAnnotationUsage.class );
assertThat( foreignKey ).isInstanceOf( AnnotationUsage.class );

final DynamicAnnotationUsage foreignKeyAnnotationUsage = (DynamicAnnotationUsage) foreignKey;
//noinspection unchecked
final AnnotationUsage<ForeignKey> foreignKeyAnnotationUsage = (AnnotationUsage<ForeignKey>) foreignKey;

assertThat( foreignKeyAnnotationUsage.getAttributeValue( "value" ) ).isEqualTo( ConstraintMode.PROVIDER_DEFAULT );
assertThat( foreignKeyAnnotationUsage.<ConstraintMode>getAttributeValue( "value" ) ).isEqualTo( ConstraintMode.PROVIDER_DEFAULT );

assertThat( foreignKeyAnnotationUsage.getAttributeValue( "name" )).isEqualTo( "" );
assertThat( foreignKeyAnnotationUsage.getAttributeValue( "options" )).isEqualTo( "" );
assertThat( foreignKeyAnnotationUsage.getAttributeValue( "foreignKeyDefinition" )).isEqualTo( "" );
assertThat( foreignKeyAnnotationUsage.<String>getAttributeValue( "name" ) ).isEqualTo( "" );
assertThat( foreignKeyAnnotationUsage.<String>getAttributeValue( "options" ) ).isEqualTo( "" );
assertThat( foreignKeyAnnotationUsage.<String>getAttributeValue( "foreignKeyDefinition" ) ).isEqualTo( "" );
}

@Test
Expand Down