1 19 package org.apache.cayenne.reflect; 20 21 import org.apache.cayenne.CayenneRuntimeException; 22 23 30 public class EmbeddedFieldAccessor implements Accessor { 31 32 protected String propertyPath; 33 protected Accessor embeddedAccessor; 34 protected Accessor embeddableAccessor; 35 protected Class embeddedClass; 36 37 public EmbeddedFieldAccessor(Class embeddableClass, Accessor embeddedAccessor, 38 Accessor embeddableAccessor) { 39 this.propertyPath = embeddedAccessor.getName() 40 + "." 41 + embeddableAccessor.getName(); 42 this.embeddedClass = embeddableClass; 43 this.embeddableAccessor = embeddableAccessor; 44 this.embeddedAccessor = embeddedAccessor; 45 } 46 47 public String getName() { 48 return propertyPath; 49 } 50 51 public Object getValue(Object object) throws PropertyException { 52 return embeddableAccessor.getValue(getEmbeddable(object)); 53 } 54 55 public void setValue(Object object, Object newValue) throws PropertyException { 56 embeddableAccessor.setValue(getEmbeddable(object), newValue); 57 } 58 59 63 protected Object getEmbeddable(Object owner) { 64 Object embeddable = embeddedAccessor.getValue(owner); 65 if (embeddable == null) { 66 embeddable = createEmbeddable(); 67 embeddedAccessor.setValue(owner, embeddable); 68 } 69 70 return embeddable; 71 } 72 73 protected Object createEmbeddable() { 74 if (embeddedClass == null) { 75 throw new NullPointerException ( 76 "Null embedded objectClass. Accessor wasn't initialized properly."); 77 } 78 79 try { 80 return embeddedClass.newInstance(); 81 } 82 catch (Throwable e) { 83 throw new CayenneRuntimeException("Error creating embedded object of class '" 84 + embeddedClass.getName() 85 + "'", e); 86 } 87 } 88 } 89 | Popular Tags |