来自: com.zaxxer.HikariCP-java6#PropertyBeanSetter.java

    1. import java.beans.BeanInfo;
    2. import java.beans.IntrospectionException;
    3. import java.beans.Introspector;
    4. import java.beans.PropertyDescriptor;
    5. import java.io.*;
    6. import java.lang.reflect.Method;
    7. import java.util.*;
    8. /**
    9. * @author tonnyyi
    10. * @since 15/4/22 10:52
    11. */
    12. public class PropertyUtil {
    13. public static void setProperties(Object bean, Properties properties) {
    14. if (bean == null || properties == null) {
    15. return;
    16. }
    17. Enumeration<?> propertyNames = properties.propertyNames();
    18. while (propertyNames.hasMoreElements()) {
    19. Object key = propertyNames.nextElement();
    20. String propertyName = key.toString();
    21. Object value = properties.getProperty(propertyName);
    22. if (value == null) {
    23. value = properties.get(propertyName);
    24. }
    25. setProperty(bean, propertyName, value);
    26. }
    27. }
    28. public static void setProperty(Object bean, String name, Object value) {
    29. String setMethodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
    30. PropertyDescriptor propertyDescriptor;
    31. try {
    32. propertyDescriptor = new PropertyDescriptor(name, bean.getClass(), null, setMethodName);
    33. }
    34. catch (IntrospectionException e) {
    35. throw new RuntimeException(e);
    36. }
    37. try {
    38. Method writeMethod = propertyDescriptor.getWriteMethod();
    39. Class<?> paramClass = writeMethod.getParameterTypes()[0];
    40. if (paramClass == int.class) {
    41. writeMethod.invoke(bean, Integer.parseInt(value.toString()));
    42. }
    43. else if (paramClass == long.class) {
    44. writeMethod.invoke(bean, Long.parseLong(value.toString()));
    45. }
    46. else if (paramClass == float.class) {
    47. writeMethod.invoke(bean, Float.parseFloat(value.toString()));
    48. }
    49. else if (paramClass == double.class) {
    50. writeMethod.invoke(bean, Double.parseDouble(value.toString()));
    51. }
    52. else if (paramClass == boolean.class) {
    53. writeMethod.invoke(bean, Boolean.parseBoolean(value.toString()));
    54. }
    55. else if (paramClass == String.class) {
    56. writeMethod.invoke(bean, value.toString());
    57. }
    58. else {
    59. writeMethod.invoke(bean, value);
    60. }
    61. }
    62. catch (Exception e) {
    63. throw new RuntimeException(e);
    64. }
    65. }
    66. public static Set<String> getPropertyNames(Class<?> clazz) {
    67. Set<String> propertyNames = new HashSet<>();
    68. try {
    69. BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
    70. for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
    71. if (!descriptor.getName().equals("class")) {
    72. propertyNames.add(descriptor.getName());
    73. }
    74. }
    75. return propertyNames;
    76. }
    77. catch (IntrospectionException e) {
    78. throw new RuntimeException(e);
    79. }
    80. }
    81. public static Object getProperty(Object bean, String name) {
    82. String getMethodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
    83. try {
    84. Method method = bean.getClass().getMethod(getMethodName);
    85. return method.invoke(bean);
    86. }
    87. catch (Exception e) {
    88. getMethodName = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
    89. try {
    90. Method method = bean.getClass().getMethod(getMethodName);
    91. return method.invoke(bean);
    92. }
    93. catch (Exception e1) {
    94. throw new RuntimeException(e1);
    95. }
    96. }
    97. }
    98. public static Properties loadProperty(String filePath) {
    99. if (filePath == null || filePath.trim().length() == 0) {
    100. return null;
    101. }
    102. File file = new File(filePath);
    103. try {
    104. InputStream is = file.isFile() ? new FileInputStream(file) : PropertyUtil.class.getResourceAsStream(filePath);
    105. if (is != null) {
    106. return loadProperty(is);
    107. }
    108. else {
    109. throw new IllegalArgumentException("Property file " + filePath + " was not found.");
    110. }
    111. }
    112. catch (IOException e) {
    113. throw new RuntimeException("Error loading properties file", e);
    114. }
    115. //return null;
    116. }
    117. public static Properties loadProperty(InputStream is) throws IOException {
    118. Properties properties = null;
    119. if (is != null) {
    120. properties = new Properties();
    121. try {
    122. properties.load(is);
    123. }
    124. finally {
    125. is.close();
    126. }
    127. }
    128. return properties;
    129. }
    130. }