@@ -45,9 +45,19 @@ public static JSONObject toJSON(ByteBuffer buf) {
4545 }
4646
4747 public static <T , P > T convertToPojo (Object value , Optional <Class <P >> pojoClass ) {
48- return !pojoClass .isPresent () || !(value instanceof Map )
49- ? (T ) value
50- : (T ) toPojo (pojoClass .get (), (Map <String , Object >) value );
48+ if (pojoClass .isPresent ()) {
49+ if (pojoClass .get ().isEnum ()) {
50+ Enum <?>[] enumConstants = ((Class <Enum <?>>) pojoClass .get ()).getEnumConstants ();
51+ for (Enum <?> enumConst : enumConstants ) {
52+ if (enumConst .name ().equals (value )) {
53+ return (T ) enumConst ;
54+ }
55+ }
56+ } else if (value instanceof Map ) {
57+ return (T ) RethinkDB .getObjectMapper ().convertValue (value , pojoClass .get ());
58+ }
59+ }
60+ return (T ) value ;
5161 }
5262
5363 public static byte [] toUTF8 (String s ) {
@@ -57,43 +67,6 @@ public static byte[] toUTF8(String s) {
5767 public static String fromUTF8 (byte [] ba ) {
5868 return new String (ba , StandardCharsets .UTF_8 );
5969 }
60-
61- /**
62- * Converts a String-to-Object map to a POJO using bean introspection.<br>
63- * The POJO's class must be public and satisfy one of the following conditions:<br>
64- * 1. Should have a public parameterless constructor and public setters for all properties
65- * in the map. Properties with no corresponding entries in the map would have default values<br>
66- * 2. Should have a public constructor with parameters matching the contents of the map
67- * either by names and value types. Names of parameters are only available since Java 8
68- * and only in case <code>javac</code> is run with <code>-parameters</code> argument.<br>
69- * If the POJO's class doesn't satisfy the conditions, a ReqlDriverError is thrown.
70- * @param <T> POJO's type
71- * @param pojoClass POJO's class to be instantiated
72- * @param map Map to be converted
73- * @return Instantiated POJO
74- */
75- @ SuppressWarnings ("unchecked" )
76- private static <T > T toPojo (Class <T > pojoClass , Map <String , Object > map ) {
77- // Jackson will throw an error if the POJO is not annotated with an ignore
78- // annotation and the server gives a value that is not a field within the POJO To
79- // prevent this, we get a list of all the field names from the class, and iterate
80- // through the map. If the map contains a key that the does not correlate to a
81- // field name, then that entry from the map is removed and we log an error.
82- List <String > nameFields = new ArrayList <>();
83- Arrays .asList (pojoClass .getDeclaredFields ()).forEach (field -> nameFields .add (field .getName ()));
84- List <String > toRemove = new ArrayList <>();
85-
86- map .keySet ().forEach (s -> {
87- if (!nameFields .contains (s ))
88- {
89- log .error ("Got JSON field [" + s + "] from server. POJO does not contain field, removing from map!" );
90- toRemove .add (s );
91- }
92- });
93- toRemove .forEach (map ::remove );
94-
95- return RethinkDB .getObjectMapper ().convertValue (map , pojoClass );
96- }
9770}
9871
9972
0 commit comments