Advertisement
Guest User

asDBObject

a guest
May 29th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1.     public static <T> BasicDBObject asDBObject(T instance) {
  2.         BasicDBObject b = new BasicDBObject();
  3.         Collection<Field> fieldMap = getAnnotationFields(instance.getClass(), DBSync.class);
  4.         for (Field f : fieldMap) {
  5.             try {
  6.                 DBSync saveData = f.getAnnotation(DBSync.class);
  7.                 if (saveData == null) {
  8.                     continue;
  9.                 }
  10.                 if (!isSerializeAble(f)) {
  11.                     continue;
  12.                 }
  13.                 boolean hadAccess = f.isAccessible();
  14.                 if (!hadAccess) {
  15.                     f.setAccessible(true);
  16.                 }
  17.                 String fieldName = f.getName();
  18.                 if (!saveData.value().isEmpty()) {
  19.                     fieldName = saveData.value();
  20.                 }
  21.                 if (f.getType().isArray()) { // array
  22.                     Object[] arrayData = (Object[]) f.get(instance);
  23.                     Class<?> arrayType = getArrayType(f);
  24.                     if (isPrimitive(arrayType)) {
  25.                         b.put(fieldName, f.get(instance));
  26.                         continue;
  27.                     } else {
  28.                         BasicDBList list = new BasicDBList();
  29.                         for (Object arrayObject : arrayData) {
  30.                             list.add(asDBObject(arrayObject));
  31.                         }
  32.                         b.put(fieldName, list);
  33.                         continue;
  34.                     }
  35.                 } else if (Collection.class.isAssignableFrom(f.getType())) { // Collection
  36.                     Collection collectionData = (Collection) f.get(instance);
  37.                     Class<?> collectionType = getCollectionType(f);
  38.                     if (isPrimitive(collectionType)) {
  39.                         b.put(fieldName, f.get(instance));
  40.                         continue;
  41.                     } else {
  42.                         BasicDBList list = new BasicDBList();
  43.                         for (Object collectionObject : collectionData) {
  44.                             list.add(asDBObject(collectionObject));
  45.                         }
  46.                         b.put(fieldName, list);
  47.                         continue;
  48.                     }
  49.                 }
  50.                 Object o = f.get(instance);
  51.                 f.setAccessible(hadAccess);
  52.                 b.put(fieldName, o);
  53.             } catch (IllegalAccessException | IllegalArgumentException ex) {
  54.                 ex.printStackTrace();
  55.             }
  56.         }
  57.         return b;
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement