1 30 31 34 package com.nightlabs.inheritance; 35 36 import java.lang.reflect.Field ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 40 43 public class MapFieldInheriter implements FieldInheriter 44 { 45 48 public void copyFieldValue(Inheritable mother, Inheritable child, Class motherClass, Class childClass, Field field, FieldMetaData motherFieldMetaData, FieldMetaData childFieldMetaData) 49 { 50 if (!Map .class.isAssignableFrom(field.getType())) 51 throw new ClassCastException ("field \""+field.getName()+"\" of class \""+motherClass.getName()+"\" is not a Map!"); 52 53 if (!(motherFieldMetaData instanceof MapFieldMetaData)) 54 throw new ClassCastException ("motherFieldMetaData for field \""+field.getName()+"\" of class \""+motherClass.getName()+"\" is not an instance of MapFieldMetaData!"); 55 56 if (!(childFieldMetaData instanceof MapFieldMetaData)) 57 throw new ClassCastException ("childFieldMetaData for field \""+field.getName()+"\" of class \""+childClass.getName()+"\" is not an instance of MapFieldMetaData!"); 58 59 MapFieldMetaData _motherFieldMetaData = (MapFieldMetaData)motherFieldMetaData; 60 MapFieldMetaData _childFieldMetaData = (MapFieldMetaData)childFieldMetaData; 61 62 Map motherMap; 63 Map childMap; 64 try { 65 field.setAccessible(true); 66 motherMap = (Map )field.get(mother); 67 childMap = (Map )field.get(child); 68 } catch (IllegalArgumentException e) { 69 throw e; 70 } catch (IllegalAccessException e) { 71 throw new RuntimeException (e); 72 } 73 74 for (Iterator it = childMap.keySet().iterator(); it.hasNext(); ) { 76 Object key = it.next(); 77 if (!motherMap.containsKey(key)) { 78 MapEntryMetaData c_memd = _childFieldMetaData.getMapEntryMetaData(key); 79 if (c_memd == null) 80 throw new NullPointerException ("childFieldMetaData.getMapEntryMetaData(\""+key+"\") field \""+field.getName()+"\" of class \""+childClass.getName()+"\" returned null!"); 81 82 if (c_memd.isValueInherited()) { 83 it.remove(); 84 _childFieldMetaData.removeMapEntryMetaData(key); 85 } 86 } } 89 for (Iterator it = motherMap.entrySet().iterator(); it.hasNext(); ) { 91 Map.Entry me = (Map.Entry )it.next(); 92 Object key = me.getKey(); 93 Object value = me.getValue(); 94 MapEntryMetaData m_memd = _motherFieldMetaData.getMapEntryMetaData(key); 95 if (m_memd == null) 96 throw new NullPointerException ("motherFieldMetaData.getMapEntryMetaData(\""+key+"\") field \""+field.getName()+"\" of class \""+motherClass.getName()+"\" returned null!"); 97 MapEntryMetaData c_memd = _childFieldMetaData.getMapEntryMetaData(key); 98 if (c_memd == null) 99 throw new NullPointerException ("childFieldMetaData.getMapEntryMetaData(\""+key+"\") field \""+field.getName()+"\" of class \""+childClass.getName()+"\" returned null!"); 100 101 if (c_memd.isValueInherited()) 102 childMap.put(key, value); 103 } 105 } 106 } 107 | Popular Tags |