List of usage examples for org.springframework.util ObjectUtils nullSafeClassName
public static String nullSafeClassName(@Nullable Object obj)
From source file:org.spring.data.gemfire.cache.ClientCacheFunctionExecutionTest.java
@Test public void testRegionSizeOnRegionUsingSpring() { Object result = onRegionFunctions.regionSize(appDataProxy.getName()); assertTrue(String.format("Expected java.util.List; but was (%1$s)!", ObjectUtils.nullSafeClassName(result)), result instanceof List); assertFalse(((List) result).isEmpty()); assertEquals(EXPECTED_REGION_SIZE, ((List) result).get(0)); }
From source file:org.spring.data.gemfire.cache.PeerCacheFunctionCreationExecutionTest.java
@Test public void testRegionSizeOnRegionUsingSpring() { Object results = onRegionFunctions.regionSize(appData.getName()); // NOTE reviewing the Spring Data GemFire source code, this is expected but a bit surprising... assertTrue(/*ww w . j a v a 2 s .co m*/ String.format("Expected java.util.List; but was (%1$s)!", ObjectUtils.nullSafeClassName(results)), results instanceof List); assertFalse(((List) results).isEmpty()); assertEquals(EXPECTED_REGION_SIZE, ((List) results).get(0)); }
From source file:org.springframework.beans.factory.support.ConstructorResolver.java
/** * Create an array of arguments to invoke a constructor or factory method, * given the resolved constructor argument values. *//*from www. j a va 2s.c o m*/ private ArgumentsHolder createArgumentArray(String beanName, RootBeanDefinition mbd, @Nullable ConstructorArgumentValues resolvedValues, BeanWrapper bw, Class<?>[] paramTypes, @Nullable String[] paramNames, Executable executable, boolean autowiring, boolean fallback) throws UnsatisfiedDependencyException { TypeConverter customConverter = this.beanFactory.getCustomTypeConverter(); TypeConverter converter = (customConverter != null ? customConverter : bw); ArgumentsHolder args = new ArgumentsHolder(paramTypes.length); Set<ConstructorArgumentValues.ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length); Set<String> autowiredBeanNames = new LinkedHashSet<>(4); for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) { Class<?> paramType = paramTypes[paramIndex]; String paramName = (paramNames != null ? paramNames[paramIndex] : ""); // Try to find matching constructor argument value, either indexed or generic. ConstructorArgumentValues.ValueHolder valueHolder = null; if (resolvedValues != null) { valueHolder = resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders); // If we couldn't find a direct match and are not supposed to autowire, // let's try the next generic, untyped argument value as fallback: // it could match after type conversion (for example, String -> int). if (valueHolder == null && (!autowiring || paramTypes.length == resolvedValues.getArgumentCount())) { valueHolder = resolvedValues.getGenericArgumentValue(null, null, usedValueHolders); } } if (valueHolder != null) { // We found a potential match - let's give it a try. // Do not consider the same value definition multiple times! usedValueHolders.add(valueHolder); Object originalValue = valueHolder.getValue(); Object convertedValue; if (valueHolder.isConverted()) { convertedValue = valueHolder.getConvertedValue(); args.preparedArguments[paramIndex] = convertedValue; } else { MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex); try { convertedValue = converter.convertIfNecessary(originalValue, paramType, methodParam); } catch (TypeMismatchException ex) { throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(valueHolder.getValue()) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage()); } Object sourceHolder = valueHolder.getSource(); if (sourceHolder instanceof ConstructorArgumentValues.ValueHolder) { Object sourceValue = ((ConstructorArgumentValues.ValueHolder) sourceHolder).getValue(); args.resolveNecessary = true; args.preparedArguments[paramIndex] = sourceValue; } } args.arguments[paramIndex] = convertedValue; args.rawArguments[paramIndex] = originalValue; } else { MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex); // No explicit match found: we're either supposed to autowire or // have to fail creating an argument array for the given constructor. if (!autowiring) { throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Ambiguous argument values for parameter of type [" + paramType.getName() + "] - did you specify the correct bean references as arguments?"); } try { Object autowiredArgument = resolveAutowiredArgument(methodParam, beanName, autowiredBeanNames, converter, fallback); args.rawArguments[paramIndex] = autowiredArgument; args.arguments[paramIndex] = autowiredArgument; args.preparedArguments[paramIndex] = new AutowiredArgumentMarker(); args.resolveNecessary = true; } catch (BeansException ex) { throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam), ex); } } } for (String autowiredBeanName : autowiredBeanNames) { this.beanFactory.registerDependentBean(autowiredBeanName, beanName); if (logger.isDebugEnabled()) { logger.debug("Autowiring by type from bean name '" + beanName + "' via " + (executable instanceof Constructor ? "constructor" : "factory method") + " to bean named '" + autowiredBeanName + "'"); } } return args; }
From source file:org.springframework.beans.factory.support.ConstructorResolver.java
/** * Resolve the prepared arguments stored in the given bean definition. *///from w w w . j a v a 2s. co m private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw, Executable executable, Object[] argsToResolve, boolean fallback) { TypeConverter customConverter = this.beanFactory.getCustomTypeConverter(); TypeConverter converter = (customConverter != null ? customConverter : bw); BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter); Class<?>[] paramTypes = executable.getParameterTypes(); Object[] resolvedArgs = new Object[argsToResolve.length]; for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) { Object argValue = argsToResolve[argIndex]; MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex); GenericTypeResolver.resolveParameterType(methodParam, executable.getDeclaringClass()); if (argValue instanceof AutowiredArgumentMarker) { argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback); } else if (argValue instanceof BeanMetadataElement) { argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue); } else if (argValue instanceof String) { argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd); } Class<?> paramType = paramTypes[argIndex]; try { resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam); } catch (TypeMismatchException ex) { throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage()); } } return resolvedArgs; }
From source file:org.springframework.data.gemfire.config.DiskStoreBeanPostProcessor.java
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { if (log.isDebugEnabled()) { log.debug(String.format("Post Processing Bean (%1$s) of Type (%2$s) with Name (%3$s)...%n", bean, ObjectUtils.nullSafeClassName(bean), beanName)); }/*from w w w. j a v a2 s .c om*/ if (bean instanceof DiskDir) { createIfNotExists((DiskDir) bean); } return bean; }
From source file:org.springframework.data.gemfire.config.DiskStoreBeanPostProcessor.java
@SuppressWarnings("unchecked") private <T> T readField(final Object obj, final String fieldName) { try {//from ww w. ja v a2s . c o m Class type = obj.getClass(); Field field; do { field = type.getDeclaredField(fieldName); type = type.getSuperclass(); // traverse up the object class hierarchy } while (field == null && !Object.class.equals(type)); if (field == null) { throw new NoSuchFieldException( String.format("Field (%1$s) does not exist on Object of type (%2$s)!", fieldName, ObjectUtils.nullSafeClassName(obj))); } field.setAccessible(true); return (T) field.get(obj); } catch (Exception e) { throw new RuntimeException(String.format("Failed to read field (%1$s) on Object of type (%2$s)!", fieldName, ObjectUtils.nullSafeClassName(obj)), e); } }
From source file:org.springframework.data.gemfire.config.support.DiskStoreDirectoryBeanPostProcessor.java
/** * {@inheritDoc}/*from ww w . j a va2s .c om*/ */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (log.isDebugEnabled()) { log.debug( String.format("Processing Bean [%1$s] of Type [%2$s] with Name [%3$s] before initialization%n", bean, ObjectUtils.nullSafeClassName(bean), beanName)); } if (bean instanceof DiskDir) { createIfNotExists((DiskDir) bean); } return bean; }
From source file:org.springframework.data.gemfire.config.support.DiskStoreDirectoryBeanPostProcessor.java
@SuppressWarnings("unchecked") private <T> T readField(Object obj, String fieldName) { try {//from w w w. j a v a2 s . co m Class type = obj.getClass(); Field field; do { field = type.getDeclaredField(fieldName); type = type.getSuperclass(); } while (field == null && !Object.class.equals(type)); if (field == null) { throw new NoSuchFieldException( String.format("No field with name [%1$s] found on object of type [%2$s]", fieldName, ObjectUtils.nullSafeClassName(obj))); } field.setAccessible(true); return (T) field.get(obj); } catch (Exception e) { throw new RuntimeException(String.format("Failed to read field [%1$s] from object of type [%2$s]", fieldName, ObjectUtils.nullSafeClassName(obj)), e); } }