diff --git a/src/main/java/org/hibernate/models/spi/AnnotationUsage.java b/src/main/java/org/hibernate/models/spi/AnnotationUsage.java index 8844077..750505c 100644 --- a/src/main/java/org/hibernate/models/spi/AnnotationUsage.java +++ b/src/main/java/org/hibernate/models/spi/AnnotationUsage.java @@ -8,7 +8,6 @@ import java.lang.annotation.Annotation; import java.util.List; -import java.util.function.Supplier; /** * Describes the usage of an {@linkplain AnnotationDescriptor annotation class} on one of its @@ -77,30 +76,6 @@ default V getAttributeValue(String name) { return (V) value; } - /** - * The value of the named annotation attribute - */ - default V getAttributeValue(String name, V defaultValue) { - final Object attributeValue = getAttributeValue( name ); - if ( attributeValue == null ) { - return defaultValue; - } - //noinspection unchecked - return (V) attributeValue; - } - - /** - * The value of the named annotation attribute - */ - default V getAttributeValue(String name, Supplier defaultValueSupplier) { - final Object attributeValue = getAttributeValue( name ); - if ( attributeValue == null ) { - return defaultValueSupplier.get(); - } - //noinspection unchecked - return (V) attributeValue; - } - default V getAttributeValue(AttributeDescriptor attributeDescriptor) { return getAttributeValue( attributeDescriptor.getName() ); } @@ -109,155 +84,51 @@ default String getString(String name) { return getAttributeValue( name ); } - default String getString(String name, String defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default String getString(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default Boolean getBoolean(String name) { return getAttributeValue( name ); } - default Boolean getBoolean(String name, boolean defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default Boolean getBoolean(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default Byte getByte(String name) { return getAttributeValue( name ); } - default Byte getByte(String name, Byte defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default Byte getByte(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default Short getShort(String name) { return getAttributeValue( name ); } - default Short getShort(String name, Short defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default Short getShort(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default Integer getInteger(String name) { return getAttributeValue( name ); } - default Integer getInteger(String name, Integer defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default Integer getInteger(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default Long getLong(String name) { return getAttributeValue( name ); } - default Long getLong(String name, Long defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default Long getLong(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default Float getFloat(String name) { return getAttributeValue( name ); } - default Float getFloat(String name, Float defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default Float getFloat(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default Double getDouble(String name) { return getAttributeValue( name ); } - default Double getDouble(String name, Double defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default Double getDouble(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default > E getEnum(String name) { return getAttributeValue( name ); } - default > E getEnum(String name, Class type) { + default > E getEnum(String name, @SuppressWarnings("unused") Class type) { return getAttributeValue( name ); } - default > E getEnum(String name, E defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default > E getEnum(String name, Supplier defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - - default > E getEnum(String name, E defaultValue, Class type) { - return getAttributeValue( name, defaultValue ); - } - - default > E getEnum(String name, Supplier defaultValueSupplier, Class type) { - return getAttributeValue( name, defaultValueSupplier ); - } - default ClassDetails getClassDetails(String name) { return getAttributeValue( name ); } - default ClassDetails getClassDetails(String name, ClassDetails defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default ClassDetails getClassDetails(String name, Supplier defaultValue) { - return getAttributeValue( name, defaultValue ); - } - default AnnotationUsage getNestedUsage(String name) { return getAttributeValue( name ); } - default AnnotationUsage getNestedUsage(String name, AnnotationUsage defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default AnnotationUsage getNestedUsage(String name, Supplier> defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } - default List getList(String name) { return getAttributeValue( name ); } - - default List getList(String name, List defaultValue) { - return getAttributeValue( name, defaultValue ); - } - - default List getList(String name, Supplier> defaultValueSupplier) { - return getAttributeValue( name, defaultValueSupplier ); - } } diff --git a/src/test/java/org/hibernate/models/annotations/AttributeAccessTests.java b/src/test/java/org/hibernate/models/annotations/AttributeAccessTests.java new file mode 100644 index 0000000..1dd0681 --- /dev/null +++ b/src/test/java/org/hibernate/models/annotations/AttributeAccessTests.java @@ -0,0 +1,103 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright: Red Hat Inc. and Hibernate Authors + */ + +package org.hibernate.models.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.hibernate.models.internal.SourceModelBuildingContextImpl; +import org.hibernate.models.spi.AnnotationUsage; +import org.hibernate.models.spi.ClassDetails; + +import org.junit.jupiter.api.Test; + +import org.jboss.jandex.Index; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.models.SourceModelTestHelper.createBuildingContext; + +/** + * @author Steve Ebersole + */ +public class AttributeAccessTests { + @Test + void testAttributeAccess() { + final SourceModelBuildingContextImpl buildingContext = createBuildingContext( (Index) null, TheClass.class ); + final ClassDetails classDetails = buildingContext.getClassDetailsRegistry().getClassDetails( TheClass.class.getName() ); + final AnnotationUsage annotationUsage = classDetails.getAnnotationUsage( EverythingBagel.class ); + + assertThat( annotationUsage.getAttributeValue( "theString" ) ).isEqualTo( "hello" ); + assertThat( annotationUsage.getAttributeValue( "theEnum" ) ).isEqualTo( Status.ACTIVE ); + assertThat( annotationUsage.getAttributeValue( "theBoolean" ) ).isEqualTo( true ); + assertThat( annotationUsage.getAttributeValue( "theByte" ) ).isEqualTo( (byte) 1 ); + assertThat( annotationUsage.getAttributeValue( "theShort" ) ).isEqualTo( (short) 2 ); + assertThat( annotationUsage.getAttributeValue( "theInteger" ) ).isEqualTo( 3 ); + assertThat( annotationUsage.getAttributeValue( "theLong" ) ).isEqualTo( 4L ); + assertThat( annotationUsage.getAttributeValue( "theFloat" ) ).isEqualTo( 5.1F ); + assertThat( annotationUsage.getAttributeValue( "theDouble" ) ).isEqualTo( 6.2 ); + + assertThat( annotationUsage.getString( "theString" ) ).isEqualTo( "hello" ); + assertThat( annotationUsage.getEnum( "theEnum" ).name() ).isEqualTo( Status.ACTIVE.name() ); + assertThat( annotationUsage.getEnum( "theEnum", Status.class ) ).isEqualTo( Status.ACTIVE ); + assertThat( annotationUsage.getBoolean( "theBoolean" ) ).isEqualTo( true ); + assertThat( annotationUsage.getByte( "theByte" ) ).isEqualTo( (byte) 1 ); + assertThat( annotationUsage.getShort( "theShort" ) ).isEqualTo( (short) 2 ); + assertThat( annotationUsage.getInteger( "theInteger" ) ).isEqualTo( 3 ); + assertThat( annotationUsage.getLong( "theLong" ) ).isEqualTo( 4L ); + assertThat( annotationUsage.getFloat( "theFloat" ) ).isEqualTo( 5.1F ); + assertThat( annotationUsage.getDouble( "theDouble" ) ).isEqualTo( 6.2 ); + + assertThat( annotationUsage.getClassDetails( "theClass" ).toJavaClass() ).isEqualTo( TheClass.class ); + assertThat( annotationUsage.getNestedUsage( "theNested" ) ).isNotNull(); + assertThat( annotationUsage.getList( "theStrings" ) ).containsExactly( "a", "b", "c" ); + } + + @EverythingBagel( + theString = "hello", + theEnum = Status.ACTIVE, + theBoolean = true, + theByte = 1, + theShort = 2, + theInteger = 3, + theLong = 4L, + theFloat = 5.1F, + theDouble = 6.2, + theClass = TheClass.class, + theNested = @Nested(), + theStrings = {"a", "b", "c"} + ) + public static class TheClass { + + } + + public enum Status { ACTIVE } + + @Target(ElementType.TYPE) + @Retention(RetentionPolicy.RUNTIME) + public @interface Nested { + } + + @Target(ElementType.TYPE) + @Retention(RetentionPolicy.RUNTIME) + public @interface EverythingBagel { + String theString(); + Status theEnum(); + boolean theBoolean(); + byte theByte(); + short theShort(); + int theInteger(); + long theLong(); + float theFloat(); + double theDouble(); + Class theClass(); + Nested theNested(); + String[] theStrings(); + } +}