Java Code Examples for org.springframework.util.ClassUtils#getShortNameAsProperty()

The following examples show how to use org.springframework.util.ClassUtils#getShortNameAsProperty() . 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: Conventions.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determine the conventional variable name for the supplied
 * {@code Object} based on its concrete type. The convention
 * used is to return the uncapitalized short name of the {@code Class},
 * according to JavaBeans property naming rules: So,
 * {@code com.myapp.Product} becomes {@code product};
 * {@code com.myapp.MyProduct} becomes {@code myProduct};
 * {@code com.myapp.UKProduct} becomes {@code UKProduct}.
 * <p>For arrays, we use the pluralized version of the array component type.
 * For {@code Collection}s we attempt to 'peek ahead' in the
 * {@code Collection} to determine the component type and
 * return the pluralized version of that component type.
 * @param value the value to generate a variable name for
 * @return the generated variable name
 */
public static String getVariableName(Object value) {
	Assert.notNull(value, "Value must not be null");
	Class<?> valueClass;
	boolean pluralize = false;

	if (value.getClass().isArray()) {
		valueClass = value.getClass().getComponentType();
		pluralize = true;
	}
	else if (value instanceof Collection) {
		Collection<?> collection = (Collection<?>) value;
		if (collection.isEmpty()) {
			throw new IllegalArgumentException("Cannot generate variable name for an empty Collection");
		}
		Object valueToCheck = peekAhead(collection);
		valueClass = getClassForValue(valueToCheck);
		pluralize = true;
	}
	else {
		valueClass = getClassForValue(value);
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}
 
Example 2
Source File: Conventions.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determine the conventional variable name for the supplied parameter,
 * taking the generic collection type (if any) into account.
 * @param parameter the method or constructor parameter to generate a variable name for
 * @return the generated variable name
 */
public static String getVariableNameForParameter(MethodParameter parameter) {
	Assert.notNull(parameter, "MethodParameter must not be null");
	Class<?> valueClass;
	boolean pluralize = false;

	if (parameter.getParameterType().isArray()) {
		valueClass = parameter.getParameterType().getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(parameter.getParameterType())) {
		valueClass = ResolvableType.forMethodParameter(parameter).asCollection().resolveGeneric();
		if (valueClass == null) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for non-typed Collection parameter type");
		}
		pluralize = true;
	}
	else {
		valueClass = parameter.getParameterType();
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}
 
Example 3
Source File: Conventions.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the conventional variable name for the supplied
 * {@code Object} based on its concrete type. The convention
 * used is to return the uncapitalized short name of the {@code Class},
 * according to JavaBeans property naming rules: So,
 * {@code com.myapp.Product} becomes {@code product};
 * {@code com.myapp.MyProduct} becomes {@code myProduct};
 * {@code com.myapp.UKProduct} becomes {@code UKProduct}.
 * <p>For arrays, we use the pluralized version of the array component type.
 * For {@code Collection}s we attempt to 'peek ahead' in the
 * {@code Collection} to determine the component type and
 * return the pluralized version of that component type.
 * @param value the value to generate a variable name for
 * @return the generated variable name
 */
public static String getVariableName(Object value) {
	Assert.notNull(value, "Value must not be null");
	Class<?> valueClass;
	boolean pluralize = false;

	if (value.getClass().isArray()) {
		valueClass = value.getClass().getComponentType();
		pluralize = true;
	}
	else if (value instanceof Collection) {
		Collection<?> collection = (Collection<?>) value;
		if (collection.isEmpty()) {
			throw new IllegalArgumentException("Cannot generate variable name for an empty Collection");
		}
		Object valueToCheck = peekAhead(collection);
		valueClass = getClassForValue(valueToCheck);
		pluralize = true;
	}
	else {
		valueClass = getClassForValue(value);
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}
 
Example 4
Source File: Conventions.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the conventional variable name for the supplied parameter,
 * taking the generic collection type (if any) into account.
 * @param parameter the method or constructor parameter to generate a variable name for
 * @return the generated variable name
 */
public static String getVariableNameForParameter(MethodParameter parameter) {
	Assert.notNull(parameter, "MethodParameter must not be null");
	Class<?> valueClass;
	boolean pluralize = false;

	if (parameter.getParameterType().isArray()) {
		valueClass = parameter.getParameterType().getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(parameter.getParameterType())) {
		valueClass = GenericCollectionTypeResolver.getCollectionParameterType(parameter);
		if (valueClass == null) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for non-typed Collection parameter type");
		}
		pluralize = true;
	}
	else {
		valueClass = parameter.getParameterType();
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}
 
Example 5
Source File: MethodMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private TestMethodMessageHandler initMethodMessageHandler(
		Consumer<TestMethodMessageHandler> customizer, Class<?>... handlerTypes) {

	StaticApplicationContext context = new StaticApplicationContext();
	for (Class<?> handlerType : handlerTypes) {
		String beanName = ClassUtils.getShortNameAsProperty(handlerType);
		context.registerPrototype(beanName, handlerType);
	}
	TestMethodMessageHandler messageHandler = new TestMethodMessageHandler();
	messageHandler.setApplicationContext(context);
	customizer.accept(messageHandler);
	messageHandler.afterPropertiesSet();
	return messageHandler;
}
 
Example 6
Source File: Conventions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine the conventional variable name for the supplied {@code Object}
 * based on its concrete type. The convention used is to return the
 * un-capitalized short name of the {@code Class}, according to JavaBeans
 * property naming rules.
 * <p>For example:<br>
 * {@code com.myapp.Product} becomes {@code "product"}<br>
 * {@code com.myapp.MyProduct} becomes {@code "myProduct"}<br>
 * {@code com.myapp.UKProduct} becomes {@code "UKProduct"}<br>
 * <p>For arrays the pluralized version of the array component type is used.
 * For {@code Collection}s an attempt is made to 'peek ahead' to determine
 * the component type and return its pluralized version.
 * @param value the value to generate a variable name for
 * @return the generated variable name
 */
public static String getVariableName(Object value) {
	Assert.notNull(value, "Value must not be null");
	Class<?> valueClass;
	boolean pluralize = false;

	if (value.getClass().isArray()) {
		valueClass = value.getClass().getComponentType();
		pluralize = true;
	}
	else if (value instanceof Collection) {
		Collection<?> collection = (Collection<?>) value;
		if (collection.isEmpty()) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for an empty Collection");
		}
		Object valueToCheck = peekAhead(collection);
		valueClass = getClassForValue(valueToCheck);
		pluralize = true;
	}
	else {
		valueClass = getClassForValue(value);
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}
 
Example 7
Source File: Conventions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine the conventional variable name for the given parameter taking
 * the generic collection type, if any, into account.
 * <p>As of 5.0 this method supports reactive types:<br>
 * {@code Mono<com.myapp.Product>} becomes {@code "productMono"}<br>
 * {@code Flux<com.myapp.MyProduct>} becomes {@code "myProductFlux"}<br>
 * {@code Observable<com.myapp.MyProduct>} becomes {@code "myProductObservable"}<br>
 * @param parameter the method or constructor parameter
 * @return the generated variable name
 */
public static String getVariableNameForParameter(MethodParameter parameter) {
	Assert.notNull(parameter, "MethodParameter must not be null");
	Class<?> valueClass;
	boolean pluralize = false;
	String reactiveSuffix = "";

	if (parameter.getParameterType().isArray()) {
		valueClass = parameter.getParameterType().getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(parameter.getParameterType())) {
		valueClass = ResolvableType.forMethodParameter(parameter).asCollection().resolveGeneric();
		if (valueClass == null) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for non-typed Collection parameter type");
		}
		pluralize = true;
	}
	else {
		valueClass = parameter.getParameterType();
		ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(valueClass);
		if (adapter != null && !adapter.getDescriptor().isNoValue()) {
			reactiveSuffix = ClassUtils.getShortName(valueClass);
			valueClass = parameter.nested().getNestedParameterType();
		}
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name + reactiveSuffix);
}
 
Example 8
Source File: Conventions.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine the conventional variable name for the supplied {@code Object}
 * based on its concrete type. The convention used is to return the
 * un-capitalized short name of the {@code Class}, according to JavaBeans
 * property naming rules.
 * <p>For example:<br>
 * {@code com.myapp.Product} becomes {@code "product"}<br>
 * {@code com.myapp.MyProduct} becomes {@code "myProduct"}<br>
 * {@code com.myapp.UKProduct} becomes {@code "UKProduct"}<br>
 * <p>For arrays the pluralized version of the array component type is used.
 * For {@code Collection}s an attempt is made to 'peek ahead' to determine
 * the component type and return its pluralized version.
 * @param value the value to generate a variable name for
 * @return the generated variable name
 */
public static String getVariableName(Object value) {
	Assert.notNull(value, "Value must not be null");
	Class<?> valueClass;
	boolean pluralize = false;

	if (value.getClass().isArray()) {
		valueClass = value.getClass().getComponentType();
		pluralize = true;
	}
	else if (value instanceof Collection) {
		Collection<?> collection = (Collection<?>) value;
		if (collection.isEmpty()) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for an empty Collection");
		}
		Object valueToCheck = peekAhead(collection);
		valueClass = getClassForValue(valueToCheck);
		pluralize = true;
	}
	else {
		valueClass = getClassForValue(value);
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}
 
Example 9
Source File: Conventions.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine the conventional variable name for the given parameter taking
 * the generic collection type, if any, into account.
 * <p>As of 5.0 this method supports reactive types:<br>
 * {@code Mono<com.myapp.Product>} becomes {@code "productMono"}<br>
 * {@code Flux<com.myapp.MyProduct>} becomes {@code "myProductFlux"}<br>
 * {@code Observable<com.myapp.MyProduct>} becomes {@code "myProductObservable"}<br>
 * @param parameter the method or constructor parameter
 * @return the generated variable name
 */
public static String getVariableNameForParameter(MethodParameter parameter) {
	Assert.notNull(parameter, "MethodParameter must not be null");
	Class<?> valueClass;
	boolean pluralize = false;
	String reactiveSuffix = "";

	if (parameter.getParameterType().isArray()) {
		valueClass = parameter.getParameterType().getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(parameter.getParameterType())) {
		valueClass = ResolvableType.forMethodParameter(parameter).asCollection().resolveGeneric();
		if (valueClass == null) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for non-typed Collection parameter type");
		}
		pluralize = true;
	}
	else {
		valueClass = parameter.getParameterType();
		ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
		if (reactiveAdapterRegistry.hasAdapters()) {
			ReactiveAdapter adapter = reactiveAdapterRegistry.getAdapter(valueClass);
			if (adapter != null && !adapter.getDescriptor().isNoValue()) {
				reactiveSuffix = ClassUtils.getShortName(valueClass);
				valueClass = parameter.nested().getNestedParameterType();
			}
		}
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name + reactiveSuffix);
}
 
Example 10
Source File: SocketBeanConfigurator.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
	T endpoint = null;
	ApplicationContext appContext = Springs.getInstance().getAppContext();
	Component ann = AnnotationUtils.findAnnotation(endpointClass, Component.class);
	if (ann != null && appContext.containsBean(ann.value())) {
		endpoint = appContext.getBean(ann.value(), endpointClass);
		return endpoint;
	}
	
	Map<String, T> socketEndpointMap = Springs.getInstance().getBeansMap(endpointClass);
	if(LangUtils.isEmpty(socketEndpointMap)){
		endpoint = ReflectUtils.newInstance(endpointClass);
		SpringUtils.injectAndInitialize(appContext, ReflectUtils.newInstance(endpointClass));
		return endpoint;
	}
	if(socketEndpointMap.size()==1){
		endpoint = LangUtils.getFirst(socketEndpointMap);
	}else{
		String beanName = ClassUtils.getShortNameAsProperty(endpointClass);
		if (socketEndpointMap.containsKey(beanName)) {
			endpoint = socketEndpointMap.get(beanName);
			return endpoint;
		}
	}
	return endpoint;
}
 
Example 11
Source File: JseHandlerInterceptor.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
private String[] convertToStringArray(Class<?>[] types) {
    if (!Arrays.isEmpty(types)) {
        String[] converted = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            converted[i] = ClassUtils.getShortNameAsProperty(types[i]);
        }
        return converted;
    }
    return null;
}
 
Example 12
Source File: Conventions.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Determine the conventional variable name for the return type of the given
 * method, taking the generic collection type, if any, into account, falling
 * back on the given return value if the method declaration is not specific
 * enough, e.g. {@code Object} return type or untyped collection.
 * <p>As of 5.0 this method supports reactive types:<br>
 * {@code Mono<com.myapp.Product>} becomes {@code "productMono"}<br>
 * {@code Flux<com.myapp.MyProduct>} becomes {@code "myProductFlux"}<br>
 * {@code Observable<com.myapp.MyProduct>} becomes {@code "myProductObservable"}<br>
 * @param method the method to generate a variable name for
 * @param resolvedType the resolved return type of the method
 * @param value the return value (may be {@code null} if not available)
 * @return the generated variable name
 */
public static String getVariableNameForReturnType(Method method, Class<?> resolvedType, @Nullable Object value) {
	Assert.notNull(method, "Method must not be null");

	if (Object.class == resolvedType) {
		if (value == null) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for an Object return type with null value");
		}
		return getVariableName(value);
	}

	Class<?> valueClass;
	boolean pluralize = false;
	String reactiveSuffix = "";

	if (resolvedType.isArray()) {
		valueClass = resolvedType.getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(resolvedType)) {
		valueClass = ResolvableType.forMethodReturnType(method).asCollection().resolveGeneric();
		if (valueClass == null) {
			if (!(value instanceof Collection)) {
				throw new IllegalArgumentException("Cannot generate variable name " +
						"for non-typed Collection return type and a non-Collection value");
			}
			Collection<?> collection = (Collection<?>) value;
			if (collection.isEmpty()) {
				throw new IllegalArgumentException("Cannot generate variable name " +
						"for non-typed Collection return type and an empty Collection value");
			}
			Object valueToCheck = peekAhead(collection);
			valueClass = getClassForValue(valueToCheck);
		}
		pluralize = true;
	}
	else {
		valueClass = resolvedType;
		ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(valueClass);
		if (adapter != null && !adapter.getDescriptor().isNoValue()) {
			reactiveSuffix = ClassUtils.getShortName(valueClass);
			valueClass = ResolvableType.forMethodReturnType(method).getGeneric().toClass();
		}
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name + reactiveSuffix);
}
 
Example 13
Source File: Conventions.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Determine the conventional variable name for the return type of the given
 * method, taking the generic collection type, if any, into account, falling
 * back on the given return value if the method declaration is not specific
 * enough, e.g. {@code Object} return type or untyped collection.
 * <p>As of 5.0 this method supports reactive types:<br>
 * {@code Mono<com.myapp.Product>} becomes {@code "productMono"}<br>
 * {@code Flux<com.myapp.MyProduct>} becomes {@code "myProductFlux"}<br>
 * {@code Observable<com.myapp.MyProduct>} becomes {@code "myProductObservable"}<br>
 * @param method the method to generate a variable name for
 * @param resolvedType the resolved return type of the method
 * @param value the return value (may be {@code null} if not available)
 * @return the generated variable name
 */
public static String getVariableNameForReturnType(Method method, Class<?> resolvedType, @Nullable Object value) {
	Assert.notNull(method, "Method must not be null");

	if (Object.class == resolvedType) {
		if (value == null) {
			throw new IllegalArgumentException(
					"Cannot generate variable name for an Object return type with null value");
		}
		return getVariableName(value);
	}

	Class<?> valueClass;
	boolean pluralize = false;
	String reactiveSuffix = "";

	if (resolvedType.isArray()) {
		valueClass = resolvedType.getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(resolvedType)) {
		valueClass = ResolvableType.forMethodReturnType(method).asCollection().resolveGeneric();
		if (valueClass == null) {
			if (!(value instanceof Collection)) {
				throw new IllegalArgumentException("Cannot generate variable name " +
						"for non-typed Collection return type and a non-Collection value");
			}
			Collection<?> collection = (Collection<?>) value;
			if (collection.isEmpty()) {
				throw new IllegalArgumentException("Cannot generate variable name " +
						"for non-typed Collection return type and an empty Collection value");
			}
			Object valueToCheck = peekAhead(collection);
			valueClass = getClassForValue(valueToCheck);
		}
		pluralize = true;
	}
	else {
		valueClass = resolvedType;
		ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
		if (reactiveAdapterRegistry.hasAdapters()) {
			ReactiveAdapter adapter = reactiveAdapterRegistry.getAdapter(valueClass);
			if (adapter != null && !adapter.getDescriptor().isNoValue()) {
				reactiveSuffix = ClassUtils.getShortName(valueClass);
				valueClass = ResolvableType.forMethodReturnType(method).getGeneric().toClass();
			}
		}
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name + reactiveSuffix);
}
 
Example 14
Source File: Conventions.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determine the conventional variable name for the return type of the supplied method,
 * taking the generic collection type (if any) into account, falling back to the
 * given return value if the method declaration is not specific enough (i.e. in case of
 * the return type being declared as {@code Object} or as untyped collection).
 * @param method the method to generate a variable name for
 * @param resolvedType the resolved return type of the method
 * @param value the return value (may be {@code null} if not available)
 * @return the generated variable name
 */
public static String getVariableNameForReturnType(Method method, Class<?> resolvedType, Object value) {
	Assert.notNull(method, "Method must not be null");

	if (Object.class == resolvedType) {
		if (value == null) {
			throw new IllegalArgumentException("Cannot generate variable name for an Object return type with null value");
		}
		return getVariableName(value);
	}

	Class<?> valueClass;
	boolean pluralize = false;

	if (resolvedType.isArray()) {
		valueClass = resolvedType.getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(resolvedType)) {
		valueClass = ResolvableType.forMethodReturnType(method).asCollection().resolveGeneric();
		if (valueClass == null) {
			if (!(value instanceof Collection)) {
				throw new IllegalArgumentException(
						"Cannot generate variable name for non-typed Collection return type and a non-Collection value");
			}
			Collection<?> collection = (Collection<?>) value;
			if (collection.isEmpty()) {
				throw new IllegalArgumentException(
						"Cannot generate variable name for non-typed Collection return type and an empty Collection value");
			}
			Object valueToCheck = peekAhead(collection);
			valueClass = getClassForValue(valueToCheck);
		}
		pluralize = true;
	}
	else {
		valueClass = resolvedType;
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}
 
Example 15
Source File: Conventions.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the conventional variable name for the return type of the supplied method,
 * taking the generic collection type (if any) into account, falling back to the
 * given return value if the method declaration is not specific enough (i.e. in case of
 * the return type being declared as {@code Object} or as untyped collection).
 * @param method the method to generate a variable name for
 * @param resolvedType the resolved return type of the method
 * @param value the return value (may be {@code null} if not available)
 * @return the generated variable name
 */
public static String getVariableNameForReturnType(Method method, Class<?> resolvedType, Object value) {
	Assert.notNull(method, "Method must not be null");

	if (Object.class == resolvedType) {
		if (value == null) {
			throw new IllegalArgumentException("Cannot generate variable name for an Object return type with null value");
		}
		return getVariableName(value);
	}

	Class<?> valueClass;
	boolean pluralize = false;

	if (resolvedType.isArray()) {
		valueClass = resolvedType.getComponentType();
		pluralize = true;
	}
	else if (Collection.class.isAssignableFrom(resolvedType)) {
		valueClass = GenericCollectionTypeResolver.getCollectionReturnType(method);
		if (valueClass == null) {
			if (!(value instanceof Collection)) {
				throw new IllegalArgumentException(
						"Cannot generate variable name for non-typed Collection return type and a non-Collection value");
			}
			Collection<?> collection = (Collection<?>) value;
			if (collection.isEmpty()) {
				throw new IllegalArgumentException(
						"Cannot generate variable name for non-typed Collection return type and an empty Collection value");
			}
			Object valueToCheck = peekAhead(collection);
			valueClass = getClassForValue(valueToCheck);
		}
		pluralize = true;
	}
	else {
		valueClass = resolvedType;
	}

	String name = ClassUtils.getShortNameAsProperty(valueClass);
	return (pluralize ? pluralize(name) : name);
}