1. @Test
    2. public void t2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException {
    3. File relativeFile = new File("./src/main/java/com/myblog/domain");
    4. File absoluteFile = new File(relativeFile.getCanonicalPath());
    5. traversalDirectory(absoluteFile);
    6. }
    7. private void traversalDirectory(File file) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
    8. File[] files = file.listFiles();
    9. for (File f : files) {
    10. if (f.isDirectory()) {
    11. traversalDirectory(f);
    12. }
    13. if (f.isFile()) {
    14. String rootPath = System.getProperty("user.dir")+File.separator+"src"+File.separator+"main"+File.separator+"java"+File.separator;
    15. String fPath = f.toString();
    16. String relativePath = fPath.replace(rootPath, "");
    17. String classPath = relativePath.replace(File.separatorChar, '.');
    18. String packagePath = classPath.substring(0, classPath.lastIndexOf("."));
    19. testSetAndGet(packagePath);
    20. }
    21. }
    22. }
    23. private void testSetAndGet(String classPath) {
    24. Object obj = null;
    25. try {
    26. //1.获取路径上所对应的类
    27. Class clazz = Class.forName(classPath);
    28. //2.获取类所对应的无参数构造方法 前提得有构造方法
    29. Constructor con = clazz.getConstructor();
    30. //3.通过构造方法得到当前类的对象
    31. obj = con.newInstance();
    32. //4.获取类中的全部的私有属性
    33. Field[] fields = clazz.getDeclaredFields();
    34. //5.构造出set+属性名的方法 并 运行给属性赋值
    35. for(Field field : fields){
    36. String firstLetter = field.getName().substring(0,1).toUpperCase();
    37. String otherLetters = field.getName().substring(1);
    38. String setMethod = new StringBuilder("set").append(firstLetter).append(otherLetters).toString();
    39. String getMethod = new StringBuilder("get").append(firstLetter).append(otherLetters).toString();
    40. String isMethod = new StringBuilder("is").append(firstLetter).append(otherLetters).toString();
    41. Method[] declaredMethods = clazz.getDeclaredMethods();
    42. for (Method method : declaredMethods) {
    43. if (method.getName().equals(getMethod)) {
    44. // 覆盖get的方法
    45. method.invoke(obj);
    46. } else if (method.getName().equals(isMethod)) {
    47. //覆盖 is方法
    48. method.invoke(obj);
    49. } else if (method.getName().equals(setMethod)) {
    50. Class type = field.getType();
    51. Object param = getParam(type);
    52. method.invoke(obj, param);
    53. }
    54. }
    55. }
    56. } catch (Exception e) {
    57. assert true;
    58. }
    59. }
    60. private Object getParam(Class classType) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
    61. String type = classType.getSimpleName();
    62. Object param = null;
    63. switch (type) {
    64. case "byte":
    65. case "Byte":
    66. param = 1;
    67. break;
    68. case "short":
    69. case "Short":
    70. param = 1;
    71. break;
    72. case "int":
    73. case "Integer":
    74. param = 1;
    75. break;
    76. case "long":
    77. case "Long":
    78. param = 1L;
    79. break;
    80. case "float":
    81. case "Float":
    82. param = 1F;
    83. break;
    84. case "double":
    85. case "Double":
    86. param = 1F;
    87. break;
    88. case "char":
    89. case "Character":
    90. param = "a";
    91. break;
    92. case "boolean":
    93. case "Boolean":
    94. param = true;
    95. break;
    96. case "String":
    97. param = "test";
    98. break;
    99. case "List":
    100. case "ArrayList":
    101. param = new ArrayList<>();
    102. break;
    103. case "Set":
    104. case "HashSet":
    105. param = new HashSet<>();
    106. break;
    107. case "Map":
    108. case "HashMap":
    109. param = new HashMap<>();
    110. break;
    111. default:
    112. testSetAndGet(classType.getName());
    113. }
    114. return param;
    115. }