Skip to content

Commit

Permalink
HHH-12011 - Use TypeVisitor rather than string parsing for array types.
Browse files Browse the repository at this point in the history
(cherry picked from commit 2ad6bf6)
  • Loading branch information
Naros committed May 11, 2018
1 parent 97f7156 commit 4ebfd43
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public AnnotationMetaAttribute visitArray(ArrayType t, Element element) {
// }
// }
// return attribute;
return new AnnotationMetaSingleAttribute( entity, element, TypeUtils.toTypeString( t ) );
return new AnnotationMetaSingleAttribute( entity, element, TypeUtils.toArrayTypeString( t, context ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,32 @@ private 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() );
}
return type.toString();
}

public static String toArrayTypeString(ArrayType type, Context context) {
// When an ArrayType is annotated with an annotation which uses TYPE_USE targets,
// we cannot simply take the TypeMirror returned by #getComponentType because it
// itself is an AnnotatedType.
//
// The simplest approach here to get the TypeMirror for both ArrayType use cases
// is to use the visitor to retrieve the underlying TypeMirror.
TypeMirror component = type.getComponentType().accept(
new SimpleTypeVisitor6<TypeMirror, Void>() {
@Override
protected TypeMirror defaultAction(TypeMirror e, Void aVoid) {
return e;
}
},
null
);

return extractClosestRealTypeAsString( component, context ) + "[]";
}

public static TypeElement getSuperclassTypeElement(TypeElement element) {
final TypeMirror superClass = element.getSuperclass();
//superclass of Object is of NoType which returns some other kind
Expand Down

0 comments on commit 4ebfd43

Please sign in to comment.