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

The following examples show how to use org.springframework.beans.BeanWrapper#getPropertyDescriptors() . 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: ConvertedDatatablesData.java    From springlets with Apache License 2.0 6 votes vote down vote up
private static Map<String, Object> convert(Object value, ConversionService conversionService) {

    BeanWrapper bean = new BeanWrapperImpl(value);
    PropertyDescriptor[] properties = bean.getPropertyDescriptors();
    Map<String, Object> convertedValue = new HashMap<>(properties.length);

    for (int i = 0; i < properties.length; i++) {
      String name = properties[i].getName();
      Object propertyValue = bean.getPropertyValue(name);
      if (propertyValue != null
          && conversionService.canConvert(propertyValue.getClass(), String.class)) {
        TypeDescriptor source = bean.getPropertyTypeDescriptor(name);
        String convertedPropertyValue =
            (String) conversionService.convert(propertyValue, source, TYPE_STRING);
        convertedValue.put(name, convertedPropertyValue);
      }
    }

    return convertedValue;
  }
 
Example 2
Source File: MapToBeanConvertor.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T injectBeanProperties(Map<String, ?> propValues, T bean){
	Class<?> beanClass = bean.getClass();
	BeanWrapper bw = SpringUtils.newBeanWrapper(bean);
	for(PropertyDescriptor pd : bw.getPropertyDescriptors()){
		PropertyContext pc = new PropertyContext(beanClass, pd);
		String key = getMapKey(pc);
		Object value = propValues.get(key);
		if(value==null){
			continue;
		}
		if (value instanceof Map) {
			value = toBean((Map<String, ?>)value, pd.getPropertyType());
		} else {
			value = SpringUtils.getFormattingConversionService().convert(value, pd.getPropertyType());
		}
		bw.setPropertyValue(pd.getName(), value);
	}
	return bean;
}
 
Example 3
Source File: BeanUtils.java    From spring-content with Apache License 2.0 6 votes vote down vote up
public static Field[] findFieldsWithAnnotation(Class<?> domainObjClass,
		Class<? extends Annotation> annotationClass, BeanWrapper wrapper) {
	List<Field> fields = new ArrayList<>();

	PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
	for (PropertyDescriptor descriptor : descriptors) {
		Field candidate = getField(domainObjClass, descriptor.getName());
		if (candidate != null) {
			if (candidate.getAnnotation(annotationClass) != null) {
				fields.add(candidate);
			}
		}
	}

	for (Field field : getAllFields(domainObjClass)) {
		if (field.getAnnotation(annotationClass) != null) {
			if (fields.contains(field) == false) {
				fields.add(field);
			}
		}
	}
	return fields.toArray(new Field[] {});
}
 
Example 4
Source File: BeanUtils.java    From spring-microservice-boilerplate with MIT License 5 votes vote down vote up
/**
 * Get names of null properties
 *
 * @param source source object
 */
private static String[] getNamesOfNullProperties(Object source) {
  final BeanWrapper src = new BeanWrapperImpl(source);
  java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

  Set<String> emptyNames = new HashSet<>();
  for (java.beans.PropertyDescriptor pd : pds) {
    Object srcValue = src.getPropertyValue(pd.getName());
    if (srcValue == null) {
      emptyNames.add(pd.getName());
    }
  }
  String[] result = new String[emptyNames.size()];
  return emptyNames.toArray(result);
}
 
Example 5
Source File: BaseEntity.java    From cms with Apache License 2.0 5 votes vote down vote up
public static String[] getIgnoreProperty(Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for (java.beans.PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null) emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}
 
Example 6
Source File: AbstractAutowireCapableBeanFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
	 * Return an array of non-simple bean properties that are unsatisfied.
	 * These are probably unsatisfied references to other beans in the
	 * factory. Does not include simple properties like primitives or Strings.
	 * @param mbd the merged bean definition the bean was created with
	 * @param bw the BeanWrapper the bean was created with
	 * @return an array of bean property names
	 * @see org.springframework.beans.BeanUtils#isSimpleProperty
	 */
	protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
		Set<String> result = new TreeSet<String>();
		PropertyValues pvs = mbd.getPropertyValues();
		PropertyDescriptor[] pds = bw.getPropertyDescriptors();
		for (PropertyDescriptor pd : pds) {
//			��ȡ����дֵ�ķ���������
			if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) &&
					!BeanUtils.isSimpleProperty(pd.getPropertyType())) {
				
				result.add(pd.getName());
			}
		}
		return StringUtils.toStringArray(result);
	}
 
