1 15 package org.apache.tapestry.bean; 16 17 import java.lang.reflect.Field ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.ClassResolver; 21 import org.apache.tapestry.IBeanProvider; 22 import org.apache.tapestry.Tapestry; 23 24 31 32 public class FieldBeanInitializer extends AbstractBeanInitializer 33 { 34 protected String _fieldName; 35 protected Object _fieldValue; 36 private boolean _fieldResolved = false; 37 38 public synchronized void setBeanProperty(IBeanProvider provider, Object bean) 39 { 40 ClassResolver resolver = provider.getClassResolver(); 41 42 if (!_fieldResolved) 43 resolveField(resolver); 44 45 setBeanProperty(bean, _fieldValue); 46 } 47 48 private void resolveField(ClassResolver resolver) 49 { 50 if (_fieldResolved) 51 return; 52 53 55 int dotx = _fieldName.lastIndexOf('.'); 56 57 if (dotx < 0) 58 throw new ApplicationRuntimeException( 59 Tapestry.format("invalid-field-name", _fieldName)); 60 61 String className = _fieldName.substring(0, dotx); 62 String simpleFieldName = _fieldName.substring(dotx + 1); 63 64 66 if (className.indexOf('.') < 0) 67 className = "java.lang." + className; 68 69 Class targetClass = null; 70 71 try 72 { 73 targetClass = resolver.findClass(className); 74 } 75 catch (Throwable t) 76 { 77 throw new ApplicationRuntimeException( 78 Tapestry.format("unable-to-resolve-class", className), 79 t); 80 } 81 82 Field field = null; 83 84 try 85 { 86 field = targetClass.getField(simpleFieldName); 87 } 88 catch (NoSuchFieldException ex) 89 { 90 throw new ApplicationRuntimeException( 91 Tapestry.format("field-not-defined", _fieldName), 92 ex); 93 } 94 95 98 try 99 { 100 _fieldValue = field.get(null); 101 } 102 catch (IllegalAccessException ex) 103 { 104 throw new ApplicationRuntimeException( 105 Tapestry.format("illegal-field-access", _fieldName), 106 ex); 107 } 108 catch (NullPointerException ex) 109 { 110 throw new ApplicationRuntimeException( 111 Tapestry.format("field-is-instance", _fieldName), 112 ex); 113 } 114 115 _fieldResolved = true; 116 } 117 118 } | Popular Tags |