1 16 17 package org.springframework.beans.factory.config; 18 19 import org.springframework.beans.BeanMetadataElement; 20 import org.springframework.util.Assert; 21 import org.springframework.util.ClassUtils; 22 import org.springframework.util.ObjectUtils; 23 24 37 public class TypedStringValue implements BeanMetadataElement { 38 39 private String value; 40 41 private Object targetType; 42 43 private Object source; 44 45 46 50 public TypedStringValue(String value) { 51 setValue(value); 52 } 53 54 60 public TypedStringValue(String value, Class targetType) { 61 setValue(value); 62 setTargetType(targetType); 63 } 64 65 71 public TypedStringValue(String value, String targetTypeName) { 72 setValue(value); 73 setTargetTypeName(targetTypeName); 74 } 75 76 77 83 public void setValue(String value) { 84 this.value = value; 85 } 86 87 90 public String getValue() { 91 return this.value; 92 } 93 94 100 public void setTargetType(Class targetType) { 101 Assert.notNull(targetType, "'targetType' must not be null"); 102 this.targetType = targetType; 103 } 104 105 108 public Class getTargetType() { 109 if (!(this.targetType instanceof Class )) { 110 throw new IllegalStateException ("Typed String value does not carry a resolved target type"); 111 } 112 return (Class ) this.targetType; 113 } 114 115 118 public void setTargetTypeName(String targetTypeName) { 119 Assert.notNull(targetTypeName, "'targetTypeName' must not be null"); 120 this.targetType = targetTypeName; 121 } 122 123 126 public String getTargetTypeName() { 127 if (this.targetType instanceof Class ) { 128 return ((Class ) this.targetType).getName(); 129 } 130 else { 131 return (String ) this.targetType; 132 } 133 } 134 135 138 public boolean hasTargetType() { 139 return (this.targetType instanceof Class ); 140 } 141 142 150 public Class resolveTargetType(ClassLoader classLoader) throws ClassNotFoundException { 151 if (this.targetType == null) { 152 return null; 153 } 154 Class resolvedClass = ClassUtils.forName(getTargetTypeName(), classLoader); 155 this.targetType = resolvedClass; 156 return resolvedClass; 157 } 158 159 160 164 public void setSource(Object source) { 165 this.source = source; 166 } 167 168 public Object getSource() { 169 return this.source; 170 } 171 172 173 public boolean equals(Object other) { 174 if (this == other) { 175 return true; 176 } 177 if (!(other instanceof TypedStringValue)) { 178 return false; 179 } 180 TypedStringValue otherValue = (TypedStringValue) other; 181 return (ObjectUtils.nullSafeEquals(this.value, otherValue.value) && 182 ObjectUtils.nullSafeEquals(this.targetType, otherValue.targetType)); 183 } 184 185 public int hashCode() { 186 return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.targetType); 187 } 188 189 public String toString() { 190 return "TypedStringValue: value [" + this.value + "], target type [" + this.targetType + "]"; 191 } 192 193 } 194 | Popular Tags |