Example 7
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 8
Source File: SoftDeletesRepositoryImpl.java    From spring-boot-jpa-data-rest-soft-delete with MIT License 5 votes vote down vote up
public static String[] getNullPropertyNames(Object source) {
	final BeanWrapper src = new BeanWrapperImpl(source);
	PropertyDescriptor[] pds = src.getPropertyDescriptors();

	Set<String> propertyNames = new HashSet<String>();
	for (PropertyDescriptor pd : pds)
		if (!pd.getName().equals(DELETED_FIELD) && src.getPropertyValue(pd.getName()) == null)
			propertyNames.add(pd.getName());

	return propertyNames.toArray(new String[propertyNames.size()]);
}
 
Example 9
Source File: FameUtil.java    From Fame with MIT License 5 votes vote down vote up
/**
 * 获取Null属性名称
 *
 * @param source 对象
 * @return Null属性名称
 */
public static String[] getNullPropertyNames(Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for (java.beans.PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null) {
            emptyNames.add(pd.getName());
        }
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}
 
Example 10
Source File: BeanUtilsExtended.java    From hermes with Apache License 2.0 5 votes vote down vote up
public static String[] getNullPropertyNames(Object source) {
	final BeanWrapper src = new BeanWrapperImpl(source);
	java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

	Set<String> emptyNames = new HashSet<String>();
	for (java.beans.PropertyDescriptor pd : pds) {
		Object srcValue = src.getPropertyValue(pd.getName());
		if (srcValue != null)
			emptyNames.add(pd.getName());
	}
	String[] result = new String[emptyNames.size()];
	return emptyNames.toArray(result);
}
 
Example 11
Source File: AbstractAutowireCapableBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return an array of non-simple bean properties that are unsatisfied.
 * These are probably unsatisfied references to other beans in the
 * factory. Does not include simple properties like primitives or Strings.
 * @param mbd the merged bean definition the bean was created with
 * @param bw the BeanWrapper the bean was created with
 * @return an array of bean property names
 * @see org.springframework.beans.BeanUtils#isSimpleProperty
 */
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
	Set<String> result = new TreeSet<String>();
	PropertyValues pvs = mbd.getPropertyValues();
	PropertyDescriptor[] pds = bw.getPropertyDescriptors();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) &&
				!BeanUtils.isSimpleProperty(pd.getPropertyType())) {
			result.add(pd.getName());
		}
	}
	return StringUtils.toStringArray(result);
}
 
Example 12
Source File: BeanUtils.java    From NetworkDisk_Storage with GNU General Public License v2.0 5 votes vote down vote up
private static String[] getNullPropertyNames(Object source) {
    BeanWrapper src = new BeanWrapperImpl(source);
    PropertyDescriptor[] pds = src.getPropertyDescriptors();
    Set<String> emptyNames = new HashSet<String>();
    for (PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null)
            emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}
 
Example 13
Source File: EntityUtils.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
/**
 * 获取对象值为null的属性
 * @param source
 * @return
 */
public static String[] getNullPropertyNames (Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for(java.beans.PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null) emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}
 
Example 14
Source File: Beans.java    From shop with Apache License 2.0 5 votes vote down vote up
private static String[] getNullPropertyNames(Object source) {
  final BeanWrapper src = new BeanWrapperImpl(source);
  PropertyDescriptor[] pds = src.getPropertyDescriptors();

  Set<String> emptyNames = new HashSet<String>();
  for (PropertyDescriptor pd : pds) {
    Object srcValue = src.getPropertyValue(pd.getName());
    if (srcValue == null) emptyNames.add(pd.getName());
  }
  String[] result = new String[emptyNames.size()];

  return emptyNames.toArray(result);
}
 
Example 15
Source File: BeanUtils.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
private static String[] getNullPropertyNames(Object source) {
  final BeanWrapper src = new BeanWrapperImpl(source);
  PropertyDescriptor[] pds = src.getPropertyDescriptors();

  Set<String> emptyNames = new HashSet<String>();
  for (PropertyDescriptor pd : pds) {
    Object srcValue = src.getPropertyValue(pd.getName());
    if (srcValue == null) emptyNames.add(pd.getName());
  }
  String[] result = new String[emptyNames.size()];
  return emptyNames.toArray(result);
}
 
