77import org .openzen .zencode .java .TypeVariableContext ;
88import org .openzen .zencode .java .module .JavaAnnotatedType ;
99import org .openzen .zencode .java .module .JavaNativePackageInfo ;
10- import org .openzen .zencode .java .module .JavaRuntimeClass ;
1110import org .openzen .zencode .shared .CodePosition ;
1211import org .openzen .zencode .shared .LiteralSourceFile ;
1312import org .openzen .zenscript .codemodel .FunctionHeader ;
1413import org .openzen .zenscript .codemodel .GenericMapper ;
1514import org .openzen .zenscript .codemodel .Modifiers ;
16- import org .openzen .zenscript .codemodel .ModuleSpace ;
1715import org .openzen .zenscript .codemodel .compilation .CompileContext ;
1816import org .openzen .zenscript .codemodel .definition .ClassDefinition ;
1917import org .openzen .zenscript .codemodel .definition .ZSPackage ;
@@ -68,15 +66,10 @@ public TypeID getType(TypeVariableContext context, AnnotatedType type) {
6866 } else if (type .isAnnotationPresent (ZenCodeType .USize .class )) {
6967 result = BasicTypeID .USIZE ;
7068 } else {
71- boolean unsigned = type .isAnnotationPresent (ZenCodeType .Unsigned .class );
72- result = loadType (context , JavaAnnotatedType .of (type ), unsigned );
69+ result = loadType (context , JavaAnnotatedType .of (type ));
7370 }
7471
75- boolean isOptional = type .isAnnotationPresent (ZenCodeType .Nullable .class );
76- if (isOptional && !result .isOptional ())
77- result = new OptionalTypeID (result );
78-
79- return result ;
72+ return optionallyWrap (type , result );
8073 }
8174
8275 @ Override
@@ -106,19 +99,19 @@ public TypeID parseType(String type) {
10699 }
107100 }
108101
109- private TypeID loadType (TypeVariableContext context , JavaAnnotatedType type , boolean unsigned ) {
102+ private TypeID loadType (TypeVariableContext context , JavaAnnotatedType type ) {
110103 final JavaAnnotatedType .ElementType elementType = type .getElementType ();
111104
112105 try {
113106 switch (elementType ) {
114107 case ANNOTATED_PARAMETERIZED_TYPE :
115- return loadAnnotatedParameterizedType (context , (AnnotatedParameterizedType ) type .getAnnotatedElement (), unsigned );
108+ return loadAnnotatedParameterizedType (context , (AnnotatedParameterizedType ) type .getAnnotatedElement ());
116109 case ANNOTATED_TYPE :
117- return loadAnnotatedType (context , (AnnotatedType ) type .getAnnotatedElement (), unsigned );
110+ return loadAnnotatedType (context , (AnnotatedType ) type .getAnnotatedElement ());
118111 case CLASS :
119- return loadClass (context , (Class <?>) type .getType (), unsigned );
112+ return loadClass (context , (Class <?>) type .getType ());
120113 case GENERIC_ARRAY :
121- return loadGenericArray (context , (GenericArrayType ) type .getType (), unsigned );
114+ return loadGenericArray (context , (GenericArrayType ) type .getType ());
122115 case PARAMETERIZED_TYPE :
123116 return loadParameterizedType (context , (ParameterizedType ) type .getType ());
124117 case TYPE_VARIABLE :
@@ -133,56 +126,52 @@ private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type, boo
133126 throw new IllegalArgumentException ("Invalid type " + elementType + ": not yet implemented or foolery" );
134127 }
135128
136- private TypeSymbol loadRawType (TypeVariableContext context , JavaAnnotatedType type , boolean unsigned ) {
129+ private TypeSymbol loadRawType (JavaAnnotatedType type ) {
137130 final JavaAnnotatedType .ElementType elementType = type .getElementType ();
138131
139132 try {
140- switch (elementType ) {
141- case CLASS :
142- return findType ((Class <?>) type .getType ());
143- default :
144- throw new IllegalArgumentException ("Didn't expect to get this kind of type: " + type );
133+ if (Objects .requireNonNull (elementType ) == JavaAnnotatedType .ElementType .CLASS ) {
134+ return findType ((Class <?>) type .getType ());
145135 }
136+ throw new IllegalArgumentException ("Didn't expect to get this kind of type: " + type );
146137 } catch (final IllegalArgumentException e ) {
147138 throw new IllegalArgumentException ("Unable to analyze type: " + type , e );
148139 }
149140 }
150141
151- private TypeID loadAnnotatedParameterizedType (TypeVariableContext context , AnnotatedParameterizedType type , boolean unsigned ) {
142+ private TypeID loadAnnotatedParameterizedType (TypeVariableContext context , AnnotatedParameterizedType type ) {
152143 final ParameterizedType parameterizedType = this .getTypeIfValid (JavaAnnotatedType .of (type .getType ()), JavaAnnotatedType .ElementType .PARAMETERIZED_TYPE );
153144 final JavaAnnotatedType rawType = JavaAnnotatedType .of (parameterizedType .getRawType ());
154145
155146 final JavaAnnotatedType [] actualTypeArguments = JavaAnnotatedType .arrayOf (type .getAnnotatedActualTypeArguments ());
156147 final TypeID [] codeParameters = new TypeID [actualTypeArguments .length ];
157148
158149 for (int i = 0 ; i < actualTypeArguments .length ; i ++) {
159- codeParameters [i ] = this .loadType (context , actualTypeArguments [i ], false );
150+ codeParameters [i ] = this .loadType (context , actualTypeArguments [i ]);
160151 }
161152
162153 if (rawType .getElementType () == JavaAnnotatedType .ElementType .CLASS ) {
154+ //noinspection SuspiciousMethodCalls
163155 if (specialTypes .containsKey (rawType .getType ())) {
156+ //noinspection SuspiciousMethodCalls
164157 return specialTypes .get (rawType .getType ()).apply (codeParameters );
165158 }
166159
167- final TypeSymbol rawTypeSymbol = loadRawType (context , rawType , unsigned );
160+ final TypeSymbol rawTypeSymbol = loadRawType (rawType );
168161 return DefinitionTypeID .create (rawTypeSymbol , codeParameters );
169162 }
170- return this .loadType (context , JavaAnnotatedType .of (type ), unsigned );
163+ return this .loadType (context , JavaAnnotatedType .of (type ));
171164 }
172165
173- private TypeID loadAnnotatedType (TypeVariableContext context , AnnotatedType type , boolean unsigned ) {
166+ private TypeID loadAnnotatedType (TypeVariableContext context , AnnotatedType type ) {
174167 final JavaAnnotatedType annotatedType = JavaAnnotatedType .of (type .getType ());
175- return this .loadType (context , annotatedType , unsigned );
168+ TypeID result = this .loadType (context , annotatedType );
169+ return optionallyWrap (type , result );
176170 }
177171
178- private TypeID loadClass (TypeVariableContext context , Class <?> type , boolean unsigned ) {
179- if (unsigned ) {
180- return unsignedByClass .computeIfAbsent (type , it -> {
181- throw new IllegalArgumentException ("This class cannot be used as unsigned: " + it );
182- });
183- }
172+ private TypeID loadClass (TypeVariableContext context , Class <?> type ) {
184173 if (type .isArray ()) {
185- final TypeID baseType = this .loadType (context , JavaAnnotatedType .of (type .getComponentType ()), false );
174+ final TypeID baseType = this .loadType (context , JavaAnnotatedType .of (type .getComponentType ()));
186175 return new ArrayTypeID (baseType );
187176 }
188177 if (type .isAnnotationPresent (FunctionalInterface .class )) {
@@ -205,9 +194,9 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type, boolean uns
205194 return DefinitionTypeID .create (definition , typeParameters .toArray (TypeID .NONE ));
206195 }
207196
208- private TypeID loadGenericArray (TypeVariableContext context , GenericArrayType type , boolean unsigned ) {
197+ private TypeID loadGenericArray (TypeVariableContext context , GenericArrayType type ) {
209198 final JavaAnnotatedType componentType = JavaAnnotatedType .of (type .getGenericComponentType ());
210- final TypeID baseType = this .loadType (context , componentType , unsigned );
199+ final TypeID baseType = this .loadType (context , componentType );
211200 return new ArrayTypeID (baseType );
212201 }
213202
@@ -221,7 +210,7 @@ private TypeID loadParameterizedType(TypeVariableContext context, ParameterizedT
221210
222211 TypeID [] codeParameters = new TypeID [typeArguments .length ];
223212 for (int i = 0 ; i < typeArguments .length ; i ++) {
224- codeParameters [i ] = this .loadType (context , typeArguments [i ], false );
213+ codeParameters [i ] = this .loadType (context , typeArguments [i ]);
225214 }
226215
227216 if (rawType == Map .class ) {
@@ -342,7 +331,7 @@ private TypeID loadFunctionalInterface(TypeVariableContext loadContext, Class<?>
342331 Map <TypeParameter , TypeID > mapping = new HashMap <>();
343332 TypeVariable <?>[] javaParameters = cls .getTypeParameters ();
344333 for (int i = 0 ; i < parameters .length ; i ++) {
345- mapping .put (context .get (javaParameters [i ]), loadType (loadContext , parameters [i ], false ));
334+ mapping .put (context .get (javaParameters [i ]), loadType (loadContext , parameters [i ]));
346335 }
347336
348337 header = header .withGenericArguments (new GenericMapper (mapping , TypeID .NONE ));
@@ -469,4 +458,11 @@ private void fillSpecialTypes() {
469458 specialTypes .put (ToLongFunction .class , args -> new FunctionTypeID (new FunctionHeader (BasicTypeID .LONG , args [0 ])));
470459 specialTypes .put (UnaryOperator .class , args -> new FunctionTypeID (new FunctionHeader (args [0 ], args [0 ])));
471460 }
461+
462+ private TypeID optionallyWrap (AnnotatedType type , TypeID result ) {
463+ boolean isOptional = type .isAnnotationPresent (ZenCodeType .Nullable .class );
464+ if (isOptional && !result .isOptional ())
465+ result = new OptionalTypeID (result );
466+ return result ;
467+ }
472468}
0 commit comments