HHH-18106 - Implemented Java constant field resolution for MockSessionFactory

This commit is contained in:
Cedomir Igaly 2024-05-14 17:31:40 +02:00 committed by Gavin King
parent d738e75662
commit 00aad06520
2 changed files with 63 additions and 1 deletions

View File

@ -119,7 +119,6 @@ import org.hibernate.type.spi.TypeConfiguration;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -291,6 +290,8 @@ public abstract class MockSessionFactory
abstract boolean isEnumConstant(String className, String terminal); abstract boolean isEnumConstant(String className, String terminal);
abstract Class<?> javaConstantType(String className, String fieldName);
abstract boolean isFieldDefined(String qualifiedClassName, String fieldName); abstract boolean isFieldDefined(String qualifiedClassName, String fieldName);
abstract boolean isConstructorDefined(String qualifiedClassName, List<Type> argumentTypes); abstract boolean isConstructorDefined(String qualifiedClassName, List<Type> argumentTypes);
@ -846,6 +847,19 @@ public abstract class MockSessionFactory
} }
} }
@Override
public JavaType<?> getJavaConstantType(String className, String fieldName) {
final Class<?> fieldType = javaConstantType( className, fieldName );
return MockSessionFactory.this.getTypeConfiguration()
.getJavaTypeRegistry()
.getDescriptor( fieldType );
}
@Override
public <T> T getJavaConstant(String className, String fieldName) {
return null;
}
@Override @Override
public <E extends Enum<E>> E enumValue(EnumJavaType<E> enumType, String enumValueName) { public <E extends Enum<E>> E enumValue(EnumJavaType<E> enumType, String enumValueName) {
if ( !isEnumConstant( enumType.getTypeName(), enumValueName ) ) { if ( !isEnumConstant( enumType.getTypeName(), enumValueName ) ) {

View File

@ -607,6 +607,54 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
.anyMatch(e -> e.getKind() == ElementKind.ENUM_CONSTANT); .anyMatch(e -> e.getKind() == ElementKind.ENUM_CONSTANT);
} }
@Override
Class<?> javaConstantType(String className, String fieldName) {
final TypeElement typeElement = elementUtil.getTypeElement( className );
if ( typeElement == null ) {
return null;
}
final TypeMirror typeMirror = typeElement.getEnclosedElements()
.stream()
.filter( e -> fieldName.equals( e.getSimpleName().toString() ) )
.filter( ProcessorSessionFactory::isStaticFinalField )
.findFirst().map( Element::asType )
.orElse( null );
if ( typeMirror == null ) {
return null;
}
try {
switch ( typeMirror.getKind() ) {
case BYTE:
return byte.class;
case SHORT:
return short.class;
case INT:
return int.class;
case LONG:
return long.class;
case FLOAT:
return float.class;
case DOUBLE:
return double.class;
case BOOLEAN:
return boolean.class;
case CHAR:
return char.class;
default:
return Class.forName( typeMirror.toString() );
}
}
catch (ClassNotFoundException ignored) {
return null;
}
}
private static boolean isStaticFinalField(Element e) {
return e.getKind() == ElementKind.FIELD
&& e.getModifiers().contains( Modifier.STATIC )
&& e.getModifiers().contains( Modifier.FINAL );
}
private static boolean isEmbeddableType(TypeElement type) { private static boolean isEmbeddableType(TypeElement type) {
return hasAnnotation(type, "Embeddable"); return hasAnnotation(type, "Embeddable");
} }