下面列出了org.springframework.beans.BeanWrapper#getPropertyDescriptors ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@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;
}
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[] {});
}
/**
* 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);
}
@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();
}
/**
* 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);
}
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);
}
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);
}
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);
}
/**
* 获取对象值为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);
}
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);
}
/**
* 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);
}
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);
}
/**
* 获取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);
}
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()]);
}
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));
}
}
}
}
/**
* 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);
}
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);
}
@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()));
}
}
}
/**
* 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());
// }
}