File: EnumLoader.java

package info (click to toggle)
python-schema-salad 8.9.20251102115403-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,060 kB
  • sloc: python: 19,247; cpp: 2,631; cs: 1,869; java: 1,341; makefile: 187; xml: 184; sh: 103; javascript: 46
file content (33 lines) | stat: -rw-r--r-- 984 bytes parent folder | download | duplicates (4)
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
package ${package}.utils;

import java.lang.reflect.Method;
import java.lang.ReflectiveOperationException;
import java.util.Arrays;
import java.util.List;

public class EnumLoader<T extends Enum> implements Loader<T>{
  private final Class<T> symbolEnumClass;

  public EnumLoader(final Class<T> symbolEnumClass) {
    this.symbolEnumClass = symbolEnumClass;
  }

  public T load(
      final Object doc,
      final String baseUri,
      final LoadingOptions loadingOptions,
      final String docRoot) {
    final String docString = Loader.validateOfJavaType(String.class, doc);
    try {
      final Method m = symbolEnumClass.getMethod("fromDocumentVal", String.class);
      final T val = (T) m.invoke(null, docString);
      return val;
    } catch (final ReflectiveOperationException e) {
      final Throwable cause = e.getCause();
      if (cause instanceof RuntimeException) {
        throw (RuntimeException) cause;
      }
      throw new RuntimeException(e);
    }
  }
}