Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

All snippets
Create a snippet
Full search

Quick search:


Editing Snippet ID: #1000619
Snippet name:
Category, Type:
Content:
// get purpose 1: access a list/array/map (safer version of x.get(y)) static <A> A get(L<A> l, int idx) { ret l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } // seems to conflict with other signatures /*static <A, B> B get(Map<A, B> map, A key) { ret map != null ? map.get(key) : null; }*/ static <A> A get(A[] l, int idx) { ret idx >= 0 && idx < l(l) ? l[idx] : null; } // default to false static bool get(bool[] l, int idx) { ret idx >= 0 && idx < l(l) ? l[idx] : false; } // get purpose 2: access a field by reflection or a map static Object get(Object o, String field) { try { if (o == null) null; if (o instanceof Class) return get((Class) o, field); if (o instanceof Map) ret ((Map) o).get(field); Field f = getOpt_findField(o.getClass(), field); if (f != null) { makeAccessible(f); ret f.get(o); } ifclass DynamicObject if (o instanceof DynamicObject) ret o/DynamicObject.fieldValues.get(field); endif } catch (Exception e) { throw asRuntimeException(e); } throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName()); } sO mapMethodLike get_raw(S field, O o) { ret get_raw(o, field); } static Object get_raw(Object o, String field) ctex { if (o == null) null; Field f = get_findField(o.getClass(), field); makeAccessible(f); ret f.get(o); } static Object get(Class c, String field) { try { Field f = get_findStaticField(c, field); makeAccessible(f); return f.get(null); } catch (Exception e) { throw new RuntimeException(e); } } static Field get_findStaticField(Class<?> c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Static field '" + field + "' not found in " + c.getName()); } static Field get_findField(Class<?> c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Field '" + field + "' not found in " + c.getName()); } static O mapMethodLike get(S field, O o) { ret get(o, field); }
Comment:
Public (visible to everyone):
Created: 2015-08-18 14:51:38

This page without fancy editor.