@Testpublic void t2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException {File relativeFile = new File("./src/main/java/com/myblog/domain");File absoluteFile = new File(relativeFile.getCanonicalPath());traversalDirectory(absoluteFile);}private void traversalDirectory(File file) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {File[] files = file.listFiles();for (File f : files) {if (f.isDirectory()) {traversalDirectory(f);}if (f.isFile()) {String rootPath = System.getProperty("user.dir")+File.separator+"src"+File.separator+"main"+File.separator+"java"+File.separator;String fPath = f.toString();String relativePath = fPath.replace(rootPath, "");String classPath = relativePath.replace(File.separatorChar, '.');String packagePath = classPath.substring(0, classPath.lastIndexOf("."));testSetAndGet(packagePath);}}}private void testSetAndGet(String classPath) {Object obj = null;try {//1.获取路径上所对应的类Class clazz = Class.forName(classPath);//2.获取类所对应的无参数构造方法 前提得有构造方法Constructor con = clazz.getConstructor();//3.通过构造方法得到当前类的对象obj = con.newInstance();//4.获取类中的全部的私有属性Field[] fields = clazz.getDeclaredFields();//5.构造出set+属性名的方法 并 运行给属性赋值for(Field field : fields){String firstLetter = field.getName().substring(0,1).toUpperCase();String otherLetters = field.getName().substring(1);String setMethod = new StringBuilder("set").append(firstLetter).append(otherLetters).toString();String getMethod = new StringBuilder("get").append(firstLetter).append(otherLetters).toString();String isMethod = new StringBuilder("is").append(firstLetter).append(otherLetters).toString();Method[] declaredMethods = clazz.getDeclaredMethods();for (Method method : declaredMethods) {if (method.getName().equals(getMethod)) {// 覆盖get的方法method.invoke(obj);} else if (method.getName().equals(isMethod)) {//覆盖 is方法method.invoke(obj);} else if (method.getName().equals(setMethod)) {Class type = field.getType();Object param = getParam(type);method.invoke(obj, param);}}}} catch (Exception e) {assert true;}}private Object getParam(Class classType) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {String type = classType.getSimpleName();Object param = null;switch (type) {case "byte":case "Byte":param = 1;break;case "short":case "Short":param = 1;break;case "int":case "Integer":param = 1;break;case "long":case "Long":param = 1L;break;case "float":case "Float":param = 1F;break;case "double":case "Double":param = 1F;break;case "char":case "Character":param = "a";break;case "boolean":case "Boolean":param = true;break;case "String":param = "test";break;case "List":case "ArrayList":param = new ArrayList<>();break;case "Set":case "HashSet":param = new HashSet<>();break;case "Map":case "HashMap":param = new HashMap<>();break;default:testSetAndGet(classType.getName());}return param;}
