• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.EnumSet;
10 import java.util.Optional;
11 
12 import static java.lang.reflect.Modifier.isAbstract;
13 
14 /**
15  * A hacky thing that collects flags we need from AST types to generate the metamodel.
16  */
17 public class AstTypeAnalysis {
18     public final boolean isAbstract;
19     public boolean isOptional = false;
20     public boolean isEnumSet = false;
21     public boolean isNodeList = false;
22     public boolean isSelfType = false;
23     public Class<?> innerType;
24 
AstTypeAnalysis(Type type)25     AstTypeAnalysis(Type type) {
26         if (type instanceof Class<?>) {
27             TypeVariable<? extends Class<?>>[] typeParameters = ((Class<?>) type).getTypeParameters();
28             if (typeParameters.length > 0) {
29                 isSelfType = true;
30             }
31         } else {
32             while (type instanceof ParameterizedType) {
33                 ParameterizedType t = (ParameterizedType) type;
34                 Type currentOuterType = t.getRawType();
35                 if (currentOuterType == NodeList.class) {
36                     isNodeList = true;
37                 }
38                 if (currentOuterType == Optional.class) {
39                     isOptional = true;
40                 }
41                 if (currentOuterType == EnumSet.class) {
42                     isEnumSet = true;
43                 }
44 
45                 if (t.getActualTypeArguments()[0] instanceof WildcardType) {
46                     type = t.getRawType();
47                     isSelfType = true;
48                     break;
49                 }
50                 type = t.getActualTypeArguments()[0];
51             }
52         }
53         innerType = (Class<?>) type;
54         isAbstract = isAbstract(innerType.getModifiers());
55     }
56 }
57