1 16 17 package org.springframework.beans.factory.config; 18 19 import java.lang.reflect.Field ; 20 import java.lang.reflect.Modifier ; 21 22 import org.springframework.beans.factory.BeanClassLoaderAware; 23 import org.springframework.beans.factory.BeanFactoryUtils; 24 import org.springframework.beans.factory.BeanNameAware; 25 import org.springframework.beans.factory.FactoryBean; 26 import org.springframework.beans.factory.FactoryBeanNotInitializedException; 27 import org.springframework.beans.factory.InitializingBean; 28 import org.springframework.util.ClassUtils; 29 import org.springframework.util.StringUtils; 30 31 55 public class FieldRetrievingFactoryBean implements FactoryBean, BeanNameAware, BeanClassLoaderAware, InitializingBean { 56 57 private Class targetClass; 58 59 private Object targetObject; 60 61 private String targetField; 62 63 private String staticField; 64 65 private String beanName; 66 67 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 68 69 private Field fieldObject; 71 72 73 80 public void setTargetClass(Class targetClass) { 81 this.targetClass = targetClass; 82 } 83 84 87 public Class getTargetClass() { 88 return targetClass; 89 } 90 91 98 public void setTargetObject(Object targetObject) { 99 this.targetObject = targetObject; 100 } 101 102 105 public Object getTargetObject() { 106 return this.targetObject; 107 } 108 109 116 public void setTargetField(String targetField) { 117 this.targetField = StringUtils.trimAllWhitespace(targetField); 118 } 119 120 123 public String getTargetField() { 124 return this.targetField; 125 } 126 127 134 public void setStaticField(String staticField) { 135 this.staticField = StringUtils.trimAllWhitespace(staticField); 136 } 137 138 144 public void setBeanName(String beanName) { 145 this.beanName = StringUtils.trimAllWhitespace(BeanFactoryUtils.originalBeanName(beanName)); 146 } 147 148 public void setBeanClassLoader(ClassLoader classLoader) { 149 this.beanClassLoader = classLoader; 150 } 151 152 153 public void afterPropertiesSet() throws ClassNotFoundException , NoSuchFieldException { 154 if (this.targetClass != null && this.targetObject != null) { 155 throw new IllegalArgumentException ("Specify either targetClass or targetObject, not both"); 156 } 157 158 if (this.targetClass == null && this.targetObject == null) { 159 if (this.targetField != null) { 160 throw new IllegalArgumentException ( 161 "Specify targetClass or targetObject in combination with targetField"); 162 } 163 164 if (this.staticField == null) { 166 this.staticField = this.beanName; 167 } 168 169 int lastDotIndex = this.staticField.lastIndexOf('.'); 171 if (lastDotIndex == -1 || lastDotIndex == this.staticField.length()) { 172 throw new IllegalArgumentException ( 173 "staticField must be a fully qualified class plus method name: " + 174 "e.g. 'example.MyExampleClass.MY_EXAMPLE_FIELD'"); 175 } 176 String className = this.staticField.substring(0, lastDotIndex); 177 String fieldName = this.staticField.substring(lastDotIndex + 1); 178 this.targetClass = ClassUtils.forName(className, this.beanClassLoader); 179 this.targetField = fieldName; 180 } 181 182 else if (this.targetField == null) { 183 throw new IllegalArgumentException ("targetField is required"); 185 } 186 187 Class targetClass = (this.targetObject != null) ? this.targetObject.getClass() : this.targetClass; 189 this.fieldObject = targetClass.getField(this.targetField); 190 } 191 192 193 public Object getObject() throws IllegalAccessException { 194 if (this.fieldObject == null) { 195 throw new FactoryBeanNotInitializedException(); 196 } 197 198 if (!Modifier.isPublic(this.fieldObject.getModifiers()) || 199 !Modifier.isPublic(this.fieldObject.getDeclaringClass().getModifiers())) { 200 this.fieldObject.setAccessible(true); 201 } 202 203 if (this.targetObject != null) { 204 return this.fieldObject.get(this.targetObject); 206 } 207 else{ 208 return this.fieldObject.get(null); 210 } 211 } 212 213 public Class getObjectType() { 214 return (this.fieldObject != null ? this.fieldObject.getType() : null); 215 } 216 217 public boolean isSingleton() { 218 return false; 219 } 220 221 } 222 | Popular Tags |