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;
}
}
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