TypedStringValue.java 6.3 KB
Newer Older
1
/*
2
 * Copyright 2002-2017 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory.config;

import org.springframework.beans.BeanMetadataElement;
20
import org.springframework.lang.Nullable;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;

/**
 * Holder for a typed String value. Can be added to bean definitions
 * in order to explicitly specify a target type for a String value,
 * for example for collection elements.
 *
 * <p>This holder will just store the String value and the target type.
 * The actual conversion will be performed by the bean factory.
 *
 * @author Juergen Hoeller
 * @since 1.2
 * @see BeanDefinition#getPropertyValues
 * @see org.springframework.beans.MutablePropertyValues#addPropertyValue
 */
public class TypedStringValue implements BeanMetadataElement {

40
	@Nullable
41 42
	private String value;

43
	@Nullable
44
	private volatile Object targetType;
45

46
	@Nullable
47 48
	private Object source;

49
	@Nullable
50 51
	private String specifiedTypeName;

52 53
	private volatile boolean dynamic;

54 55 56 57 58

	/**
	 * Create a new {@link TypedStringValue} for the given String value.
	 * @param value the String value
	 */
59
	public TypedStringValue(@Nullable String value) {
60 61 62 63 64 65 66 67 68
		setValue(value);
	}

	/**
	 * Create a new {@link TypedStringValue} for the given String value
	 * and target type.
	 * @param value the String value
	 * @param targetType the type to convert to
	 */
69
	public TypedStringValue(@Nullable String value, Class<?> targetType) {
70 71 72 73 74 75 76 77 78 79
		setValue(value);
		setTargetType(targetType);
	}

	/**
	 * Create a new {@link TypedStringValue} for the given String value
	 * and target type.
	 * @param value the String value
	 * @param targetTypeName the type to convert to
	 */
80
	public TypedStringValue(@Nullable String value, String targetTypeName) {
81 82 83 84 85 86 87 88 89 90 91
		setValue(value);
		setTargetTypeName(targetTypeName);
	}


	/**
	 * Set the String value.
	 * <p>Only necessary for manipulating a registered value,
	 * for example in BeanFactoryPostProcessors.
	 * @see PropertyPlaceholderConfigurer
	 */
92
	public void setValue(@Nullable String value) {
93 94 95 96 97 98
		this.value = value;
	}

	/**
	 * Return the String value.
	 */
99
	@Nullable
100 101 102 103 104 105 106 107 108 109
	public String getValue() {
		return this.value;
	}

	/**
	 * Set the type to convert to.
	 * <p>Only necessary for manipulating a registered value,
	 * for example in BeanFactoryPostProcessors.
	 * @see PropertyPlaceholderConfigurer
	 */
110
	public void setTargetType(Class<?> targetType) {
111 112 113 114 115 116 117
		Assert.notNull(targetType, "'targetType' must not be null");
		this.targetType = targetType;
	}

	/**
	 * Return the type to convert to.
	 */
J
Juergen Hoeller 已提交
118
	public Class<?> getTargetType() {
119 120
		Object targetTypeValue = this.targetType;
		if (!(targetTypeValue instanceof Class)) {
121 122
			throw new IllegalStateException("Typed String value does not carry a resolved target type");
		}
P
Phillip Webb 已提交
123
		return (Class<?>) targetTypeValue;
124 125 126 127 128
	}

	/**
	 * Specify the type to convert to.
	 */
129
	public void setTargetTypeName(@Nullable String targetTypeName) {
130 131 132 133 134 135
		this.targetType = targetTypeName;
	}

	/**
	 * Return the type to convert to.
	 */
136
	@Nullable
137
	public String getTargetTypeName() {
138 139
		Object targetTypeValue = this.targetType;
		if (targetTypeValue instanceof Class) {
P
Phillip Webb 已提交
140
			return ((Class<?>) targetTypeValue).getName();
141 142
		}
		else {
143
			return (String) targetTypeValue;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		}
	}

	/**
	 * Return whether this typed String value carries a target type .
	 */
	public boolean hasTargetType() {
		return (this.targetType instanceof Class);
	}

	/**
	 * Determine the type to convert to, resolving it from a specified class name
	 * if necessary. Will also reload a specified Class from its name when called
	 * with the target type already resolved.
	 * @param classLoader the ClassLoader to use for resolving a (potential) class name
	 * @return the resolved type to convert to
	 * @throws ClassNotFoundException if the type cannot be resolved
	 */
162
	@Nullable
163
	public Class<?> resolveTargetType(@Nullable ClassLoader classLoader) throws ClassNotFoundException {
164 165
		String typeName = getTargetTypeName();
		if (typeName == null) {
166 167
			return null;
		}
168
		Class<?> resolvedClass = ClassUtils.forName(typeName, classLoader);
169 170 171 172 173 174
		this.targetType = resolvedClass;
		return resolvedClass;
	}


	/**
175
	 * Set the configuration source {@code Object} for this metadata element.
176 177
	 * <p>The exact type of the object will depend on the configuration mechanism used.
	 */
178
	public void setSource(@Nullable Object source) {
179 180 181
		this.source = source;
	}

182
	@Override
183
	@Nullable
184 185 186 187
	public Object getSource() {
		return this.source;
	}

188 189 190
	/**
	 * Set the type name as actually specified for this particular value, if any.
	 */
191
	public void setSpecifiedTypeName(@Nullable String specifiedTypeName) {
192 193 194 195 196 197
		this.specifiedTypeName = specifiedTypeName;
	}

	/**
	 * Return the type name as actually specified for this particular value, if any.
	 */
198
	@Nullable
199 200 201 202
	public String getSpecifiedTypeName() {
		return this.specifiedTypeName;
	}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	/**
	 * Mark this value as dynamic, i.e. as containing an expression
	 * and hence not being subject to caching.
	 */
	public void setDynamic() {
		this.dynamic = true;
	}

	/**
	 * Return whether this value has been marked as dynamic.
	 */
	public boolean isDynamic() {
		return this.dynamic;
	}

218

219
	@Override
220 221 222 223 224 225 226 227 228 229 230 231
	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof TypedStringValue)) {
			return false;
		}
		TypedStringValue otherValue = (TypedStringValue) other;
		return (ObjectUtils.nullSafeEquals(this.value, otherValue.value) &&
				ObjectUtils.nullSafeEquals(this.targetType, otherValue.targetType));
	}

232
	@Override
233 234 235 236
	public int hashCode() {
		return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.targetType);
	}

237
	@Override
238 239 240 241 242
	public String toString() {
		return "TypedStringValue: value [" + this.value + "], target type [" + this.targetType + "]";
	}

}