Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Last active February 21, 2016 20:28
Show Gist options
  • Save AnnaBoro/6d2d0930b2f0f2706d0a to your computer and use it in GitHub Desktop.
Save AnnaBoro/6d2d0930b2f0f2706d0a to your computer and use it in GitHub Desktop.
initClass
package lesson7_10.reflection;
import java.lang.reflect.Field;
import java.util.Map;
public class Reflection2<T> {
public T initClass(Class<T> tClass, Map<String, Object> map) throws IllegalAccessException, InstantiationException, InvocationTargetException {
T oClass = tClass.newInstance();
Method[] methods = oClass.getClass().getDeclaredMethods();
for (Map.Entry<String, Object> entry : map.entrySet()) {
for (Method method : methods) {
if (method.getName().equalsIgnoreCase("set" + entry.getKey())) {
method.invoke(oClass, entry.getValue());
}
}
}
return oClass;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment