org.springframework.util.TypeUtils Java Examples

The following examples show how to use org.springframework.util.TypeUtils. 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: AspectJAfterReturningAdvice.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Following AspectJ semantics, if a returning clause was specified, then the
 * advice is only invoked if the returned value is an instance of the given
 * returning type and generic type parameters, if any, match the assignment
 * rules. If the returning type is Object, the advice is *always* invoked.
 * @param returnValue the return value of the target method
 * @return whether to invoke the advice method for the given return value
 */
private boolean shouldInvokeOnReturnValueOf(Method method, @Nullable Object returnValue) {
	Class<?> type = getDiscoveredReturningType();
	Type genericType = getDiscoveredReturningGenericType();
	// If we aren't dealing with a raw type, check if generic parameters are assignable.
	return (matchesReturnValue(type, method, returnValue) &&
			(genericType == null || genericType == type ||
					TypeUtils.isAssignable(genericType, method.getGenericReturnType())));
}
 
Example #2
Source File: AspectJAfterReturningAdvice.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Following AspectJ semantics, if a returning clause was specified, then the
 * advice is only invoked if the returned value is an instance of the given
 * returning type and generic type parameters, if any, match the assignment
 * rules. If the returning type is Object, the advice is *always* invoked.
 * @param returnValue the return value of the target method
 * @return whether to invoke the advice method for the given return value
 */
private boolean shouldInvokeOnReturnValueOf(Method method, @Nullable Object returnValue) {
	Class<?> type = getDiscoveredReturningType();
	Type genericType = getDiscoveredReturningGenericType();
	// If we aren't dealing with a raw type, check if generic parameters are assignable.
	return (matchesReturnValue(type, method, returnValue) &&
			(genericType == null || genericType == type ||
					TypeUtils.isAssignable(genericType, method.getGenericReturnType())));
}
 
Example #3
Source File: AspectJAfterReturningAdvice.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Following AspectJ semantics, if a returning clause was specified, then the
 * advice is only invoked if the returned value is an instance of the given
 * returning type and generic type parameters, if any, match the assignment
 * rules. If the returning type is Object, the advice is *always* invoked.
 * @param returnValue the return value of the target method
 * @return whether to invoke the advice method for the given return value
 */
private boolean shouldInvokeOnReturnValueOf(Method method, Object returnValue) {
	Class<?> type = getDiscoveredReturningType();
	Type genericType = getDiscoveredReturningGenericType();
	// If we aren't dealing with a raw type, check if generic parameters are assignable.
	return (matchesReturnValue(type, method, returnValue) &&
			(genericType == null || genericType == type ||
					TypeUtils.isAssignable(genericType, method.getGenericReturnType())));
}
 
Example #4
Source File: DatastoreTemplate.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private void validateKey(Object entity, PathElement ancestorPE) {
	DatastorePersistentEntity datastorePersistentEntity =
			this.datastoreMappingContext.getPersistentEntity(entity.getClass());
	DatastorePersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();

	if (!TypeUtils.isAssignable(BaseKey.class, idProp.getType())) {
		throw new DatastoreDataException("Only Key types are allowed for descendants id");
	}

	Key key = getKey(entity, false);
	if (key == null || key.getAncestors().stream().anyMatch((pe) -> pe.equals(ancestorPE))) {
		return;
	}
	throw new DatastoreDataException("Descendant object has a key without current ancestor");
}
 
Example #5
Source File: AspectJAfterReturningAdvice.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Following AspectJ semantics, if a returning clause was specified, then the
 * advice is only invoked if the returned value is an instance of the given
 * returning type and generic type parameters, if any, match the assignment
 * rules. If the returning type is Object, the advice is *always* invoked.
 * @param returnValue the return value of the target method
 * @return whether to invoke the advice method for the given return value
 */
private boolean shouldInvokeOnReturnValueOf(Method method, Object returnValue) {
	Class<?> type = getDiscoveredReturningType();
	Type genericType = getDiscoveredReturningGenericType();
	// If we aren't dealing with a raw type, check if generic parameters are assignable.
	return (matchesReturnValue(type, method, returnValue) &&
			(genericType == null || genericType == type ||
					TypeUtils.isAssignable(genericType, method.getGenericReturnType())));
}
 
Example #6
Source File: AbstractJackson2HttpMessageConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	JsonEncoding encoding = getJsonEncoding(contentType);
	JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
	try {
		writePrefix(generator, object);

		Class<?> serializationView = null;
		FilterProvider filters = null;
		Object value = object;
		JavaType javaType = null;
		if (object instanceof MappingJacksonValue) {
			MappingJacksonValue container = (MappingJacksonValue) object;
			value = container.getValue();
			serializationView = container.getSerializationView();
			filters = container.getFilters();
		}
		if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
			javaType = getJavaType(type, null);
		}
		ObjectWriter objectWriter;
		if (serializationView != null) {
			objectWriter = this.objectMapper.writerWithView(serializationView);
		}
		else if (filters != null) {
			objectWriter = this.objectMapper.writer(filters);
		}
		else {
			objectWriter = this.objectMapper.writer();
		}
		if (javaType != null && javaType.isContainerType()) {
			objectWriter = objectWriter.forType(javaType);
		}
		SerializationConfig config = objectWriter.getConfig();
		if (contentType != null && contentType.isCompatibleWith(TEXT_EVENT_STREAM) &&
				config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
			objectWriter = objectWriter.with(this.ssePrettyPrinter);
		}
		objectWriter.writeValue(generator, value);

		writeSuffix(generator, object);
		generator.flush();

	}
	catch (JsonProcessingException ex) {
		throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getOriginalMessage(), ex);
	}
}
 
Example #7
Source File: AbstractJackson2HttpMessageConverter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
	JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
	try {
		writePrefix(generator, object);

		Class<?> serializationView = null;
		FilterProvider filters = null;
		Object value = object;
		JavaType javaType = null;
		if (object instanceof MappingJacksonValue) {
			MappingJacksonValue container = (MappingJacksonValue) object;
			value = container.getValue();
			serializationView = container.getSerializationView();
			filters = container.getFilters();
		}
		if (jackson26Available && type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
			javaType = getJavaType(type, null);
		}
		ObjectWriter objectWriter;
		if (serializationView != null) {
			objectWriter = this.objectMapper.writerWithView(serializationView);
		}
		else if (filters != null) {
			objectWriter = this.objectMapper.writer(filters);
		}
		else {
			objectWriter = this.objectMapper.writer();
		}
		if (javaType != null && javaType.isContainerType()) {
			objectWriter = objectWriter.withType(javaType);
		}
		objectWriter.writeValue(generator, value);

		writeSuffix(generator, object);
		generator.flush();

	}
	catch (JsonProcessingException ex) {
		throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
	}
}