Java Code Examples for org.springframework.util.ObjectUtils#unwrapOptional()

The following examples show how to use org.springframework.util.ObjectUtils#unwrapOptional() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractNestablePropertyAccessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Switch the target object, replacing the cached introspection results only
 * if the class of the new object is different to that of the replaced object.
 * @param object the new target object
 * @param nestedPath the nested path of the object
 * @param rootObject the root object at the top of the path
 */
public void setWrappedInstance(Object object, @Nullable String nestedPath, @Nullable Object rootObject) {
	this.wrappedObject = ObjectUtils.unwrapOptional(object);
	Assert.notNull(this.wrappedObject, "Target object must not be null");
	this.nestedPath = (nestedPath != null ? nestedPath : "");
	this.rootObject = (!this.nestedPath.isEmpty() ? rootObject : this.wrappedObject);
	this.nestedPropertyAccessors = null;
	this.typeConverterDelegate = new TypeConverterDelegate(this, this.wrappedObject);
}
 
Example 2
Source File: AbstractNestablePropertyAccessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Retrieve a Property accessor for the given nested property.
 * Create a new one if not found in the cache.
 * <p>Note: Caching nested PropertyAccessors is necessary now,
 * to keep registered custom editors for nested properties.
 * @param nestedProperty property to create the PropertyAccessor for
 * @return the PropertyAccessor instance, either cached or newly created
 */
private AbstractNestablePropertyAccessor getNestedPropertyAccessor(String nestedProperty) {
	if (this.nestedPropertyAccessors == null) {
		this.nestedPropertyAccessors = new HashMap<>();
	}
	// Get value of bean property.
	PropertyTokenHolder tokens = getPropertyNameTokens(nestedProperty);
	String canonicalName = tokens.canonicalName;
	Object value = getPropertyValue(tokens);
	if (value == null || (value instanceof Optional && !((Optional) value).isPresent())) {
		if (isAutoGrowNestedPaths()) {
			value = setDefaultValue(tokens);
		}
		else {
			throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + canonicalName);
		}
	}

	// Lookup cached sub-PropertyAccessor, create new one if not found.
	AbstractNestablePropertyAccessor nestedPa = this.nestedPropertyAccessors.get(canonicalName);
	if (nestedPa == null || nestedPa.getWrappedInstance() != ObjectUtils.unwrapOptional(value)) {
		if (logger.isTraceEnabled()) {
			logger.trace("Creating new nested " + getClass().getSimpleName() + " for property '" + canonicalName + "'");
		}
		nestedPa = newNestedPropertyAccessor(value, this.nestedPath + canonicalName + NESTED_PROPERTY_SEPARATOR);
		// Inherit all type-specific PropertyEditors.
		copyDefaultEditorsTo(nestedPa);
		copyCustomEditorsTo(nestedPa, canonicalName);
		this.nestedPropertyAccessors.put(canonicalName, nestedPa);
	}
	else {
		if (logger.isTraceEnabled()) {
			logger.trace("Using cached nested property accessor for property '" + canonicalName + "'");
		}
	}
	return nestedPa;
}
 
Example 3
Source File: AbstractNestablePropertyAccessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Switch the target object, replacing the cached introspection results only
 * if the class of the new object is different to that of the replaced object.
 * @param object the new target object
 * @param nestedPath the nested path of the object
 * @param rootObject the root object at the top of the path
 */
public void setWrappedInstance(Object object, @Nullable String nestedPath, @Nullable Object rootObject) {
	this.wrappedObject = ObjectUtils.unwrapOptional(object);
	Assert.notNull(this.wrappedObject, "Target object must not be null");
	this.nestedPath = (nestedPath != null ? nestedPath : "");
	this.rootObject = (!this.nestedPath.isEmpty() ? rootObject : this.wrappedObject);
	this.nestedPropertyAccessors = null;
	this.typeConverterDelegate = new TypeConverterDelegate(this, this.wrappedObject);
}
 
Example 4
Source File: AbstractNestablePropertyAccessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Retrieve a Property accessor for the given nested property.
 * Create a new one if not found in the cache.
 * <p>Note: Caching nested PropertyAccessors is necessary now,
 * to keep registered custom editors for nested properties.
 * @param nestedProperty property to create the PropertyAccessor for
 * @return the PropertyAccessor instance, either cached or newly created
 */
private AbstractNestablePropertyAccessor getNestedPropertyAccessor(String nestedProperty) {
	if (this.nestedPropertyAccessors == null) {
		this.nestedPropertyAccessors = new HashMap<>();
	}
	// Get value of bean property.
	PropertyTokenHolder tokens = getPropertyNameTokens(nestedProperty);
	String canonicalName = tokens.canonicalName;
	Object value = getPropertyValue(tokens);
	if (value == null || (value instanceof Optional && !((Optional) value).isPresent())) {
		if (isAutoGrowNestedPaths()) {
			value = setDefaultValue(tokens);
		}
		else {
			throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + canonicalName);
		}
	}

	// Lookup cached sub-PropertyAccessor, create new one if not found.
	AbstractNestablePropertyAccessor nestedPa = this.nestedPropertyAccessors.get(canonicalName);
	if (nestedPa == null || nestedPa.getWrappedInstance() != ObjectUtils.unwrapOptional(value)) {
		if (logger.isTraceEnabled()) {
			logger.trace("Creating new nested " + getClass().getSimpleName() + " for property '" + canonicalName + "'");
		}
		nestedPa = newNestedPropertyAccessor(value, this.nestedPath + canonicalName + NESTED_PROPERTY_SEPARATOR);
		// Inherit all type-specific PropertyEditors.
		copyDefaultEditorsTo(nestedPa);
		copyCustomEditorsTo(nestedPa, canonicalName);
		this.nestedPropertyAccessors.put(canonicalName, nestedPa);
	}
	else {
		if (logger.isTraceEnabled()) {
			logger.trace("Using cached nested property accessor for property '" + canonicalName + "'");
		}
	}
	return nestedPa;
}
 
Example 5
Source File: CacheAspectSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Nullable
private Object unwrapReturnValue(Object returnValue) {
	return ObjectUtils.unwrapOptional(returnValue);
}
 
Example 6
Source File: CacheAspectSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Nullable
private Object unwrapReturnValue(Object returnValue) {
	return ObjectUtils.unwrapOptional(returnValue);
}
 
Example 7
Source File: DataBinder.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a new DataBinder instance.
 * @param target the target object to bind onto (or {@code null}
 * if the binder is just used to convert a plain parameter value)
 * @param objectName the name of the target object
 */
public DataBinder(@Nullable Object target, String objectName) {
	this.target = ObjectUtils.unwrapOptional(target);
	this.objectName = objectName;
}
 
Example 8
Source File: DataBinder.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Create a new DataBinder instance.
 * @param target the target object to bind onto (or {@code null}
 * if the binder is just used to convert a plain parameter value)
 * @param objectName the name of the target object
 */
public DataBinder(@Nullable Object target, String objectName) {
	this.target = ObjectUtils.unwrapOptional(target);
	this.objectName = objectName;
}