Java Code Examples for org.springframework.beans.BeanWrapper#isReadableProperty()

The following examples show how to use org.springframework.beans.BeanWrapper#isReadableProperty() . 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: MapItemSqlParameterSourceProvider.java    From CogStack-Pipeline with Apache License 2.0 6 votes vote down vote up
/**
 * Provide parameter values in an {@link MapSqlParameterSource} based on values from
 * the provided item.
 * Supports accessing nested maps or arrays.
 *  e.g. :map1.level1.level2
 *    or :map1.array1[10].level2
 * However it assume keys for the maps are all String.
 * @param item the item to use for parameter values
 */
@Override
public SqlParameterSource createSqlParameterSource(T item) {
  BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(item);
  if (beanWrapper.isReadableProperty(mapPropertyName)) {
      Map<String, Object> map = (Map<String, Object>) beanWrapper.getPropertyValue(mapPropertyName);
      if (shouldFlattenMap) {
        Map<String, Object> flattenMap = new HashMap<String, Object>();
        flatten(map, flattenMap, "");
        return new MapSqlParameterSource(flattenMap);
      } else {
        return new MapSqlParameterSource(map);
      }
  }
  return new MapSqlParameterSource();
}
 
Example 2
Source File: BeanUtil.java    From newbee-mall with GNU General Public License v3.0 5 votes vote down vote up
public static Map<String, Object> toMap(Object bean, String... ignoreProperties) {
    Map<String, Object> map = new LinkedHashMap<>();
    List<String> ignoreList = new ArrayList<>(Arrays.asList(ignoreProperties));
    ignoreList.add("class");
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    for (PropertyDescriptor pd : beanWrapper.getPropertyDescriptors()) {
        if (!ignoreList.contains(pd.getName()) && beanWrapper.isReadableProperty(pd.getName())) {
            Object propertyValue = beanWrapper.getPropertyValue(pd.getName());
            map.put(pd.getName(), propertyValue);
        }
    }
    return map;
}
 
Example 3
Source File: FieldValueCounterSinkConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
private void processPojo(String counterName, Object payload) {
	BeanWrapper beanWrapper = new BeanWrapperImpl(payload);
	if (beanWrapper.isReadableProperty(fvcSinkProperties.getFieldName())) {
		Object value = beanWrapper.getPropertyValue(fvcSinkProperties.getFieldName());
		processValue(counterName, value);
	}
}
 
Example 4
Source File: SimpleBeanCopier.java    From onetwo with Apache License 2.0 5 votes vote down vote up
private boolean isSrcHasProperty(BeanWrapper srcBean, String targetPropertyName){
	if(srcBean.getWrappedInstance() instanceof Map){
		Map<?, ?> map = (Map<?, ?>)srcBean.getWrappedInstance();
		return map.containsKey(targetPropertyName);
	}else{
		return srcBean.isReadableProperty(targetPropertyName);
	}
}
 
Example 5
Source File: AdvancedParameterSource.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
public AdvancedParameterSource addBean( Object bean ){
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess( bean );
    for( PropertyDescriptor pd : beanWrapper.getPropertyDescriptors() ){
        if( beanWrapper.isReadableProperty( pd.getName() ) ){
            String paramName = pd.getName();
            Object value = beanWrapper.getPropertyValue( paramName );
            addValue( paramName, value );
        }
    }
    return this;
}
 
Example 6
Source File: Util.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
public static void setCorrespondingProperties(BeanWrapper target, BeanWrapper source) {
	for (PropertyDescriptor pd : source.getPropertyDescriptors()) {
		String property = pd.getName();
		if (!"class".equals(property) && source.isReadableProperty(property) &&
					source.getPropertyValue(property) != null) {
			if (target.isWritableProperty(property)) {
				target.setPropertyValue(property, source.getPropertyValue(property));
			}
		}
	}
}
 
