private static Object tryToClone(Cloneable o) { Object result = null; try { Method cloneMethod = o.getClass().getMethod("clone", new Class[0]); result = cloneMethod.invoke(o, new Object[0]); } catch (Exception e) { ourLogger.warn("Couldn't clone data of type " + o.getClass().getName(), e); } return result; }
/** * Create a new ObjectColumn. * * @param type the data type of Objects in this column * @param nrows the initial size of the column * @param capacity the initial capacity of the column * @param defaultValue the default value for the column. If this value is cloneable, it will be * cloned when assigned as defaultValue, otherwise the input reference will be used for every * default value. */ public ObjectColumn(Class type, int nrows, int capacity, Object defaultValue) { super(type, defaultValue); if (capacity < nrows) { throw new IllegalArgumentException("Capacity value can not be less than the row count."); } m_values = new Object[capacity]; try { // since Object's clone method is protected, we default to // using reflection to create clones. Cloneable def = (Cloneable) defaultValue; Method m = def.getClass().getMethod("clone", (Class[]) null); for (int i = 0; i < capacity; ++i) { m_values[i] = m.invoke(m_defaultValue, (Object[]) null); } } catch (Exception e) { if (defaultValue != null) { Logger.getLogger(getClass().getName()) .fine( "Default value of type \"" + defaultValue.getClass().getName() + "\" is not " + "cloneable. Using Object reference directly."); } Arrays.fill(m_values, defaultValue); } m_size = nrows; }
public void revertToDefault(int row) { try { // since Object's clone method is protected, we default to // using reflection to create clones. Cloneable def = (Cloneable) m_defaultValue; Method m = def.getClass().getMethod("clone", (Class[]) null); set(m.invoke(m_defaultValue, (Object[]) null), row); } catch (Exception e) { set(m_defaultValue, row); } }
/** @see prefuse.data.column.Column#setMaximumRow(int) */ public void setMaximumRow(int nrows) { if (nrows > m_values.length) { int capacity = Math.max((3 * m_values.length) / 2 + 1, nrows); Object[] values = new Object[capacity]; System.arraycopy(m_values, 0, values, 0, m_size); try { // since Object's clone method is protected, we default to // using reflection to create clones. Cloneable def = (Cloneable) m_defaultValue; Method m = def.getClass().getMethod("clone", (Class[]) null); for (int i = m_size; i < capacity; ++i) { values[i] = m.invoke(m_defaultValue, (Object[]) null); } } catch (Exception e) { Arrays.fill(values, m_size, capacity, m_defaultValue); } m_values = values; } m_size = nrows; }
static { Cloneable c = new Cloneable() {}; // anonymous cloneable anonymousClass = c.getClass(); }