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

hee9841 / excel-module / #18

08 Apr 2025 11:10AM UTC coverage: 82.835% (-0.3%) from 83.096%
#18

push

github

web-flow
Fix: 에러 메세지 수정(STY_CU_003_B, STG_CT_001_B,STG_ID_002_B 테스트 사항) (#46)

* Fix: 에러 메세지 수정(STY_CU_003_B, STG_CT_001_B,STG_ID_002_B 테스트 사항)

* Style: comment, 및 포멧 수정

1 of 1 new or added line in 1 file covered. (100.0%)

32 existing lines in 2 files now uncovered.

526 of 635 relevant lines covered (82.83%)

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 io.github.hee9841.excel.annotation.ExcelColumn;
7
import java.util.Objects;
8
import java.util.Set;
9
import javax.annotation.processing.AbstractProcessor;
10
import javax.annotation.processing.Messager;
11
import javax.annotation.processing.ProcessingEnvironment;
12
import javax.annotation.processing.RoundEnvironment;
13
import javax.annotation.processing.SupportedAnnotationTypes;
14
import javax.lang.model.SourceVersion;
15
import javax.lang.model.element.Element;
16
import javax.lang.model.element.ElementKind;
17
import javax.lang.model.element.TypeElement;
18
import javax.lang.model.type.TypeKind;
19
import javax.lang.model.type.TypeMirror;
20
import javax.lang.model.util.Elements;
21
import javax.lang.model.util.Types;
22
import javax.tools.Diagnostic;
23

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

41
    private Messager messager;
42
    private Types typeUtils;
43
    private Elements elementUtils;
44

45
    @Override
46
    public synchronized void init(ProcessingEnvironment processingEnv) {
UNCOV
47
        super.init(processingEnv);
×
UNCOV
48
        messager = processingEnv.getMessager();
×
UNCOV
49
        typeUtils = processingEnv.getTypeUtils();
×
UNCOV
50
        elementUtils = processingEnv.getElementUtils();
×
UNCOV
51
    }
×
52

53
    @Override
54
    public SourceVersion getSupportedSourceVersion() {
55
        return SourceVersion.latestSupported();
×
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
        // 4.Primitive type check
UNCOV
98
        if (typeMirror.getKind().isPrimitive()) {
×
99
            return true;
×
100
        }
101

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

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

UNCOV
117
            return true;
×
118
        }
119

UNCOV
120
        return false;
×
121
    }
122

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

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

© 2025 Coveralls, Inc