HHH-12011 - Fix metamodel generation with TYPE_USE targeted annotations.

(cherry picked from commit 67096ce)
This commit is contained in:
Chris Cranford 2018-05-04 14:57:29 -04:00
parent e3b29c6f2d
commit 97f715636f
1 changed files with 35 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeKind;
@ -60,6 +61,40 @@ public final class TypeUtils {
}
public static String toTypeString(TypeMirror type) {
// Fix for HHH-12011
if ( type.getKind() == TypeKind.ARRAY ) {
// IMPL NOTE:
//
// With the introduction of TYPE_USE, this method would return any annotated array where
// the annotation was TYPE_USE as "(@package.to.TheAnnotation :: datatype)". We expected
// that we would return "datatype[]" instead.
//
// We check if we detect the "(... :: datatype)" pattern and if so, we extract the datatype
// manually and construct the expected outcome but only if annotations are present.
//
// This fix makes sure that for annotated primitive arrays that we return the datatype
// exactly as it appears in the java class, just like non-annotated primitive arrays.
//
// I do question this behavior as it illustrates we handle primitive array types differently
// that primitive non-array types. For example, primitive non-arrays are returned as their
// autoboxed object types where primitive arrays are returned as-is.
//
// In short this means that a field of type "byte" is generated as "Byte" in the metamodel
// generated class but a "byte[]" is generated as "byte[]" rather than "Byte[]".
//
final TypeMirror typeMirror = ( (ArrayType) type ).getComponentType();
final List<? extends AnnotationMirror> annotationMirrors = typeMirror.getAnnotationMirrors();
if ( !annotationMirrors.isEmpty() ) {
String typeName = typeMirror.toString();
if ( typeName.startsWith( "(" ) && typeName.endsWith( ")" ) ) {
int seperator = typeName.indexOf( ":: " );
if ( seperator != -1 ) {
return typeName.substring( seperator + 3, typeName.length() - 1 ) + "[]";
}
}
}
}
if ( type.getKind().isPrimitive() ) {
return PRIMITIVES.get( type.toString() );
}