/** * Returns a FieldInfo object for the given class and field name. * * @param c the Class * @param fieldname the fieldname * @return a FieldInfo object, or null if the fieldname is not found */ public static FieldInfo getFieldInfo(Class<?> c, String fieldname) { return getFieldInfos(c).get(fieldname); }
/** * Returns a FieldInfo object for the given class and field name. * * @param c the Class * @param fieldname the fieldname * @return a FieldInfo object, or null if the fieldname is not found */ public static FieldInfo getFieldInfo(Class<?> c, String fieldname) { return getFieldInfos(c).get(fieldname); }
/** * Convert an Object to Item format. * @param obj object to convert * @return a new Full Data Item */ public Item makeItem(FastPathObject obj) { return makeItemImpl(obj, TypeUtil.getFieldInfos(obj.getClass()).keySet()); }
/** * Convert an Object to Item format. * @param obj object to convert * @return a new Full Data Item */ public Item makeItem(FastPathObject obj) { return makeItemImpl(obj, TypeUtil.getFieldInfos(obj.getClass()).keySet()); }
private static <O extends InterMineObject> O cloneInterMineObject(O obj, boolean copyCollections) throws IllegalAccessException { @SuppressWarnings("unchecked") Class<O> clazz = (Class<O>) obj.getClass(); O newObj = DynamicUtil.createObject(clazz); for (String fieldName : TypeUtil.getFieldInfos(obj.getClass()).keySet()) { Object value = obj.getFieldProxy(fieldName); if (copyCollections && (value instanceof Collection<?>)) { newObj.setFieldValue(fieldName, new HashSet<Object>((Collection<?>) value)); } else { newObj.setFieldValue(fieldName, value); } } return newObj; }
/** * 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 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 testGetFieldInfosNoGetters() throws Exception { Map expected = new HashMap(); Class c = NoGetSet.class; // random class with no getters try { assertEquals(Collections.EMPTY_MAP, TypeUtil.getFieldInfos(c)); } catch (NullPointerException e) { fail("Should not have thrown a NullPointerException"); } }
for (String fieldName : TypeUtil.getFieldInfos(imo.getClass()).keySet()) { Object fieldValue; try {
for (String fieldName : TypeUtil.getFieldInfos(imo.getClass()).keySet()) { Object fieldValue; try {
/** * Writes the contents of any pending Clobs to the database, and replaces them in the objects * with a real ClobAccess object. * * @param c a connection * @param o the object to transform * @throws ObjectStoreException if something goes wrong * @throws SQLException if something goes wrong * @throws IllegalAccessException if something goes wrong */ protected void writePendingClobs(Connection c, Object o) throws ObjectStoreException, SQLException, IllegalAccessException { for (Map.Entry<String, TypeUtil.FieldInfo> fieldEntry : TypeUtil.getFieldInfos(o.getClass()).entrySet()) { TypeUtil.FieldInfo fieldInfo = fieldEntry.getValue(); if (ClobAccess.class.isAssignableFrom(fieldInfo.getType())) { ClobAccess ca = (ClobAccess) TypeUtil.getFieldValue(o, fieldInfo.getName()); if (ca instanceof PendingClob) { // We can't call createClob here - we already have a connection, and // we must use that one. Clob clob = new Clob(getSerialWithConnection(c)); replaceClobWithConnection(c, clob, ((PendingClob) ca) .toString()); DynamicUtil.setFieldValue(o, fieldInfo.getName(), new ClobAccess(this, clob)); } } } }
/** * Writes the contents of any pending Clobs to the database, and replaces them in the objects * with a real ClobAccess object. * * @param c a connection * @param o the object to transform * @throws ObjectStoreException if something goes wrong * @throws SQLException if something goes wrong * @throws IllegalAccessException if something goes wrong */ protected void writePendingClobs(Connection c, Object o) throws ObjectStoreException, SQLException, IllegalAccessException { for (Map.Entry<String, TypeUtil.FieldInfo> fieldEntry : TypeUtil.getFieldInfos(o.getClass()).entrySet()) { TypeUtil.FieldInfo fieldInfo = fieldEntry.getValue(); if (ClobAccess.class.isAssignableFrom(fieldInfo.getType())) { ClobAccess ca = (ClobAccess) TypeUtil.getFieldValue(o, fieldInfo.getName()); if (ca instanceof PendingClob) { // We can't call createClob here - we already have a connection, and // we must use that one. Clob clob = new Clob(getSerialWithConnection(c)); replaceClobWithConnection(c, clob, ((PendingClob) ca) .toString()); DynamicUtil.setFieldValue(o, fieldInfo.getName(), new ClobAccess(this, clob)); } } } }
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()); }
Map<String, FieldInfo> infos = TypeUtil.getFieldInfos(obj.getClass()); for (String fieldName : infos.keySet()) {
Map<String, FieldInfo> infos = TypeUtil.getFieldInfos(obj.getClass()); for (String fieldName : infos.keySet()) {
.getClass()); Set<String> modelFieldNames = fieldDescriptors.keySet(); Set<String> typeUtilFieldNames = TypeUtil.getFieldInfos(newObj.getClass()).keySet();
: TypeUtil.getFieldInfos(o.getClass()).entrySet()) { TypeUtil.FieldInfo fieldInfo = fieldEntry.getValue(); if (InterMineObject.class.isAssignableFrom(fieldInfo.getType())) {
: TypeUtil.getFieldInfos(o.getClass()).entrySet()) { TypeUtil.FieldInfo fieldInfo = fieldEntry.getValue(); if (InterMineObject.class.isAssignableFrom(fieldInfo.getType())) {
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()); }
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()); }