Example 7
Source File: TomcatJdbcDataSourceFactoryTest.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
@Test
void testAllPropertiesSet() throws Exception {
	TomcatJdbcDataSourceFactory tomcatJdbcDataSourceFactory = new TomcatJdbcDataSourceFactory();

	tomcatJdbcDataSourceFactory.setDbProperties(new Properties());
	tomcatJdbcDataSourceFactory.setDefaultAutoCommit(true);
	tomcatJdbcDataSourceFactory.setDefaultReadOnly(false);
	tomcatJdbcDataSourceFactory.setDefaultTransactionIsolation(
			TransactionDefinition.ISOLATION_READ_COMMITTED);
	tomcatJdbcDataSourceFactory.setDefaultCatalog("myCatalog");
	tomcatJdbcDataSourceFactory.setConnectionProperties("foo=bar");
	tomcatJdbcDataSourceFactory.setInitialSize(11);
	tomcatJdbcDataSourceFactory.setMaxActive(100);
	tomcatJdbcDataSourceFactory.setMaxIdle(110);
	tomcatJdbcDataSourceFactory.setMinIdle(10);
	tomcatJdbcDataSourceFactory.setMaxWait(23);
	tomcatJdbcDataSourceFactory.setValidationQuery("SELECT 1");
	tomcatJdbcDataSourceFactory.setTestOnBorrow(true);
	tomcatJdbcDataSourceFactory.setTestOnReturn(true);
	tomcatJdbcDataSourceFactory.setTestWhileIdle(true);
	tomcatJdbcDataSourceFactory.setTimeBetweenEvictionRunsMillis(100);
	tomcatJdbcDataSourceFactory.setNumTestsPerEvictionRun(100);
	tomcatJdbcDataSourceFactory.setMinEvictableIdleTimeMillis(1000);
	tomcatJdbcDataSourceFactory.setAccessToUnderlyingConnectionAllowed(false);
	tomcatJdbcDataSourceFactory.setRemoveAbandoned(true);
	tomcatJdbcDataSourceFactory.setLogAbandoned(true);

	tomcatJdbcDataSourceFactory.setValidationInterval(10000);
	tomcatJdbcDataSourceFactory.setJmxEnabled(true);
	tomcatJdbcDataSourceFactory.setInitSQL("SET SCHEMA");
	tomcatJdbcDataSourceFactory.setTestOnConnect(true);
	tomcatJdbcDataSourceFactory.setJdbcInterceptors("foo");
	tomcatJdbcDataSourceFactory.setFairQueue(false);
	tomcatJdbcDataSourceFactory.setUseEquals(false);
	tomcatJdbcDataSourceFactory.setAbandonWhenPercentageFull(80);
	tomcatJdbcDataSourceFactory.setMaxAge(100);
	tomcatJdbcDataSourceFactory.setUseLock(true);
	tomcatJdbcDataSourceFactory.setSuspectTimeout(200);
	tomcatJdbcDataSourceFactory.setDataSourceJNDI("foo");
	tomcatJdbcDataSourceFactory.setAlternateUsernameAllowed(true);
	tomcatJdbcDataSourceFactory.setCommitOnReturn(true);
	tomcatJdbcDataSourceFactory.setRollbackOnReturn(true);
	tomcatJdbcDataSourceFactory.setUseDisposableConnectionFacade(false);
	tomcatJdbcDataSourceFactory.setLogValidationErrors(true);
	tomcatJdbcDataSourceFactory.setPropagateInterruptState(true);

	DataSourceInformation dataSourceInformation = new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "user", "password");
	DataSource dataSource = tomcatJdbcDataSourceFactory
			.createDataSource(dataSourceInformation);

	BeanWrapper source = PropertyAccessorFactory
			.forBeanPropertyAccess(tomcatJdbcDataSourceFactory);
	BeanWrapper target = PropertyAccessorFactory
			.forBeanPropertyAccess(dataSource.getPoolProperties());
	List<String> ignoredProperties = Arrays.asList("driverClassName", "url",
			"username", "password");

	for (PropertyDescriptor propertyDescriptor : source.getPropertyDescriptors()) {
		if (propertyDescriptor.getWriteMethod() != null
				&& target.isReadableProperty(propertyDescriptor.getName())
				&& !ignoredProperties.contains(propertyDescriptor.getName())) {
			assertThat(target.getPropertyValue(propertyDescriptor.getName()))
					.isEqualTo(source.getPropertyValue(propertyDescriptor.getName()));
		}
	}

}
 
Example 8
Source File: ObjectPdxInstanceAdapter.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the {@link Object value} for the {@link PropertyDescriptor property} identified by
 * the given {@link String field name} on the underlying, target {@link Object}.
 *
 * @param fieldName {@link String} containing the name of the field to get the {@link Object value} for.
 * @return the {@link Object value} for the {@link PropertyDescriptor property} identified by
 * the given {@link String field name} on the underlying, target {@link Object}.
 * @see org.springframework.beans.BeanWrapper#getPropertyValue(String)
 * @see #getBeanWrapper()
 */
@Override
public Object getField(String fieldName) {

	BeanWrapper beanWrapper = getBeanWrapper();

	return beanWrapper.isReadableProperty(fieldName)
		? beanWrapper.getPropertyValue(fieldName)
		: null;
}