|
2 | 2 | package cz.habarta.typescript.generator.parser; |
3 | 3 |
|
4 | 4 | import com.fasterxml.jackson.annotation.JsonFormat; |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
5 | 6 | import com.fasterxml.jackson.annotation.JsonSubTypes; |
6 | 7 | import com.fasterxml.jackson.annotation.JsonTypeInfo; |
7 | 8 | import com.fasterxml.jackson.annotation.JsonTypeName; |
@@ -255,46 +256,79 @@ protected EnumModel<?> parseEnum(SourceType<Class<?>> sourceClass) { |
255 | 256 | jsonFormat.shape() == JsonFormat.Shape.NUMBER || |
256 | 257 | jsonFormat.shape() == JsonFormat.Shape.NUMBER_FLOAT || |
257 | 258 | jsonFormat.shape() == JsonFormat.Shape.NUMBER_INT); |
| 259 | + |
| 260 | + final List<EnumMemberModel<String>> stringMembers = new ArrayList<>(); |
| 261 | + final List<EnumMemberModel<Number>> numberMembers = new ArrayList<>(); |
| 262 | + if (sourceClass.type.isEnum()) { |
| 263 | + final Class<?> enumClass = (Class<?>) sourceClass.type; |
| 264 | + |
| 265 | + try { |
| 266 | + Method valueMethod = null; |
| 267 | + final BeanInfo beanInfo = Introspector.getBeanInfo(enumClass); |
| 268 | + for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { |
| 269 | + final Method readMethod = propertyDescriptor.getReadMethod(); |
| 270 | + if (readMethod.isAnnotationPresent(JsonValue.class)) { |
| 271 | + valueMethod = readMethod; |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + int index = 0; |
| 276 | + for (Field field : enumClass.getFields()) { |
| 277 | + if (field.isEnumConstant()) { |
| 278 | + if (isNumberBased) { |
| 279 | + final Number value = getNumberEnumValue(field, valueMethod, index++); |
| 280 | + numberMembers.add(new EnumMemberModel<>(field.getName(), value, null)); |
| 281 | + } else { |
| 282 | + final String value = getStringEnumValue(field, valueMethod); |
| 283 | + stringMembers.add(new EnumMemberModel<>(field.getName(), value, null)); |
| 284 | + } |
| 285 | + } |
| 286 | + } |
| 287 | + } catch (Exception e) { |
| 288 | + System.out.println(String.format("Cannot get enum values for '%s' enum", enumClass.getName())); |
| 289 | + e.printStackTrace(System.out); |
| 290 | + } |
| 291 | + } |
| 292 | + |
258 | 293 | if (isNumberBased) { |
259 | | - return parseNumberEnum(sourceClass); |
| 294 | + return new EnumModel<>(sourceClass.type, EnumKind.NumberBased, numberMembers, null); |
260 | 295 | } else { |
261 | | - return super.parseEnum(sourceClass); |
| 296 | + return new EnumModel<>(sourceClass.type, EnumKind.StringBased, stringMembers, null); |
262 | 297 | } |
263 | 298 | } |
264 | 299 |
|
265 | | - private EnumModel<Number> parseNumberEnum(SourceType<Class<?>> sourceClass) { |
266 | | - final Class<?> enumClass = sourceClass.type; |
267 | | - final List<EnumMemberModel<Number>> members = new ArrayList<>(); |
268 | | - |
269 | | - try { |
270 | | - Method valueMethod = null; |
271 | | - final BeanInfo beanInfo = Introspector.getBeanInfo(enumClass); |
272 | | - for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { |
273 | | - final Method readMethod = propertyDescriptor.getReadMethod(); |
274 | | - if (readMethod.isAnnotationPresent(JsonValue.class)) { |
275 | | - valueMethod = readMethod; |
276 | | - } |
| 300 | + private Number getNumberEnumValue(Field field, Method valueMethod, int index) throws Exception { |
| 301 | + if (valueMethod != null) { |
| 302 | + final Object valueObject = invokeJsonValueMethod(field, valueMethod); |
| 303 | + if (valueObject instanceof Number) { |
| 304 | + return (Number) valueObject; |
277 | 305 | } |
| 306 | + } |
| 307 | + return index; |
| 308 | + } |
278 | 309 |
|
279 | | - int index = 0; |
280 | | - for (Field field : enumClass.getFields()) { |
281 | | - if (field.isEnumConstant()) { |
282 | | - final Number value; |
283 | | - if (valueMethod != null) { |
284 | | - final Object constant = field.get(null); |
285 | | - value = (Number) valueMethod.invoke(constant); |
286 | | - } else { |
287 | | - value = index++; |
288 | | - } |
289 | | - members.add(new EnumMemberModel<>(field.getName(), value, null)); |
290 | | - } |
| 310 | + private String getStringEnumValue(Field field, Method valueMethod) throws Exception { |
| 311 | + if (valueMethod != null) { |
| 312 | + final Object valueObject = invokeJsonValueMethod(field, valueMethod); |
| 313 | + if (valueObject instanceof String) { |
| 314 | + return (String) valueObject; |
291 | 315 | } |
292 | | - } catch (Exception e) { |
293 | | - System.out.println(String.format("Cannot get enum values for '%s' enum", enumClass.getName())); |
294 | | - e.printStackTrace(System.out); |
295 | 316 | } |
| 317 | + if (field.isAnnotationPresent(JsonProperty.class)) { |
| 318 | + final JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class); |
| 319 | + if (!jsonProperty.value().equals(JsonProperty.USE_DEFAULT_NAME)) { |
| 320 | + return jsonProperty.value(); |
| 321 | + } |
| 322 | + } |
| 323 | + return field.getName(); |
| 324 | + } |
296 | 325 |
|
297 | | - return new EnumModel<>(enumClass, EnumKind.NumberBased, members, null); |
| 326 | + private Object invokeJsonValueMethod(Field field, Method valueMethod) throws ReflectiveOperationException { |
| 327 | + field.setAccessible(true); |
| 328 | + final Object constant = field.get(null); |
| 329 | + valueMethod.setAccessible(true); |
| 330 | + final Object valueObject = valueMethod.invoke(constant); |
| 331 | + return valueObject; |
298 | 332 | } |
299 | 333 |
|
300 | 334 | } |
0 commit comments