• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hee9841 / excel-module / #14

27 Mar 2025 12:44PM UTC coverage: 83.096% (-0.3%) from 83.415%
#14

Pull #35

github

web-flow
Merge c460834c6 into 00304f2fa
Pull Request #35: Feat: Excel, ExcelColumn 어노테이션 정합성 예외 처리

22 of 34 new or added lines in 3 files covered. (64.71%)

526 of 633 relevant lines covered (83.1%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/io/github/hee9841/excel/annotation/processor/ExcelColumnAnnotationProcessor.java
1
package io.github.hee9841.excel.annotation.processor;
2

3
import static io.github.hee9841.excel.global.SystemValues.ALLOWED_FIELD_TYPES;
4
import static io.github.hee9841.excel.global.SystemValues.ALLOWED_FIELD_TYPES_STRING;
5

6
import com.google.auto.service.AutoService;
7
import io.github.hee9841.excel.annotation.ExcelColumn;
8
import java.util.Objects;
9
import java.util.Set;
10
import javax.annotation.processing.AbstractProcessor;
11
import javax.annotation.processing.Messager;
12
import javax.annotation.processing.ProcessingEnvironment;
13
import javax.annotation.processing.Processor;
14
import javax.annotation.processing.RoundEnvironment;
15
import javax.annotation.processing.SupportedAnnotationTypes;
16
import javax.annotation.processing.SupportedSourceVersion;
17
import javax.lang.model.SourceVersion;
18
import javax.lang.model.element.Element;
19
import javax.lang.model.element.ElementKind;
20
import javax.lang.model.element.TypeElement;
21
import javax.lang.model.type.TypeKind;
22
import javax.lang.model.type.TypeMirror;
23
import javax.lang.model.util.Elements;
24
import javax.lang.model.util.Types;
25
import javax.tools.Diagnostic;
26

27
/**
28
 * Annotation processor for {@link ExcelColumn} annotation.
29
 * This processor validates that the annotation is only applied to supported field types.
30
 * 
31
 * <p>Supported types include:
32
 * <ul>
33
 *   <li>String</li>
34
 *   <li>Character/char</li>
35
 *   <li>Numeric types (Byte, Short, Integer, Long, Float, Double and their primitives)</li>
36
 *   <li>Boolean/boolean</li>
37
 *   <li>Date/Time types (LocalDate, LocalDateTime, Date, java.sql.Date)</li>
38
 *   <li>Enum types</li>
39
 * </ul>
40
 */
41
@AutoService(Processor.class)
42
@SupportedAnnotationTypes("io.github.hee9841.excel.annotation.ExcelColumn")
43
@SupportedSourceVersion(SourceVersion.RELEASE_8)
44
public class ExcelColumnAnnotationProcessor extends AbstractProcessor {
×
45

46
    private Messager messager;
47
    private Types typeUtils;
48
    private Elements elementUtils;
49

50
    @Override
51
    public synchronized void init(ProcessingEnvironment processingEnv) {
52
        super.init(processingEnv);
×
53
        messager = processingEnv.getMessager();
×
54
        typeUtils = processingEnv.getTypeUtils();
×
55
        elementUtils = processingEnv.getElementUtils();
×
56
    }
×
57

58
    @Override
59
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
60
        boolean hasError = false;
×
61

62
        for (Element element : roundEnv.getElementsAnnotatedWith(ExcelColumn.class)) {
×
63
            if (!isAllowedType(element)) {
×
64
                hasError = true;
×
65
            }
66
        }
×
67
        return !hasError;
×
68
    }
69

70
    /**
71
     * Checks if the annotated element has an allowed type.
72
     * Validates that the element is a field and checks if its type is either an enum
73
     * or one of the supported types defined in ALLOWED_TYPES. Array types are not allowed.
74
     *
75
     * @param element the element to check
76
     * @return true if the element has an allowed type, false otherwise
77
     */
78
    private boolean isAllowedType(Element element) {
79
        // 1.If element is enum type, return true
80
        if (!element.getKind().isField()) {
×
81
            error(element, "@ExcelColumn can only be applied to field type");
×
82
            return false;
×
83
        }
84

85
        TypeMirror typeMirror = element.asType();
×
86

87
        // 3. If element is array type, return false
88
        if (typeMirror.getKind() == TypeKind.ARRAY) {
×
89
            error(element, "@ExcelColumn cannot be applied to array type");
×
90
            return false;
×
91
        }
92

93
        if (isEnumType(typeMirror)) {
×
94
            return true;
×
95
        }
96

97

98
        // 4.Primitive type check
99
        if (typeMirror.getKind().isPrimitive()) {
×
100
            return true;
×
101
        }
102

103
        //5.Check if type is in allowed types list
104
        if (typeMirror.getKind() == TypeKind.DECLARED) {
×
NEW
105
            boolean isAllowed = ALLOWED_FIELD_TYPES.stream()
×
106
                .map(clazz -> elementUtils.getTypeElement(clazz.getTypeName()))
×
107
                .filter(Objects::nonNull)
×
108
                .map(TypeElement::asType)
×
109
                .anyMatch(allowedType ->
×
110
                    typeUtils.isAssignable(typeMirror, allowedType));
×
111

112
            if (!isAllowed) {
×
113
                error(element, "@ExcelColumn can only be applied to allowed types(%s).",
×
114
                    ALLOWED_FIELD_TYPES_STRING);
115
                return false;
×
116
            }
117

118
            return true;
×
119
        }
120

121
        return false;
×
122
    }
123

124
    /**
125
     * Checks if the given TypeMirror represents an enum type.
126
     *
127
     * @param typeMirror the type to check
128
     * @return true if the type is an enum, false otherwise
129
     */
130
    private boolean isEnumType(TypeMirror typeMirror) {
131
        TypeElement typeElement = (TypeElement) typeUtils.asElement(typeMirror);
×
132
        return typeElement != null && typeElement.getKind() == ElementKind.ENUM;
×
133
    }
134

135
    /**
136
     * Reports an error for the given element using the processor's messager.
137
     *
138
     * @param e the element for which to report the error
139
     * @param msg the error message format string
140
     * @param args the arguments to be used in the formatted message
141
     */
142
    private void error(Element e, String msg, Object... args) {
143
        messager.printMessage(
×
144
            Diagnostic.Kind.ERROR,
145
            String.format(msg, args),
×
146
            e);
147
    }
×
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc