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"); }