@@ -58,7 +58,7 @@ public TypeDef parse(TypeElement typeElement) {
58
58
.qualifiedName (config .getQualifiedName ())
59
59
.simpleName (config .getName ())
60
60
.enumValueInfos (
61
- enumValueMarker .marked () ? parseEnumConstants (enumConstantElems , enumValueMarker ) : useEnumConstantNames (enumConstantElems )
61
+ enumValueMarker .marked () ? parseEnumConstants (typeElement , enumConstantElems , enumValueMarker ) : useEnumConstantNames (enumConstantElems )
62
62
)
63
63
.build ();
64
64
}
@@ -71,7 +71,7 @@ private static List<EnumValueInfo> useEnumConstantNames(List<VariableElement> en
71
71
return res ;
72
72
}
73
73
74
- private List <EnumValueInfo > parseEnumConstants (List <VariableElement > enumConstants , EnumValueMarker enumValueMarker ) {
74
+ private List <EnumValueInfo > parseEnumConstants (TypeElement enumTypeElement , List <VariableElement > enumConstants , EnumValueMarker enumValueMarker ) {
75
75
List <EnumValueInfo > res = new ArrayList <>(enumConstants .size ());
76
76
TypeInfo valueTypeInfo = typeInfoParser .parse (enumValueMarker .enumValueVariableElem .asType ());
77
77
int ctorArgIdx = enumValueMarker .matchAndGetConstructorArgIdx ();
@@ -82,22 +82,24 @@ private List<EnumValueInfo> parseEnumConstants(List<VariableElement> enumConstan
82
82
Tree tree = ctx .getTrees ().getTree (enumConstant );
83
83
if (tree instanceof VariableTree ) {
84
84
VariableTree variableTree = (VariableTree ) tree ;
85
- Object value = resolveValue (variableTree , ctorArgIdx );
85
+ Object value = resolveValue (enumTypeElement , variableTree , ctorArgIdx );
86
86
if (value != null ) {
87
87
res .add (new EnumValueInfo (valueTypeInfo , value ));
88
88
}
89
89
} else if (tree == null ) {
90
- ctx .error ("Literal value cannot be parsed from enum constant: %s, because source tree from the element is null." +
90
+ ctx .error ("Literal value cannot be parsed from enum constant: %s of enum %s , because source tree from the element is null." +
91
91
" This could mean at the time of the annotation processing, the source tree was not available." +
92
- " Is this class from a dependency jar/compiled class file? Please refer to the documentation for more information." , enumConstant );
92
+ " Is this class from a dependency jar/compiled class file? Please refer to the documentation for more information." ,
93
+ enumConstant , enumTypeElement );
93
94
} else {
94
- throw new SharedTypeInternalError (String .format ("Unsupported tree, kind: %s, tree: %s, element: %s" , tree .getKind (), tree , enumConstant ));
95
+ throw new SharedTypeInternalError (String .format (
96
+ "Unsupported tree during parsing enum %s, kind: %s, tree: %s, element: %s" , enumTypeElement , tree .getKind (), tree , enumConstant ));
95
97
}
96
98
}
97
99
return res ;
98
100
}
99
101
100
- private Object resolveValue (VariableTree tree , int ctorArgIdx ) {
102
+ private Object resolveValue (TypeElement enumTypeElement , VariableTree tree , int ctorArgIdx ) {
101
103
ExpressionTree init = tree .getInitializer ();
102
104
if (init instanceof NewClassTree ) {
103
105
NewClassTree newClassTree = (NewClassTree ) init ;
@@ -107,16 +109,16 @@ private Object resolveValue(VariableTree tree, int ctorArgIdx) {
107
109
LiteralTree argLiteralTree = (LiteralTree ) argTree ;
108
110
return argLiteralTree .getValue ();
109
111
} else {
110
- ctx .error ("Unsupported argument: %s in %s, argIndex: %s. Only literals are supported as enum value."
111
- , argTree , tree , ctorArgIdx );
112
+ ctx .error ("Unsupported argument in enum type %s : %s in %s, argIndex: %s. Only literals are supported as enum value." ,
113
+ enumTypeElement , argTree , tree , ctorArgIdx );
112
114
return null ;
113
115
}
114
116
} catch (IndexOutOfBoundsException e ) {
115
117
throw new SharedTypeInternalError (String .format (
116
- "Initializer has incorrect number of arguments: %s in tree: %s, argIndex: %s" , init , tree , ctorArgIdx ));
118
+ "Initializer in enum %s has incorrect number of arguments: %s in tree: %s, argIndex: %s" , enumTypeElement , init , tree , ctorArgIdx ));
117
119
}
118
120
}
119
- throw new SharedTypeInternalError (String .format ("Unsupported initializer: %s in tree: %s" , init , tree ));
121
+ throw new SharedTypeInternalError (String .format ("Unsupported initializer in enum %s : %s in tree: %s" , enumTypeElement , init , tree ));
120
122
}
121
123
122
124
@ RequiredArgsConstructor
@@ -142,7 +144,7 @@ void parseConstructor(ExecutableElement constructor) {
142
144
143
145
void setField (VariableElement enumValueVariableElem ) {
144
146
if (this .enumValueVariableElem != null ) {
145
- ctx .error ("Enum '%s' has multiple annotation @%s usage, only one field or constructor parameter is allowed, found on %s and %s" ,
147
+ ctx .error ("Enum %s has multiple annotation @%s usage, only one field or constructor parameter is allowed, found on %s and %s" ,
146
148
config .getQualifiedName (), SharedType .EnumValue .class , this .enumValueVariableElem , enumValueVariableElem );
147
149
} else {
148
150
this .enumValueVariableElem = enumValueVariableElem ;
@@ -166,12 +168,13 @@ int matchAndGetConstructorArgIdx() {
166
168
String lombokSuggestion = "" ;
167
169
if (constructorArgNames .isEmpty ()) {
168
170
lombokSuggestion = "The discovered constructor has 0 parameter, if Lombok is used to generate the constructor," +
169
- " please ensure annotation processing of Lombok is executed before SharedType." ;
171
+ " please ensure annotation processing of Lombok is executed before SharedType. Or add explicit constructor." +
172
+ " Later version of SharedType may infer constructor parameter position by field position without an explicit constructor." ;
170
173
}
171
174
172
- ctx .error ("Enum '%s' has @%s annotated on a field, but no constructor parameter can be matched."
175
+ ctx .error ("Enum %s has @%s annotated on a field, but no constructor parameter can be matched."
173
176
+ lombokSuggestion
174
- + " Please refer to the documentation on how to correctly use mark enum value." ,
177
+ + " May refer to the documentation on how to correctly mark enum value." ,
175
178
config .getQualifiedName (), SharedType .EnumValue .class );
176
179
return -1 ;
177
180
}
0 commit comments