1 package com.github.mygreen.supercsv.builder;
2
3 import java.lang.reflect.Field;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Objects;
7
8 import com.github.mygreen.supercsv.annotation.CsvBean;
9 import com.github.mygreen.supercsv.annotation.CsvColumn;
10 import com.github.mygreen.supercsv.annotation.DefaultGroup;
11 import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
12 import com.github.mygreen.supercsv.localization.MessageBuilder;
13
14
15
16
17
18
19
20
21 public class LazyBeanMappingFactory extends BeanMappingFactory {
22
23
24
25
26 public LazyBeanMappingFactory() {
27
28 }
29
30
31
32
33
34
35
36
37
38
39
40
41
42 @Override
43 public <T> BeanMapping<T> create(final Class<T> beanType, final Class<?>... groups) {
44
45 Objects.requireNonNull(beanType);
46
47 final Configuration configuration = getConfiguration();
48 final BeanMapping<T> beanMapping = new BeanMapping<>(beanType);
49 beanMapping.setConfiguration(configuration);
50
51
52 final CsvBean beanAnno = beanType.getAnnotation(CsvBean.class);
53 if(beanAnno == null) {
54 throw new SuperCsvInvalidAnnotationException(beanAnno, MessageBuilder.create("anno.notFound")
55 .varWithClass("property", beanType)
56 .varWithAnno("anno", CsvBean.class)
57 .format());
58 }
59
60
61 buildHeaderMapper(beanMapping, beanAnno);
62
63
64 buildValidators(beanMapping, beanAnno, groups);
65
66
67 buildColumnMappingList(beanMapping, beanType, groups);
68
69
70 buildCallbackMethods(beanMapping, beanType, beanAnno);
71
72 return beanMapping;
73 }
74
75
76
77
78
79
80
81
82 @Override
83 protected <T> void buildColumnMappingList(final BeanMapping<T> beanMapping, final Class<T> beanType, final Class<?>[] groups) {
84
85 final List<ColumnMapping> columnMappingList = new ArrayList<>();
86 for(Field field : beanType.getDeclaredFields()) {
87
88 final CsvColumn columnAnno = field.getAnnotation(CsvColumn.class);
89 if(columnAnno != null) {
90 columnMappingList.add(createColumnMapping(field, columnAnno, groups));
91 }
92
93 }
94
95 beanMapping.addAllColumns(columnMappingList);
96
97 }
98
99
100 }