44import online .sharedtype .processor .context .TypeStore ;
55import online .sharedtype .processor .domain .ArrayTypeInfo ;
66import online .sharedtype .processor .domain .ConcreteTypeInfo ;
7- import online .sharedtype .processor .domain .Constants ;
87import online .sharedtype .processor .domain .DependingKind ;
98import online .sharedtype .processor .domain .TypeInfo ;
109import online .sharedtype .processor .domain .TypeVariableInfo ;
1716import javax .lang .model .type .TypeKind ;
1817import javax .lang .model .type .TypeMirror ;
1918import javax .lang .model .type .TypeVariable ;
19+ import javax .lang .model .util .Types ;
2020import java .util .Collections ;
2121import java .util .List ;
2222import java .util .stream .Collectors ;
2525import static online .sharedtype .processor .support .Preconditions .checkArgument ;
2626
2727/**
28- *
2928 * @author Cause Chung
3029 */
3130final class TypeInfoParserImpl implements TypeInfoParser {
3231 private final Context ctx ;
3332 private final TypeStore typeStore ;
33+ private final Types types ;
3434
3535 TypeInfoParserImpl (Context ctx ) {
3636 this .ctx = ctx ;
3737 this .typeStore = ctx .getTypeStore ();
38+ this .types = ctx .getProcessingEnv ().getTypeUtils ();
3839 }
3940
4041 @ Override
@@ -66,9 +67,8 @@ private TypeInfo parseDeclared(DeclaredType declaredType, TypeContext typeContex
6667 TypeMirror currentType = declaredType ;
6768 TypeInfo typeInfo = null ;
6869 while (ctx .isArraylike (currentType )) {
69- checkArgument (typeArgs .size () == 1 , "Array type must have exactly one type argument, but got: %s, type: %s" , typeArgs .size (), currentType );
7070 arrayStack ++;
71- currentType = typeArgs . get ( 0 );
71+ currentType = locateArrayComponentType ( currentType );
7272 if (currentType instanceof DeclaredType ) {
7373 DeclaredType argDeclaredType = (DeclaredType ) currentType ;
7474 TypeElement element = (TypeElement ) argDeclaredType .asElement ();
@@ -108,7 +108,7 @@ private TypeInfo parseDeclared(DeclaredType declaredType, TypeContext typeContex
108108 }
109109
110110 if (typeContext .getDependingKind () == DependingKind .COMPONENTS && typeInfo instanceof ConcreteTypeInfo ) {
111- ConcreteTypeInfo concreteTypeInfo = (ConcreteTypeInfo )typeInfo ;
111+ ConcreteTypeInfo concreteTypeInfo = (ConcreteTypeInfo ) typeInfo ;
112112 concreteTypeInfo .referencingTypes ().add (typeContext .getTypeDef ());
113113 }
114114
@@ -125,14 +125,33 @@ private TypeVariableInfo parseTypeVariable(TypeVariable typeVariable, TypeContex
125125 String qualifiedName = TypeVariableInfo .concatQualifiedName (contextTypeQualifiedName , simpleName );
126126 TypeInfo typeInfo = typeStore .getTypeInfo (qualifiedName , Collections .emptyList ());
127127 if (typeInfo != null ) {
128- return (TypeVariableInfo )typeInfo ;
128+ return (TypeVariableInfo ) typeInfo ;
129129 }
130130 typeInfo = TypeVariableInfo .builder ()
131131 .contextTypeQualifiedName (contextTypeQualifiedName )
132132 .name (simpleName )
133133 .qualifiedName (qualifiedName )
134134 .build ();
135135 typeStore .saveTypeInfo (qualifiedName , Collections .emptyList (), typeInfo );
136- return (TypeVariableInfo )typeInfo ;
136+ return (TypeVariableInfo ) typeInfo ;
137+ }
138+
139+ private TypeMirror locateArrayComponentType (TypeMirror typeMirror ) {
140+ TypeMirror cur = typeMirror ;
141+ int depth = 0 ;
142+ while (!ctx .isTopArrayType (cur )) {
143+ for (TypeMirror supertype : types .directSupertypes (cur )) {
144+ if (ctx .isArraylike (supertype )) {
145+ cur = supertype ;
146+ break ;
147+ }
148+ }
149+ if (depth ++ > 100 ) {
150+ throw new SharedTypeInternalError ("Array type hierarchy exceed max depth: " + typeMirror );
151+ }
152+ }
153+ List <? extends TypeMirror > typeArgs = ((DeclaredType )cur ).getTypeArguments ();
154+ checkArgument (typeArgs .size () == 1 , "Array type must have exactly one type argument, but got: %s, type: %s" , typeArgs .size (), typeMirror );
155+ return typeArgs .get (0 );
137156 }
138157}
0 commit comments