Example 16
Source File: BeanUtils.java    From black-shop with Apache License 2.0 5 votes vote down vote up
private static String[] getNullPropertyNames(Object source) {
  final BeanWrapper src = new BeanWrapperImpl(source);
  PropertyDescriptor[] pds = src.getPropertyDescriptors();

  Set<String> emptyNames = new HashSet<String>();
  for (PropertyDescriptor pd : pds) {
    Object srcValue = src.getPropertyValue(pd.getName());
    if (srcValue == null) emptyNames.add(pd.getName());
  }
  String[] result = new String[emptyNames.size()];
  return emptyNames.toArray(result);
}
 
Example 17
Source File: AbstractAutowireCapableBeanFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Return an array of non-simple bean properties that are unsatisfied.
 * These are probably unsatisfied references to other beans in the
 * factory. Does not include simple properties like primitives or Strings.
 * @param mbd the merged bean definition the bean was created with
 * @param bw the BeanWrapper the bean was created with
 * @return an array of bean property names
 * @see org.springframework.beans.BeanUtils#isSimpleProperty
 */
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
	Set<String> result = new TreeSet<>();
	PropertyValues pvs = mbd.getPropertyValues();
	PropertyDescriptor[] pds = bw.getPropertyDescriptors();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) &&
				!BeanUtils.isSimpleProperty(pd.getPropertyType())) {
			result.add(pd.getName());
		}
	}
	return StringUtils.toStringArray(result);
}
 
Example 18
Source File: AbstractSampleRandomGroupsController.java    From proctor with Apache License 2.0 5 votes vote down vote up
@Override
public String printProctorContext(final ProctorContext proctorContext)  {
    final StringBuilder sb = new StringBuilder();
    final BeanWrapper beanWrapper = new BeanWrapperImpl(proctorContext);
    for (final PropertyDescriptor descriptor : beanWrapper.getPropertyDescriptors()) {
        final String propertyName = descriptor.getName();
        if (!"class".equals(propertyName)) { // ignore class property which every object has
            final Object propertyValue = beanWrapper.getPropertyValue(propertyName);
            sb.append(propertyName).append(": '").append(propertyValue).append("'").append("\n");
        }
    }
    return sb.toString();
}
 
Example 19
Source File: UserPreferencesServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
     * Save annotation references, such as {@code BratAnnotator#windowSize}..., in a properties file
     * so that they are not required to configure every time they open the document.
     *
     * @param aUsername
     *            the user name
     * @param aMode
     *            differentiate the setting, either it is for {@code AnnotationPage} or
     *            {@code CurationPage}
     * @param aPref
     *            The Object to be saved as preference in the properties file.
     * @param aProject
     *            The project where the user is working on.
     * @throws IOException
     *             if an I/O error occurs.
     */
    private void saveLegacyPreferences(Project aProject, String aUsername, Mode aMode,
            AnnotationPreference aPref)
        throws IOException
    {
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(aPref);
        Properties props = new Properties();
        for (PropertyDescriptor value : wrapper.getPropertyDescriptors()) {
            if (wrapper.getPropertyValue(value.getName()) == null) {
                continue;
            }
            props.setProperty(aMode + "." + value.getName(),
                    wrapper.getPropertyValue(value.getName()).toString());
        }
        String propertiesPath = repositoryProperties.getPath().getAbsolutePath() + "/"
                + PROJECT_FOLDER + "/" + aProject.getId() + "/" + SETTINGS_FOLDER + "/" + aUsername;
        // append existing preferences for the other mode
        if (new File(propertiesPath, ANNOTATION_PREFERENCE_PROPERTIES_FILE).exists()) {
            Properties properties = loadLegacyPreferencesFile(aUsername, aProject);
            for (Entry<Object, Object> entry : properties.entrySet()) {
                String key = entry.getKey().toString();
                // Maintain other Modes of annotations confs than this one
                if (!key.substring(0, key.indexOf(".")).equals(aMode.toString())) {
                    props.put(entry.getKey(), entry.getValue());
                }
            }
        }

        // for (String name : props.stringPropertyNames()) {
        // log.info("{} = {}", name, props.getProperty(name));
        // }

        FileUtils.forceMkdir(new File(propertiesPath));
        props.store(new FileOutputStream(
                new File(propertiesPath, ANNOTATION_PREFERENCE_PROPERTIES_FILE)), null);

//        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
//                String.valueOf(aProject.getId()))) {
//            log.info("Saved preferences for user [{}] in project [{}]({})", aUsername,
//                    aProject.getName(), aProject.getId());
//        }
    }
 
Example 20
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()));
		}
	}

}