1 package com.github.javaparser.generator.metamodel; 2 3 import com.github.javaparser.ast.NodeList; 4 5 import java.lang.reflect.ParameterizedType; 6 import java.lang.reflect.Type; 7 import java.lang.reflect.TypeVariable; 8 import java.lang.reflect.WildcardType; 9 import java.util.Optional; 10 11 import static java.lang.reflect.Modifier.isAbstract; 12 13 /** 14 * A hacky thing that collects flags we need from AST types to generate the metamodel. 15 */ 16 class AstTypeAnalysis { 17 final boolean isAbstract; 18 boolean isOptional = false; 19 boolean isNodeList = false; 20 boolean isSelfType = false; 21 Class<?> innerType; 22 AstTypeAnalysis(Type type)23 AstTypeAnalysis(Type type) { 24 if (type instanceof Class<?>) { 25 TypeVariable<? extends Class<?>>[] typeParameters = ((Class<?>) type).getTypeParameters(); 26 if (typeParameters.length > 0) { 27 isSelfType = true; 28 } 29 } else { 30 while (type instanceof ParameterizedType) { 31 ParameterizedType t = (ParameterizedType) type; 32 Type currentOuterType = t.getRawType(); 33 if (currentOuterType == NodeList.class) { 34 isNodeList = true; 35 } 36 if (currentOuterType == Optional.class) { 37 isOptional = true; 38 } 39 40 if (t.getActualTypeArguments()[0] instanceof WildcardType) { 41 type = t.getRawType(); 42 isSelfType = true; 43 break; 44 } 45 type = t.getActualTypeArguments()[0]; 46 } 47 } 48 innerType = (Class<?>) type; 49 isAbstract = isAbstract(innerType.getModifiers()); 50 } 51 } 52