19
19
import static org .neo4j .driver .internal .util .Extract .assertParameter ;
20
20
import static org .neo4j .driver .internal .util .Iterables .newHashMapWithSize ;
21
21
22
+ import java .lang .reflect .AccessibleObject ;
22
23
import java .time .Duration ;
23
24
import java .time .LocalDate ;
24
25
import java .time .LocalDateTime ;
@@ -247,6 +248,7 @@ public static Value value(Object value) {
247
248
248
249
/**
249
250
* Returns an array of values from object vararg.
251
+ *
250
252
* @param input the object value(s)
251
253
* @return the array of values
252
254
*/
@@ -256,6 +258,7 @@ public static Value[] values(final Object... input) {
256
258
257
259
/**
258
260
* Returns a value from value vararg.
261
+ *
259
262
* @param input the value(s)
260
263
* @return the value
261
264
*/
@@ -265,6 +268,7 @@ public static Value value(Value... input) {
265
268
266
269
/**
267
270
* Returns a value from byte vararg.
271
+ *
268
272
* @param input the byte value(s)
269
273
* @return the value
270
274
*/
@@ -274,6 +278,7 @@ public static Value value(byte... input) {
274
278
275
279
/**
276
280
* Returns a value from string vararg.
281
+ *
277
282
* @param input the string value(s)
278
283
* @return the value
279
284
*/
@@ -286,6 +291,7 @@ public static Value value(String... input) {
286
291
287
292
/**
288
293
* Returns a value from boolean vararg.
294
+ *
289
295
* @param input the boolean value(s)
290
296
* @return the value
291
297
*/
@@ -298,6 +304,7 @@ public static Value value(boolean... input) {
298
304
299
305
/**
300
306
* Returns a value from char vararg.
307
+ *
301
308
* @param input the char value(s)
302
309
* @return the value
303
310
*/
@@ -310,6 +317,7 @@ public static Value value(char... input) {
310
317
311
318
/**
312
319
* Returns a value from long vararg.
320
+ *
313
321
* @param input the long value(s)
314
322
* @return the value
315
323
*/
@@ -322,6 +330,7 @@ public static Value value(long... input) {
322
330
323
331
/**
324
332
* Returns a value from short vararg.
333
+ *
325
334
* @param input the short value(s)
326
335
* @return the value
327
336
*/
@@ -331,8 +340,10 @@ public static Value value(short... input) {
331
340
.collect (Collectors .toCollection (() -> new ArrayList <>(input .length )));
332
341
return new ListValue (values );
333
342
}
343
+
334
344
/**
335
345
* Returns a value from int vararg.
346
+ *
336
347
* @param input the int value(s)
337
348
* @return the value
338
349
*/
@@ -342,8 +353,10 @@ public static Value value(int... input) {
342
353
.collect (Collectors .toCollection (() -> new ArrayList <>(input .length )));
343
354
return new ListValue (values );
344
355
}
356
+
345
357
/**
346
358
* Returns a value from double vararg.
359
+ *
347
360
* @param input the double value(s)
348
361
* @return the value
349
362
*/
@@ -356,6 +369,7 @@ public static Value value(double... input) {
356
369
357
370
/**
358
371
* Returns a value from float vararg.
372
+ *
359
373
* @param input the float value(s)
360
374
* @return the value
361
375
*/
@@ -368,6 +382,7 @@ public static Value value(float... input) {
368
382
369
383
/**
370
384
* Returns a value from list of objects.
385
+ *
371
386
* @param vals the list of objects
372
387
* @return the value
373
388
*/
@@ -379,6 +394,7 @@ public static Value value(List<Object> vals) {
379
394
380
395
/**
381
396
* Returns a value from iterable of objects.
397
+ *
382
398
* @param val the iterable of objects
383
399
* @return the value
384
400
*/
@@ -388,6 +404,7 @@ public static Value value(Iterable<Object> val) {
388
404
389
405
/**
390
406
* Returns a value from iterator of objects.
407
+ *
391
408
* @param val the iterator of objects
392
409
* @return the value
393
410
*/
@@ -401,6 +418,7 @@ public static Value value(Iterator<Object> val) {
401
418
402
419
/**
403
420
* Returns a value from stream of objects.
421
+ *
404
422
* @param stream the stream of objects
405
423
* @return the value
406
424
*/
@@ -463,14 +481,17 @@ public static Value value(Stream<Object> stream) {
463
481
* limitations on how those are supported by the database. Please read the Neo4j Cypher Manual for more up-to-date
464
482
* details. For example, at the time of writing, it is not possible to store maps as properties
465
483
* (see the following <a href="https://neo4j.com/docs/cypher-manual/current/values-and-types/property-structural-constructed/#constructed-types">page</a>).
484
+ * <p>
485
+ * If accessors for record components are not accessible, the driver will try to make them accessible using
486
+ * {@link AccessibleObject#trySetAccessible()} and will emit an error if this does not succeed.
466
487
*
467
488
* @param record the record to map
468
489
* @return the map value
490
+ * @throws ClientException when mapping fails
469
491
* @see TypeSystem#MAP()
470
492
* @see java.lang.Record
471
493
* @see java.lang.reflect.RecordComponent
472
494
* @see Property
473
- * @throws ClientException when mapping fails
474
495
* @since 5.28.5
475
496
*/
476
497
@ Preview (name = "Object mapping" )
@@ -483,11 +504,16 @@ public static Value value(java.lang.Record record) {
483
504
var isVector = recordComponent .getAnnotation (org .neo4j .driver .mapping .Vector .class ) != null ;
484
505
Value value ;
485
506
try {
486
- var objectValue = recordComponent .getAccessor ().invoke (record );
507
+ var accessor = recordComponent .getAccessor ();
508
+ if (!accessor .canAccess (record ) && !accessor .trySetAccessible ()) {
509
+ throw new IllegalStateException (
510
+ "Failed to make record component '%s' accessible" .formatted (recordComponent .getName ()));
511
+ }
512
+ var objectValue = accessor .invoke (record );
487
513
value = (objectValue != null ) ? isVector ? vector (objectValue ) : value (objectValue ) : null ;
488
514
} catch (Throwable throwable ) {
489
515
var message = "Failed to map '%s' property to value during mapping '%s' to map value"
490
- .formatted (property , record .getClass ().getCanonicalName ());
516
+ .formatted (property , record .getClass ().getName ());
491
517
throw new ClientException (
492
518
GqlStatusError .UNKNOWN .getStatus (),
493
519
GqlStatusError .UNKNOWN .getStatusDescription (message ),
@@ -505,6 +531,7 @@ public static Value value(java.lang.Record record) {
505
531
506
532
/**
507
533
* Returns a value from char.
534
+ *
508
535
* @param val the char value
509
536
* @return the value
510
537
*/
@@ -514,6 +541,7 @@ public static Value value(final char val) {
514
541
515
542
/**
516
543
* Returns a value from string.
544
+ *
517
545
* @param val the string value
518
546
* @return the value
519
547
*/
@@ -523,6 +551,7 @@ public static Value value(final String val) {
523
551
524
552
/**
525
553
* Returns a value from long.
554
+ *
526
555
* @param val the long value
527
556
* @return the value
528
557
*/
@@ -532,6 +561,7 @@ public static Value value(final long val) {
532
561
533
562
/**
534
563
* Returns a value from int.
564
+ *
535
565
* @param val the int value
536
566
* @return the value
537
567
*/
@@ -541,6 +571,7 @@ public static Value value(final int val) {
541
571
542
572
/**
543
573
* Returns a value from double.
574
+ *
544
575
* @param val the double value
545
576
* @return the value
546
577
*/
@@ -550,6 +581,7 @@ public static Value value(final double val) {
550
581
551
582
/**
552
583
* Returns a value from boolean.
584
+ *
553
585
* @param val the boolean value
554
586
* @return the value
555
587
*/
@@ -559,6 +591,7 @@ public static Value value(final boolean val) {
559
591
560
592
/**
561
593
* Returns a value from string to object map.
594
+ *
562
595
* @param val the string to object map
563
596
* @return the value
564
597
*/
@@ -572,6 +605,7 @@ public static Value value(final Map<String, Object> val) {
572
605
573
606
/**
574
607
* Returns a value from local date.
608
+ *
575
609
* @param localDate the local date value
576
610
* @return the value
577
611
*/
@@ -581,6 +615,7 @@ public static Value value(LocalDate localDate) {
581
615
582
616
/**
583
617
* Returns a value from offset time.
618
+ *
584
619
* @param offsetTime the offset time value
585
620
* @return the value
586
621
*/
@@ -590,6 +625,7 @@ public static Value value(OffsetTime offsetTime) {
590
625
591
626
/**
592
627
* Returns a value from local time.
628
+ *
593
629
* @param localTime the local time value
594
630
* @return the value
595
631
*/
@@ -599,6 +635,7 @@ public static Value value(LocalTime localTime) {
599
635
600
636
/**
601
637
* Returns a value from local date time.
638
+ *
602
639
* @param localDateTime the local date time value
603
640
* @return the value
604
641
*/
@@ -608,6 +645,7 @@ public static Value value(LocalDateTime localDateTime) {
608
645
609
646
/**
610
647
* Returns a value from offset date time.
648
+ *
611
649
* @param offsetDateTime the offset date time value
612
650
* @return the value
613
651
*/
@@ -617,6 +655,7 @@ public static Value value(OffsetDateTime offsetDateTime) {
617
655
618
656
/**
619
657
* Returns a value from zoned date time.
658
+ *
620
659
* @param zonedDateTime the zoned date time value
621
660
* @return the value
622
661
*/
@@ -626,6 +665,7 @@ public static Value value(ZonedDateTime zonedDateTime) {
626
665
627
666
/**
628
667
* Returns a value from period.
668
+ *
629
669
* @param period the period value
630
670
* @return the value
631
671
*/
@@ -635,6 +675,7 @@ public static Value value(Period period) {
635
675
636
676
/**
637
677
* Returns a value from duration.
678
+ *
638
679
* @param duration the duration value
639
680
* @return the value
640
681
*/
@@ -644,9 +685,10 @@ public static Value value(Duration duration) {
644
685
645
686
/**
646
687
* Returns a value from month, day, seconds and nanoseconds values.
647
- * @param months the month value
648
- * @param days the day value
649
- * @param seconds the seconds value
688
+ *
689
+ * @param months the month value
690
+ * @param days the day value
691
+ * @param seconds the seconds value
650
692
* @param nanoseconds the nanoseconds value
651
693
* @return the value
652
694
*/
@@ -656,6 +698,7 @@ public static Value isoDuration(long months, long days, long seconds, int nanose
656
698
657
699
/**
658
700
* Returns a value from ISO duration.
701
+ *
659
702
* @param duration the ISO duration value
660
703
* @return the value
661
704
*/
@@ -665,9 +708,10 @@ private static Value value(IsoDuration duration) {
665
708
666
709
/**
667
710
* Returns a value from SRID, x and y values.
711
+ *
668
712
* @param srid the SRID value
669
- * @param x the x value
670
- * @param y the y value
713
+ * @param x the x value
714
+ * @param y the y value
671
715
* @return the value
672
716
*/
673
717
public static Value point (int srid , double x , double y ) {
@@ -676,6 +720,7 @@ public static Value point(int srid, double x, double y) {
676
720
677
721
/**
678
722
* Returns a value from point.
723
+ *
679
724
* @param point the point value
680
725
* @return the value
681
726
*/
@@ -685,10 +730,11 @@ private static Value value(Point point) {
685
730
686
731
/**
687
732
* Returns a value from SRID, x ,y and z values.
733
+ *
688
734
* @param srid the SRID value
689
- * @param x the x value
690
- * @param y the y value
691
- * @param z the z value
735
+ * @param x the x value
736
+ * @param y the y value
737
+ * @param z the z value
692
738
* @return the value
693
739
*/
694
740
public static Value point (int srid , double x , double y , double z ) {
@@ -854,7 +900,7 @@ public static Function<Value, Map<String, Object>> ofMap() {
854
900
* the provided converter.
855
901
*
856
902
* @param valueConverter converter to use for the values of the map
857
- * @param <T> the type of values in the returned map
903
+ * @param <T> the type of values in the returned map
858
904
* @return a function that returns {@link Value#asMap(Function)} of a {@link Value}
859
905
*/
860
906
public static <T > Function <Value , Map <String , T >> ofMap (final Function <Value , T > valueConverter ) {
@@ -873,8 +919,8 @@ public static Function<Value, Entity> ofEntity() {
873
919
/**
874
920
* Converts values to {@link Long entity id}.
875
921
*
876
- * @deprecated superseded by {@link #ofEntityElementId()}.
877
922
* @return a function that returns the id an entity {@link Value}
923
+ * @deprecated superseded by {@link #ofEntityElementId()}.
878
924
*/
879
925
@ Deprecated
880
926
public static Function <Value , Long > ofEntityId () {
@@ -1002,7 +1048,7 @@ public static Function<Value, List<Object>> ofList() {
1002
1048
* Converts values to {@link List} of {@code T}.
1003
1049
*
1004
1050
* @param innerMap converter for the values inside the list
1005
- * @param <T> the type of values inside the list
1051
+ * @param <T> the type of values inside the list
1006
1052
* @return a function that returns {@link Value#asList(Function)} of a {@link Value}
1007
1053
*/
1008
1054
public static <T > Function <Value , List <T >> ofList (final Function <Value , T > innerMap ) {
0 commit comments