Loop over all fields in a Java class

Use getDeclaredFields on [Class] ClasWithStuff myStuff = new ClassWithStuff(); Field[] fields = myStuff.getClass().getDeclaredFields(); for(Field f : fields){ Class t = f.getType(); Object v = f.get(myStuff); if(t == boolean.class && Boolean.FALSE.equals(v)) // found default value else if(t.isPrimitive() && ((Number) v).doubleValue() == 0) // found default value else if(!t.isPrimitive() && v == null) // found default value … Read more

Javascript – removing undefined fields from an object [duplicate]

A one-liner using ES6 arrow function and ternary operator: Object.keys(obj).forEach(key => obj[key] === undefined ? delete obj[key] : {}); Or use short-circuit evaluation instead of ternary: (@Matt Langlois, thanks for the info!) Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]) Same example using if statement: Object.keys(obj).forEach(key => { if (obj[key] === undefined) { delete obj[key]; … Read more

Set field value with reflection

Hope this is something what you are trying to do : import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class Test { private Map ttp = new HashMap(); public void test() { Field declaredField = null; try { declaredField = Test.class.getDeclaredField(“ttp”); boolean accessible = declaredField.isAccessible(); declaredField.setAccessible(true); ConcurrentHashMap<Object, Object> concHashMap = new ConcurrentHashMap<Object, Object>(); concHashMap.put(“key1”, … Read more

Get list of all input objects using JavaScript, without accessing a form object

(See update at end of answer.) You can get a NodeList of all of the input elements via getElementsByTagName (DOM specification, MDC, MSDN), then simply loop through it: var inputs, index; inputs = document.getElementsByTagName(‘input’); for (index = 0; index < inputs.length; ++index) { // deal with inputs[index] element. } There I’ve used it on the … Read more