From 2ad6bf67241aa03cd26966aae1b2d35ffd227c62 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Mon, 7 May 2018 12:38:41 -0400 Subject: [PATCH] HHH-12011 - Use TypeVisitor rather than string parsing for array types. --- .../MetaAttributeGenerationVisitor.java | 2 +- .../hibernate/jpamodelgen/util/TypeUtils.java | 54 +++++++------------ 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java index f031a2bee3..b9147941ff 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java @@ -77,7 +77,7 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6 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.getKind() ); } 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() { + @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