GenericWritable implements Writable, Configurable { public void set(Writable obj) { instance = obj; Class<? extends Writable> instanceClazz = instance.getClass(); Class<? extends Writable>[] clazzes = getTypes(); for (int i = 0; i < clazzes.length; i++) { Class<? extends Writable> clazz = clazzes[i]; if (clazz.equals(instanceClazz)) { type = (byte) i; return; } } throw new RuntimeException("The type of instance is: " + instance.getClass() + ", which is NOT registered."); }  void set(Writable obj); Writable get(); @Override String toString(); @Override void readFields(DataInput in); @Override void write(DataOutput out); @Override Configuration getConf(); @Override void setConf(Configuration conf);  }
@Test public void testSet() throws Exception { Foo foo = new Foo(); FooGenericWritable generic = new FooGenericWritable(); generic.set(foo); try { generic = new FooGenericWritable(); generic.set(new IntWritable(1)); fail("Generic writable should have thrown an exception for a Writable not registered"); }catch (RuntimeException e) { } }