Here you can find the source of buildOptions(T buildedOptions, Bindings options)
public static <T> T buildOptions(T buildedOptions, Bindings options)
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Map; import java.util.Map.Entry; import java.util.function.Function; import java.util.stream.Collectors; import javax.script.Bindings; import com.google.common.base.Preconditions; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import jdk.nashorn.api.scripting.JSObject; public class Main { public static <T> T buildOptions(T buildedOptions, Bindings options) { Preconditions.checkNotNull(buildedOptions); if (options == null || options.isEmpty()) { return buildedOptions; }/*from w w w. j a v a2 s .co m*/ Map<String, Method> methods = Arrays.stream(buildedOptions.getClass().getMethods()).filter(m -> !Object.class.equals(m.getDeclaringClass())) .collect(Collectors.toMap(m -> m.getName(), Function.identity())); for (Entry<String, Object> entry : options.entrySet()) { String key = entry.getKey(); Method keyMethod = methods.get(key); if (keyMethod == null) { throw new IllegalArgumentException("Wrong options argument " + key); } Object value = entry.getValue(); if (value instanceof Bindings) { value = dbObjectFromMap((Bindings)value); } try { keyMethod.invoke(buildedOptions, value); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } return buildedOptions; } public static BasicDBObject dbObjectFromMap(Map<String, Object> from) { BasicDBObject result = new BasicDBObject(); for (Entry<String, Object> entry : from.entrySet()) { result.append(entry.getKey(), convert(entry.getValue())); } return result; } @SuppressWarnings("unchecked") private static Object convert(Object from) { if (from instanceof JSObject) { if (((JSObject) from).isArray()) { return convertArray((Bindings) from); } } if (from instanceof Map) { return dbObjectFromMap((Map<String, Object>) from); } return from; } private static BasicDBList convertArray(Bindings from) { BasicDBList list = new BasicDBList(); for (int i = 0; i < from.size(); i++) { list.add(from.get(String.valueOf(i))); } return list; } }