try { type = TypeUtil.getFieldInfo(o.getClass(), fieldName).getGetter().getReturnType().getName(); } catch (Exception e3) {
try { type = TypeUtil.getFieldInfo(o.getClass(), fieldName).getGetter().getReturnType().getName(); } catch (Exception e3) {
/** * Returns the value of a public or protected Field of an Object given the field name * * @param o the Object * @param fieldName the name of the relevant Field * @return the value of the Field * @throws IllegalAccessException if the field is inaccessible */ public static Object getFieldValue(Object o, String fieldName) throws IllegalAccessException { try { return getGetter(o.getClass(), fieldName).invoke(o, new Object[] {}); } catch (Exception e) { String type = ""; try { type = " (a " + getFieldInfo(o.getClass(), fieldName).getGetter().getReturnType() .getName() + ")"; } catch (Exception e3) { type = " (available fields are " + getFieldInfos(o.getClass()).keySet() + ")"; } IllegalAccessException e2 = new IllegalAccessException("Couldn't get field \"" + Util.decomposeClass(o.getClass()) + "." + fieldName + "\"" + type); e2.initCause(e); throw e2; } }
/** * Returns the value of a public or protected Field of an Object given by the field name without * dereferencing any ProxyReference objects. * * @param o the Object * @param fieldName the name of the relevant Field * @return the value of the field, without dereferencing ProxyReferences * @throws IllegalAccessException if the field is inaccessible */ public static Object getFieldProxy(Object o, String fieldName) throws IllegalAccessException { try { Method proxyGetter = getProxyGetter(o.getClass(), fieldName); if (proxyGetter == null) { proxyGetter = getGetter(o.getClass(), fieldName); } return proxyGetter.invoke(o, new Object[] {}); } catch (Exception e) { String type = null; try { type = getFieldInfo(o.getClass(), fieldName).getGetter().getReturnType().getName(); } catch (Exception e3) { // ignore } IllegalAccessException e2 = new IllegalAccessException("Couldn't proxyGet field \"" + o.getClass().getName() + "." + fieldName + "\"" + (type == null ? "" : " (a " + type + ")")); e2.initCause(e); throw e2; } }
/** * Returns the value of a public or protected Field of an Object given by the field name without * dereferencing any ProxyReference objects. * * @param o the Object * @param fieldName the name of the relevant Field * @return the value of the field, without dereferencing ProxyReferences * @throws IllegalAccessException if the field is inaccessible */ public static Object getFieldProxy(Object o, String fieldName) throws IllegalAccessException { try { Method proxyGetter = getProxyGetter(o.getClass(), fieldName); if (proxyGetter == null) { proxyGetter = getGetter(o.getClass(), fieldName); } return proxyGetter.invoke(o, new Object[] {}); } catch (Exception e) { String type = null; try { type = getFieldInfo(o.getClass(), fieldName).getGetter().getReturnType().getName(); } catch (Exception e3) { // ignore } IllegalAccessException e2 = new IllegalAccessException("Couldn't proxyGet field \"" + o.getClass().getName() + "." + fieldName + "\"" + (type == null ? "" : " (a " + type + ")")); e2.initCause(e); throw e2; } }
/** * Returns the value of a public or protected Field of an Object given the field name * * @param o the Object * @param fieldName the name of the relevant Field * @return the value of the Field * @throws IllegalAccessException if the field is inaccessible */ public static Object getFieldValue(Object o, String fieldName) throws IllegalAccessException { try { return getGetter(o.getClass(), fieldName).invoke(o, new Object[] {}); } catch (Exception e) { String type = ""; try { type = " (a " + getFieldInfo(o.getClass(), fieldName).getGetter().getReturnType() .getName() + ")"; } catch (Exception e3) { type = " (available fields are " + getFieldInfos(o.getClass()).keySet() + ")"; } IllegalAccessException e2 = new IllegalAccessException("Couldn't get field \"" + Util.decomposeClass(o.getClass()) + "." + fieldName + "\"" + type); e2.initCause(e); throw e2; } }
public void testGetFieldInfosSuper() throws Exception { Class c = Company.class; Map got = TypeUtil.getFieldInfos(c); assertEquals(new HashSet(Arrays.asList(new String[] {"id", "name", "vatNumber", "address", "bank", "oldContracts", "contractors", "CEO", "departments", "secretarys"})), got.keySet()); TypeUtil.FieldInfo idInfo = (TypeUtil.FieldInfo) got.get("id"); assertEquals("id", idInfo.getName()); assertEquals(c.getMethod("getId", new Class[] {}), idInfo.getGetter()); assertEquals(c.getMethod("setId", new Class[] {Integer.class}), idInfo.getSetter()); TypeUtil.FieldInfo addressInfo = (TypeUtil.FieldInfo) got.get("address"); assertEquals("address", addressInfo.getName()); assertEquals(c.getMethod("getAddress", new Class[] {}), addressInfo.getGetter()); assertEquals(c.getMethod("setAddress", new Class[] {Address.class}), addressInfo.getSetter()); TypeUtil.FieldInfo nameInfo = (TypeUtil.FieldInfo) got.get("name"); assertEquals("name", nameInfo.getName()); assertEquals(c.getMethod("getName", new Class[] {}), nameInfo.getGetter()); assertEquals(c.getMethod("setName", new Class[] {String.class}), nameInfo.getSetter()); TypeUtil.FieldInfo vatInfo = (TypeUtil.FieldInfo) got.get("vatNumber"); assertEquals("vatNumber", vatInfo.getName()); assertEquals(c.getMethod("getVatNumber", new Class[] {}), vatInfo.getGetter()); assertEquals(c.getMethod("setVatNumber", new Class[] {Integer.TYPE}), vatInfo.getSetter()); }
public void testGetFieldInfosDynamic() throws Exception { Class<?> c = DynamicUtil.createObject(Collections.singleton(Company.class)).getClass(); Map got = TypeUtil.getFieldInfos(c); assertEquals(new HashSet(Arrays.asList(new String[] {"id", "name", "vatNumber", "bank", "address", "oldContracts", "contractors", "CEO", "departments", "secretarys"})), got.keySet()); TypeUtil.FieldInfo idInfo = (TypeUtil.FieldInfo) got.get("id"); assertEquals("id", idInfo.getName()); assertEquals(c.getMethod("getId", new Class[] {}), idInfo.getGetter()); assertEquals(c.getMethod("setId", new Class[] {Integer.class}), idInfo.getSetter()); TypeUtil.FieldInfo addressInfo = (TypeUtil.FieldInfo) got.get("address"); assertEquals("address", addressInfo.getName()); assertEquals(c.getMethod("getAddress", new Class[] {}), addressInfo.getGetter()); assertEquals(c.getMethod("setAddress", new Class[] {Address.class}), addressInfo.getSetter()); TypeUtil.FieldInfo nameInfo = (TypeUtil.FieldInfo) got.get("name"); assertEquals("name", nameInfo.getName()); assertEquals(c.getMethod("getName", new Class[] {}), nameInfo.getGetter()); assertEquals(c.getMethod("setName", new Class[] {String.class}), nameInfo.getSetter()); TypeUtil.FieldInfo vatInfo = (TypeUtil.FieldInfo) got.get("vatNumber"); assertEquals("vatNumber", vatInfo.getName()); assertEquals(c.getMethod("getVatNumber", new Class[] {}), vatInfo.getGetter()); assertEquals(c.getMethod("setVatNumber", new Class[] {Integer.TYPE}), vatInfo.getSetter()); }
/** * Returns the Method object that is the getter for the field name. * * @param c the Class * @param fieldName the name of the relevant field * @return the Getter, or null if the field is not found */ public static Method getGetter(Class<?> c, String fieldName) { FieldInfo info = getFieldInfo(c, fieldName); if (info != null) { return info.getGetter(); } return null; }
/** * Returns the Method object that is the getter for the field name. * * @param c the Class * @param fieldName the name of the relevant field * @return the Getter, or null if the field is not found */ public static Method getGetter(Class<?> c, String fieldName) { FieldInfo info = getFieldInfo(c, fieldName); if (info != null) { return info.getGetter(); } return null; }
public void testGetFieldInfos() throws Exception { Class c = Address.class; Map got = TypeUtil.getFieldInfos(c); assertEquals(new HashSet(Arrays.asList(new String[] {"id", "address"})), got.keySet()); TypeUtil.FieldInfo idInfo = (TypeUtil.FieldInfo) got.get("id"); assertEquals("id", idInfo.getName()); assertEquals(c.getMethod("getId", new Class[] {}), idInfo.getGetter()); assertEquals(c.getMethod("setId", new Class[] {Integer.class}), idInfo.getSetter()); TypeUtil.FieldInfo addressInfo = (TypeUtil.FieldInfo) got.get("address"); assertEquals("address", addressInfo.getName()); assertEquals(c.getMethod("getAddress", new Class[] {}), addressInfo.getGetter()); assertEquals(c.getMethod("setAddress", new Class[] {String.class}), addressInfo.getSetter()); }