@Test public void test() { MyClass me = new MyClass(); InstanceTypeAssigner instanceTypeAssigner = new InstanceTypeAssigner(me, B.class); B meB = new B("b"); BB meBB = new BB("bb", 4); CC meCC = new CC("cc", 5); Object[] objects = new Object[]{10, "ssss", new A(22), meB, meBB, meCC}; for (Object o : objects) { instanceTypeAssigner.tryAssign(o); } Assert.assertEquals(null, me.a); Assert.assertEquals(meB, me.b); Assert.assertEquals(meBB, me.bb); Assert.assertEquals(meCC, me.cc); }
public boolean tryAssign(Object obj) { try { for (int i = 0; i < typeFieldClasses.size(); i++) { if (typeFieldClasses.get(i).isInstance(obj) && typeFields.get(i).get(instance) == null) { typeFields.get(i).set(instance, obj); return true; } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } return false; }
InstanceTypeAssigner { public boolean tryAssign(Object obj) { try { for (int i = 0; i < typeFieldClasses.size(); i++) { if (typeFieldClasses.get(i).isInstance(obj) && typeFields.get(i).get(instance) == null) { typeFields.get(i).set(instance, obj); return true; } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } return false; } }
InstanceTypeAssigner { public boolean tryAssign(Object obj) { try { for (int i = 0; i < typeFieldClasses.size(); i++) { if (typeFieldClasses.get(i).isInstance(obj) && typeFields.get(i).get(instance) == null) { typeFields.get(i).set(instance, obj); return true; } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } return false; } InstanceTypeAssigner(Object instance, Class<?> fieldType); }
InstanceTypeAssigner { public boolean tryAssign(Object obj) { try { for (int i = 0; i < typeFieldClasses.size(); i++) { if (typeFieldClasses.get(i).isInstance(obj) && typeFields.get(i).get(instance) == null) { typeFields.get(i).set(instance, obj); return true; } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } return false; } InstanceTypeAssigner(Object instance, Class<?> fieldType); boolean tryAssign(Object obj); }
InstanceTypeAssigner { public boolean tryAssign(Object obj) { try { for (int i = 0; i < typeFieldClasses.size(); i++) { if (typeFieldClasses.get(i).isInstance(obj) && typeFields.get(i).get(instance) == null) { typeFields.get(i).set(instance, obj); return true; } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } return false; } InstanceTypeAssigner(Object instance, Class<?> fieldType); boolean tryAssign(Object obj);  }