Skip to content

Instantly share code, notes, and snippets.

@dvelez-jimenez
Last active February 5, 2019 00:01
Show Gist options
  • Save dvelez-jimenez/d89873388a371fcbfc5ad2081c64056d to your computer and use it in GitHub Desktop.
Save dvelez-jimenez/d89873388a371fcbfc5ad2081c64056d to your computer and use it in GitHub Desktop.
Magia negra de injeccion de parametros usando reflection by Airon
import java.lang.reflect.Field;
import java.util.map;
public class ObjectUtil {
public static void injectFromMap(Object t1, Map<String, String> props) throws IllegalArgumentException, IllegalAccessException{
Class<? extends Object> c = t1.getClass();
Field[] fields = t1.getClass().getDeclaredFields();
for(Field field: fields){
String fullName = c.getName() + "." + field.getName();
if(props.contains(fullName)){
boolean oldAccesible = f.isAccesible();
field.setAccesible(true);
field.set(t1, props.get(fullName));
f.setAccesible(oldAccesible);
}
}
}
}
private Map<String, String> parameters;
ObjectUtil.injectFormMap(this, parameters);
@dvelez-jimenez
Copy link
Author

Magia negra de injeccion de parametros By Airon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment