Skip to content

Commit

Permalink
#59 - Add safe cast methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Apr 17, 2024
1 parent 3cf203b commit a30b969
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 130 deletions.
131 changes: 1 addition & 130 deletions src/main/java/org/hibernate/models/spi/AnnotationUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,30 +76,6 @@ default <V> V getAttributeValue(String name) {
return (V) value;
}

/**
* The value of the named annotation attribute
*/
default <V> 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> V getAttributeValue(String name, Supplier<V> defaultValueSupplier) {
final Object attributeValue = getAttributeValue( name );
if ( attributeValue == null ) {
return defaultValueSupplier.get();
}
//noinspection unchecked
return (V) attributeValue;
}

default <V> V getAttributeValue(AttributeDescriptor<V> attributeDescriptor) {
return getAttributeValue( attributeDescriptor.getName() );
}
Expand All @@ -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<String> 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<Boolean> 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<Byte> 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<Short> 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<Integer> 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<Long> 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<Float> 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<Double> defaultValueSupplier) {
return getAttributeValue( name, defaultValueSupplier );
}

default <E extends Enum<E>> E getEnum(String name) {
return getAttributeValue( name );
}

default <E extends Enum<E>> E getEnum(String name, Class<E> type) {
default <E extends Enum<E>> E getEnum(String name, @SuppressWarnings("unused") Class<E> type) {
return getAttributeValue( name );
}

default <E extends Enum<E>> E getEnum(String name, E defaultValue) {
return getAttributeValue( name, defaultValue );
}

default <E extends Enum<E>> E getEnum(String name, Supplier<E> defaultValueSupplier) {
return getAttributeValue( name, defaultValueSupplier );
}

default <E extends Enum<E>> E getEnum(String name, E defaultValue, Class<E> type) {
return getAttributeValue( name, defaultValue );
}

default <E extends Enum<E>> E getEnum(String name, Supplier<E> defaultValueSupplier, Class<E> 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<ClassDetails> defaultValue) {
return getAttributeValue( name, defaultValue );
}

default <X extends Annotation> AnnotationUsage<X> getNestedUsage(String name) {
return getAttributeValue( name );
}

default <X extends Annotation> AnnotationUsage<X> getNestedUsage(String name, AnnotationUsage<X> defaultValue) {
return getAttributeValue( name, defaultValue );
}

default <X extends Annotation> AnnotationUsage<X> getNestedUsage(String name, Supplier<AnnotationUsage<X>> defaultValueSupplier) {
return getAttributeValue( name, defaultValueSupplier );
}

default <E> List<E> getList(String name) {
return getAttributeValue( name );
}

default <E> List<E> getList(String name, List<E> defaultValue) {
return getAttributeValue( name, defaultValue );
}

default <E> List<E> getList(String name, Supplier<List<E>> defaultValueSupplier) {
return getAttributeValue( name, defaultValueSupplier );
}
}
Original file line number Diff line number Diff line change
@@ -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<EverythingBagel> annotationUsage = classDetails.getAnnotationUsage( EverythingBagel.class );

assertThat( annotationUsage.<String>getAttributeValue( "theString" ) ).isEqualTo( "hello" );
assertThat( annotationUsage.<Status>getAttributeValue( "theEnum" ) ).isEqualTo( Status.ACTIVE );
assertThat( annotationUsage.<Boolean>getAttributeValue( "theBoolean" ) ).isEqualTo( true );
assertThat( annotationUsage.<Byte>getAttributeValue( "theByte" ) ).isEqualTo( (byte) 1 );
assertThat( annotationUsage.<Short>getAttributeValue( "theShort" ) ).isEqualTo( (short) 2 );
assertThat( annotationUsage.<Integer>getAttributeValue( "theInteger" ) ).isEqualTo( 3 );
assertThat( annotationUsage.<Long>getAttributeValue( "theLong" ) ).isEqualTo( 4L );
assertThat( annotationUsage.<Float>getAttributeValue( "theFloat" ) ).isEqualTo( 5.1F );
assertThat( annotationUsage.<Double>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();
}
}

1 comment on commit a30b969

@sebersole
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a fix for #64. IntelliJ's new Git dialog is just confusing

Please sign in to comment.