来自:
com.zaxxer.HikariCP-java6#PropertyBeanSetter.java
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Method;
import java.util.*;
/**
* @author tonnyyi
* @since 15/4/22 10:52
*/
public class PropertyUtil {
public static void setProperties(Object bean, Properties properties) {
if (bean == null || properties == null) {
return;
}
Enumeration<?> propertyNames = properties.propertyNames();
while (propertyNames.hasMoreElements()) {
Object key = propertyNames.nextElement();
String propertyName = key.toString();
Object value = properties.getProperty(propertyName);
if (value == null) {
value = properties.get(propertyName);
}
setProperty(bean, propertyName, value);
}
}
public static void setProperty(Object bean, String name, Object value) {
String setMethodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
PropertyDescriptor propertyDescriptor;
try {
propertyDescriptor = new PropertyDescriptor(name, bean.getClass(), null, setMethodName);
}
catch (IntrospectionException e) {
throw new RuntimeException(e);
}
try {
Method writeMethod = propertyDescriptor.getWriteMethod();
Class<?> paramClass = writeMethod.getParameterTypes()[0];
if (paramClass == int.class) {
writeMethod.invoke(bean, Integer.parseInt(value.toString()));
}
else if (paramClass == long.class) {
writeMethod.invoke(bean, Long.parseLong(value.toString()));
}
else if (paramClass == float.class) {
writeMethod.invoke(bean, Float.parseFloat(value.toString()));
}
else if (paramClass == double.class) {
writeMethod.invoke(bean, Double.parseDouble(value.toString()));
}
else if (paramClass == boolean.class) {
writeMethod.invoke(bean, Boolean.parseBoolean(value.toString()));
}
else if (paramClass == String.class) {
writeMethod.invoke(bean, value.toString());
}
else {
writeMethod.invoke(bean, value);
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Set<String> getPropertyNames(Class<?> clazz) {
Set<String> propertyNames = new HashSet<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
if (!descriptor.getName().equals("class")) {
propertyNames.add(descriptor.getName());
}
}
return propertyNames;
}
catch (IntrospectionException e) {
throw new RuntimeException(e);
}
}
public static Object getProperty(Object bean, String name) {
String getMethodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
try {
Method method = bean.getClass().getMethod(getMethodName);
return method.invoke(bean);
}
catch (Exception e) {
getMethodName = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
try {
Method method = bean.getClass().getMethod(getMethodName);
return method.invoke(bean);
}
catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
public static Properties loadProperty(String filePath) {
if (filePath == null || filePath.trim().length() == 0) {
return null;
}
File file = new File(filePath);
try {
InputStream is = file.isFile() ? new FileInputStream(file) : PropertyUtil.class.getResourceAsStream(filePath);
if (is != null) {
return loadProperty(is);
}
else {
throw new IllegalArgumentException("Property file " + filePath + " was not found.");
}
}
catch (IOException e) {
throw new RuntimeException("Error loading properties file", e);
}
//return null;
}
public static Properties loadProperty(InputStream is) throws IOException {
Properties properties = null;
if (is != null) {
properties = new Properties();
try {
properties.load(is);
}
finally {
is.close();
}
}
return properties;
}
}