Convert Java nested beans to aaa.bbb [0] .ccc format

Introduction

--Since it is necessary to convert the nested Bean to the format of aaa.bbb [0] .ccc, there may be a bug because it is a prototype for the time being. --The assumed type is as follows --String or List or Child Bean or List in the member variable of the parent bean

Bean0.java


@Data
public class Bean0 {
	private List<Bean1> listBean1;
}

Bean1.java


@Data
public class Bean1 {
	private String id;
	private String name;
	private List<String> list;
	private Bean2 bean2;
	private List<Bean2> listBean2;
}

Bean1.java


@Data
public class Bean2 {
	private String count;
	private List<String> hogeList;
}

Bean1Test.java


@SpringBootTest
public class Bean1Test {


	@Test
	public void hoge() throws IllegalArgumentException, IllegalAccessException {
		Bean0 b0 = new Bean0();
		List<Bean1> list = new ArrayList<Bean1>();
		for(int i = 0; i <= 2; i++) {
			Bean1 b1 = new Bean1();

			b1.setId("" + i);
			b1.setName("Hogetaro" + i);
			b1.setList(new ArrayList<String>(Arrays.asList("aaa", "bbb", "ccc")));

			Bean2 b2 = new Bean2();
			b2.setCount("10" + i);
			b2.setHogeList(Arrays.asList("1" + i));

			b1.setBean2(b2);

			List<Bean2> listb2 = new ArrayList<Bean2>();
			Bean2 b21 = new Bean2();
			b21.setCount("101" + i);
			b21.setHogeList(Arrays.asList("11" + i, "12" + i));
			listb2.add(b21);

			Bean2 b22 = new Bean2();
			b22.setCount("102" + i);
			b22.setHogeList(Arrays.asList("101" + i, "1001" + i, "10001" + i));
			listb2.add(b22);

			b1.setListBean2(listb2);
			list.add(b1);
		}
		b0.setListBean1(list);

		try {
			List<String> ret = getFieldNameValue(b0);
			for(String str: ret) {
				System.out.println(str);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	private List<String> getFieldNameValue(Object obj) throws IllegalArgumentException, IllegalAccessException {
		List<String> list = new ArrayList<String>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for(Field f : fields) {
        	f.setAccessible(true);
        	Object val = f.get(obj);
            if(f.getType().equals(String.class)) {
                list.add(f.getName() + ":" + val);
            }
            else if(f.getType().equals(List.class)) {
            	int i = 0;
            	int j = 0;
            	if(null == f.get(obj)) continue;
        		for(Object obj2: (List<Object>)val) {
        			if(obj2.getClass().equals(String.class)) {
        				list.add(f.getName() + "[" + i++ + "]:" + obj2.toString());
        			}
        			else {
        				List<String> list2 = getFieldNameValue(obj2);
        				List<String> list3 = new ArrayList<String>();
        				for(String str2 : list2) {
        					list3.add(f.getName() + "[" + j + "]" + "." + str2);
        				}
        				list.addAll(list3);
        				j++;
        			}
            	}
            } else {
            	List<String> list2 = getFieldNameValue(val);
				List<String> list3 = new ArrayList<String>();
				for(String str2 : list2) {
					list3.add(f.getName() + "." + str2);
				}
				list.addAll(list3);
            }
        }
        return list;
	}
}

--JUnit execution result

listBean1[0].id:0
listBean1[0].name:Hogetaro 0
listBean1[0].list[0]:aaa
listBean1[0].list[1]:bbb
listBean1[0].list[2]:ccc
listBean1[0].bean2.count:100
listBean1[0].bean2.hogeList[0]:10
listBean1[0].listBean2[0].count:1010
listBean1[0].listBean2[0].hogeList[0]:110
listBean1[0].listBean2[0].hogeList[1]:120
listBean1[0].listBean2[1].count:1020
listBean1[0].listBean2[1].hogeList[0]:1010
listBean1[0].listBean2[1].hogeList[1]:10010
listBean1[0].listBean2[1].hogeList[2]:100010
listBean1[1].id:1
listBean1[1].name:Hogetaro 1
listBean1[1].list[0]:aaa
listBean1[1].list[1]:bbb
listBean1[1].list[2]:ccc
listBean1[1].bean2.count:101
listBean1[1].bean2.hogeList[0]:11
listBean1[1].listBean2[0].count:1011
listBean1[1].listBean2[0].hogeList[0]:111
listBean1[1].listBean2[0].hogeList[1]:121
listBean1[1].listBean2[1].count:1021
listBean1[1].listBean2[1].hogeList[0]:1011
listBean1[1].listBean2[1].hogeList[1]:10011
listBean1[1].listBean2[1].hogeList[2]:100011
listBean1[2].id:2
listBean1[2].name:Hogetaro 2
listBean1[2].list[0]:aaa
listBean1[2].list[1]:bbb
listBean1[2].list[2]:ccc
listBean1[2].bean2.count:102
listBean1[2].bean2.hogeList[0]:12
listBean1[2].listBean2[0].count:1012
listBean1[2].listBean2[0].hogeList[0]:112
listBean1[2].listBean2[0].hogeList[1]:122
listBean1[2].listBean2[1].count:1022
listBean1[2].listBean2[1].hogeList[0]:1012
listBean1[2].listBean2[1].hogeList[1]:10012
listBean1[2].listBean2[1].hogeList[2]:100012

Recommended Posts

Convert Java nested beans to aaa.bbb [0] .ccc format
Convert Java Powerpoint to XPS
How to convert Java radix
[Java] Convert ArrayList to array
Convert 2D array to csv format with Java 8 Stream API
Convert ruby object to JSON format
[Java] Convert 1-to-N List to Map
[Android] Convert Android Java code to Kotlin
[Java] Convert array to ArrayList * Caution
Convert all Android apps (Java) to Kotlin
Convert SVG files to PNG files in Java
[* Java *] I went to JJUG CCC 2017 Fall
[Java] Convert DB code to code value using enum
How to convert a solidity contract to a Java contract class
google java format
[Java] Nested class
[Java] Introduction to Java
Introduction to java
Launch Docker from Java to convert Office documents to PDF
<java> Read Zip file and convert directly to string
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
Convert Java enum enums and JSON to and from Jackson
Sample code to convert List to List <String> in Java Stream
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
Java applications convert Word (DOC / DOCX) documents to PDF
Introduce RMagick to convert existing existing image files to another format
How to convert a file to a byte array in Java
[Kotlin] Convert ZonedDateTime to String by specifying the format
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-