@@ -265,50 +265,50 @@ private void readMethod(Element element, List<FieldReader> localFields) {
265
265
}
266
266
// for getter/accessor methods only, not setters
267
267
PropertyPrism .getOptionalOn (methodElement )
268
- .filter (p -> !hasRecordPropertyAnnotation (methodElement ))
269
- .ifPresent (
270
- propertyPrism -> {
271
- if (!methodElement .getParameters ().isEmpty ()) {
272
- logError (
273
- errorContext
274
- + baseType
275
- + ", @Json.Property can only be placed on Getter Methods, but on %s" ,
276
- methodElement );
277
- return ;
278
- }
279
-
280
- // getter property as simulated read-only field with getter method
281
- final var frequency = frequency (propertyPrism .value ());
282
- final var reader =
283
- new FieldReader (
284
- element , namingConvention , currentSubType , genericTypeParams , frequency );
285
- reader .getterMethod (new MethodReader (methodElement ));
286
- localFields .add (reader );
287
- });
288
- }
289
-
290
- private boolean hasRecordPropertyAnnotation (ExecutableElement methodElement ) {
268
+ .filter (p -> !hasRecordPropertyAnnotation (methodElement ))
269
+ .ifPresent (propertyPrism -> {
270
+ if (!methodElement .getParameters ().isEmpty ()) {
271
+ logError (errorContext + baseType + ", @Json.Property can only be placed on Getter Methods, but on %s" , methodElement );
272
+ return ;
273
+ }
274
+
275
+ // getter property as simulated read-only field with getter method
276
+ final var frequency = frequency (propertyPrism .value ());
277
+ final var reader = new FieldReader (element , namingConvention , currentSubType , genericTypeParams , frequency );
278
+ reader .getterMethod (new MethodReader (methodElement ));
279
+ localFields .add (reader );
280
+ });
281
+ }
291
282
283
+ private boolean hasRecordPropertyAnnotation (ExecutableElement methodElement ) {
292
284
try {
293
285
return APContext .jdkVersion () >= 16
294
- && Optional .ofNullable (
295
- Elements .class
296
- .getMethod ("recordComponentFor" , ExecutableElement .class )
297
- .invoke (APContext .elements (), methodElement ))
298
- .map (Element .class ::cast )
299
- .flatMap (
300
- e -> // e is a RecordComponentElement that doesn't have the annotation
301
- //so we look up the field by name to see if the annotation is present
302
- ElementFilter .fieldsIn (e .getEnclosingElement ().getEnclosedElements ()).stream ()
303
- .filter (f -> f .getSimpleName ().contentEquals (e .getSimpleName ()))
304
- .findAny ())
305
- .filter (PropertyPrism ::isPresent )
306
- .isPresent ();
286
+ && Optional .ofNullable (recordComponentFor (methodElement ))
287
+ .map (Element .class ::cast )
288
+ .flatMap (TypeReader ::matchingField )
289
+ .filter (PropertyPrism ::isPresent )
290
+ .isPresent ();
307
291
} catch (Exception e ) {
308
292
return false ;
309
293
}
310
294
}
311
295
296
+ /**
297
+ * e is a RecordComponentElement that doesn't have the annotation
298
+ * look up the field by name to see if the annotation is present
299
+ */
300
+ private static Optional <VariableElement > matchingField (Element e ) {
301
+ return ElementFilter .fieldsIn (e .getEnclosingElement ().getEnclosedElements ()).stream ()
302
+ .filter (f -> f .getSimpleName ().contentEquals (e .getSimpleName ()))
303
+ .findAny ();
304
+ }
305
+
306
+ private static Object recordComponentFor (ExecutableElement methodElement ) throws Exception {
307
+ return Elements .class
308
+ .getMethod ("recordComponentFor" , ExecutableElement .class )
309
+ .invoke (APContext .elements (), methodElement );
310
+ }
311
+
312
312
private boolean checkMethod2 (ExecutableElement methodElement ) {
313
313
if (!Util .isPublic (methodElement )) {
314
314
return false ;
0 commit comments