From 00aad06520bda91d0a49a266c3d91996f623b35b Mon Sep 17 00:00:00 2001 From: Cedomir Igaly Date: Tue, 14 May 2024 17:31:40 +0200 Subject: [PATCH] HHH-18106 - Implemented Java constant field resolution for MockSessionFactory --- .../validation/MockSessionFactory.java | 16 ++++++- .../validation/ProcessorSessionFactory.java | 48 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java index e912db9959..b9870ce93f 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java @@ -119,7 +119,6 @@ import org.hibernate.type.spi.TypeConfiguration; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; import java.util.UUID; @@ -291,6 +290,8 @@ public abstract class MockSessionFactory abstract boolean isEnumConstant(String className, String terminal); + abstract Class javaConstantType(String className, String fieldName); + abstract boolean isFieldDefined(String qualifiedClassName, String fieldName); abstract boolean isConstructorDefined(String qualifiedClassName, List 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 getJavaConstant(String className, String fieldName) { + return null; + } + @Override public > E enumValue(EnumJavaType enumType, String enumValueName) { if ( !isEnumConstant( enumType.getTypeName(), enumValueName ) ) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java index c7557aaf98..d10906f615 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java @@ -607,6 +607,54 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory { .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) { return hasAnnotation(type, "Embeddable"); }