1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 package com.helger.jcodemodel.meta;
42
43 import java.util.List;
44
45 import javax.annotation.Nonnull;
46 import javax.lang.model.element.Element;
47 import javax.lang.model.element.ElementKind;
48 import javax.lang.model.element.ExecutableElement;
49 import javax.lang.model.element.TypeElement;
50 import javax.lang.model.element.TypeParameterElement;
51 import javax.lang.model.element.VariableElement;
52 import javax.lang.model.type.TypeKind;
53 import javax.lang.model.type.TypeMirror;
54
55 import com.helger.jcodemodel.AbstractJClass;
56 import com.helger.jcodemodel.AbstractJType;
57 import com.helger.jcodemodel.JCodeModel;
58 import com.helger.jcodemodel.JDefinedClass;
59 import com.helger.jcodemodel.JMethod;
60 import com.helger.jcodemodel.JTypeVar;
61 import com.helger.jcodemodel.JVar;
62
63
64
65
66 class ClassFiller
67 {
68 private final JDefinedClass m_aNewClass;
69 private final JCodeModel m_aCodeModel;
70 private final DecidedErrorTypesModelsAdapter m_aModelsAdapter;
71
72 ClassFiller (final JCodeModel codeModel,
73 final DecidedErrorTypesModelsAdapter modelsAdapter,
74 final JDefinedClass newClass)
75 {
76 m_aCodeModel = codeModel;
77 m_aModelsAdapter = modelsAdapter;
78 m_aNewClass = newClass;
79 }
80
81 void fillClass (@Nonnull final TypeElement element,
82 @Nonnull final TypeEnvironment environment) throws CodeModelBuildingException, ErrorTypeFound
83 {
84 m_aNewClass.hide ();
85 final Annotatorml#Annotator">Annotator classAnnotator = new Annotator (m_aModelsAdapter, m_aNewClass, environment);
86 classAnnotator.annotate (element.getAnnotationMirrors ());
87 for (final TypeParameterElement parameter : element.getTypeParameters ())
88 {
89 final JTypeVar typeVariable = m_aNewClass.generify (parameter.getSimpleName ().toString ());
90 environment.put (typeVariable.name (), typeVariable);
91 for (final TypeMirror type : parameter.getBounds ())
92 {
93 typeVariable.bound ((AbstractJClass) m_aModelsAdapter.toJType (type, environment));
94 }
95 }
96 final TypeMirror superclass = element.getSuperclass ();
97 if (superclass != null && superclass.getKind () != TypeKind.NONE)
98 {
99 m_aNewClass._extends ((AbstractJClass) m_aModelsAdapter.toJType (superclass, environment));
100 }
101 for (final TypeMirror iface : element.getInterfaces ())
102 {
103 m_aNewClass._implements ((AbstractJClass) m_aModelsAdapter.toJType (iface, environment));
104 }
105 for (final Element enclosedElement : element.getEnclosedElements ())
106 {
107 if (enclosedElement.getKind ().equals (ElementKind.INTERFACE) ||
108 enclosedElement.getKind ().equals (ElementKind.CLASS))
109 {
110 final TypeElement innerClassElement = (TypeElement) enclosedElement;
111 m_aModelsAdapter.defineInnerClass (m_aNewClass, innerClassElement, environment.enclosed ());
112 }
113 else
114 if (enclosedElement.getKind ().equals (ElementKind.METHOD))
115 {
116 final ExecutableElement executable = (ExecutableElement) enclosedElement;
117 final JMethod method = m_aNewClass.method (DecidedErrorTypesModelsAdapter.toJMod (executable.getModifiers ()),
118 m_aCodeModel.VOID,
119 executable.getSimpleName ().toString ());
120 final TypeEnvironment methodEnvironment = environment.enclosed ();
121 final Annotatorl#Annotator">Annotator methodAnnotator = new Annotator (m_aModelsAdapter, method, environment);
122 methodAnnotator.annotate (executable.getAnnotationMirrors ());
123 for (final TypeParameterElement parameter : executable.getTypeParameters ())
124 {
125 final JTypeVar typeVariable = method.generify (parameter.getSimpleName ().toString ());
126 methodEnvironment.put (typeVariable.name (), typeVariable);
127 for (final TypeMirror type : parameter.getBounds ())
128 {
129 typeVariable.bound ((AbstractJClass) m_aModelsAdapter.toJType (type, methodEnvironment));
130 }
131 }
132 method.type (m_aModelsAdapter.toJType (executable.getReturnType (), methodEnvironment));
133 for (final TypeMirror type : executable.getThrownTypes ())
134 {
135 final AbstractJClass/com/helger/jcodemodel/AbstractJClass.html#AbstractJClass">AbstractJClass throwable = (AbstractJClass) m_aModelsAdapter.toJType (type, methodEnvironment);
136 method._throws (throwable);
137 }
138 final List <? extends VariableElement> parameters = executable.getParameters ();
139 int n = 0;
140 for (final VariableElement variable : parameters)
141 {
142 final String parameterName = variable.getSimpleName ().toString ();
143 final TypeMirror parameterTypeMirror = variable.asType ();
144 final AbstractJType parameterType = m_aModelsAdapter.toJType (parameterTypeMirror, methodEnvironment);
145 JVar param;
146 if (executable.isVarArgs () && n == parameters.size () - 1)
147 {
148 param = method.varParam (DecidedErrorTypesModelsAdapter.toJMod (variable.getModifiers ()),
149 parameterType.elementType (),
150 parameterName);
151 }
152 else
153 {
154 param = method.param (DecidedErrorTypesModelsAdapter.toJMod (variable.getModifiers ()),
155 parameterType,
156 parameterName);
157 }
158 final Annotatornnotator">Annotator parametorAnnotator = new Annotator (m_aModelsAdapter, param, methodEnvironment);
159 parametorAnnotator.annotate (variable.getAnnotationMirrors ());
160 n++;
161 }
162 }
163 }
164 }
165
166 }