20
20
package org .elasticsearch .painless ;
21
21
22
22
import org .elasticsearch .painless .Definition .Cast ;
23
- import org .elasticsearch .painless .Definition .Type ;
24
23
import org .elasticsearch .painless .Definition .def ;
25
24
26
25
import java .util .Objects ;
31
30
*/
32
31
public final class AnalyzerCaster {
33
32
34
- private Definition definition ;
35
-
36
- public AnalyzerCaster (Definition definition ) {
37
- this .definition = definition ;
38
- }
39
-
40
- public Cast getLegalCast (Location location , Type actualType , Type expectedType , boolean explicit , boolean internal ) {
41
- Objects .requireNonNull (actualType );
42
- Objects .requireNonNull (expectedType );
43
-
44
- Class <?> actual = actualType .clazz ;
45
- Class <?> expected = expectedType .clazz ;
46
-
47
- if (actualType .dynamic ) {
48
- actual = Definition .ObjectClassTodefClass (actual );
49
- }
50
-
51
- if (expectedType .dynamic ) {
52
- expected = Definition .ObjectClassTodefClass (expected );
53
- }
33
+ public static Cast getLegalCast (Location location , Class <?> actual , Class <?> expected , boolean explicit , boolean internal ) {
34
+ Objects .requireNonNull (actual );
35
+ Objects .requireNonNull (expected );
54
36
55
37
if (actual == expected ) {
56
38
return null ;
@@ -487,7 +469,7 @@ public Cast getLegalCast(Location location, Type actualType, Type expectedType,
487
469
}
488
470
}
489
471
490
- public Object constCast (Location location , final Object constant , final Cast cast ) {
472
+ public static Object constCast (Location location , Object constant , Cast cast ) {
491
473
Class <?> fsort = cast .from ;
492
474
Class <?> tsort = cast .to ;
493
475
@@ -498,7 +480,7 @@ public Object constCast(Location location, final Object constant, final Cast cas
498
480
} else if (fsort == char .class && tsort == String .class ) {
499
481
return Utility .charToString ((char )constant );
500
482
} else if (fsort .isPrimitive () && fsort != boolean .class && tsort .isPrimitive () && tsort != boolean .class ) {
501
- final Number number ;
483
+ Number number ;
502
484
503
485
if (fsort == char .class ) {
504
486
number = (int )(char )constant ;
@@ -523,224 +505,201 @@ public Object constCast(Location location, final Object constant, final Cast cas
523
505
}
524
506
}
525
507
526
- public Type promoteNumeric (Type from , boolean decimal ) {
527
- Class <?> sort = from .clazz ;
528
-
529
- if (from .dynamic ) {
530
- return definition .DefType ;
531
- } else if ((sort == double .class ) && decimal ) {
532
- return definition .doubleType ;
533
- } else if ((sort == float .class ) && decimal ) {
534
- return definition .floatType ;
535
- } else if (sort == long .class ) {
536
- return definition .longType ;
537
- } else if (sort == int .class || sort == char .class || sort == short .class || sort == byte .class ) {
538
- return definition .intType ;
508
+ public static Class <?> promoteNumeric (Class <?> from , boolean decimal ) {
509
+ if (from == def .class || from == double .class && decimal || from == float .class && decimal || from == long .class ) {
510
+ return from ;
511
+ } else if (from == int .class || from == char .class || from == short .class || from == byte .class ) {
512
+ return int .class ;
539
513
}
540
514
541
515
return null ;
542
516
}
543
517
544
- public Type promoteNumeric (Type from0 , Type from1 , boolean decimal ) {
545
- Class <?> sort0 = from0 .clazz ;
546
- Class <?> sort1 = from1 .clazz ;
547
-
548
- if (from0 .dynamic || from1 .dynamic ) {
549
- return definition .DefType ;
518
+ public static Class <?> promoteNumeric (Class <?> from0 , Class <?> from1 , boolean decimal ) {
519
+ if (from0 == def .class || from1 == def .class ) {
520
+ return def .class ;
550
521
}
551
522
552
523
if (decimal ) {
553
- if (sort0 == double .class || sort1 == double .class ) {
554
- return definition . doubleType ;
555
- } else if (sort0 == float .class || sort1 == float .class ) {
556
- return definition . floatType ;
524
+ if (from0 == double .class || from1 == double .class ) {
525
+ return double . class ;
526
+ } else if (from0 == float .class || from1 == float .class ) {
527
+ return float . class ;
557
528
}
558
529
}
559
530
560
- if (sort0 == long .class || sort1 == long .class ) {
561
- return definition . longType ;
562
- } else if (sort0 == int .class || sort1 == int .class ||
563
- sort0 == char .class || sort1 == char .class ||
564
- sort0 == short .class || sort1 == short .class ||
565
- sort0 == byte .class || sort1 == byte .class ) {
566
- return definition . intType ;
531
+ if (from0 == long .class || from1 == long .class ) {
532
+ return long . class ;
533
+ } else if (from0 == int .class || from1 == int .class ||
534
+ from0 == char .class || from1 == char .class ||
535
+ from0 == short .class || from1 == short .class ||
536
+ from0 == byte .class || from1 == byte .class ) {
537
+ return int . class ;
567
538
}
568
539
569
540
return null ;
570
541
}
571
542
572
- public Type promoteAdd (Type from0 , Type from1 ) {
573
- Class <?> sort0 = from0 .clazz ;
574
- Class <?> sort1 = from1 .clazz ;
575
-
576
- if (sort0 == String .class || sort1 == String .class ) {
577
- return definition .StringType ;
543
+ public static Class <?> promoteAdd (Class <?> from0 , Class <?> from1 ) {
544
+ if (from0 == String .class || from1 == String .class ) {
545
+ return String .class ;
578
546
}
579
547
580
548
return promoteNumeric (from0 , from1 , true );
581
549
}
582
550
583
- public Type promoteXor (Type from0 , Type from1 ) {
584
- Class <?> sort0 = from0 .clazz ;
585
- Class <?> sort1 = from1 .clazz ;
586
-
587
- if (from0 .dynamic || from1 .dynamic ) {
588
- return definition .DefType ;
551
+ public static Class <?> promoteXor (Class <?> from0 , Class <?> from1 ) {
552
+ if (from0 == def .class || from1 == def .class ) {
553
+ return def .class ;
589
554
}
590
555
591
- if (sort0 == boolean .class || sort1 == boolean .class ) {
592
- return definition . booleanType ;
556
+ if (from0 == boolean .class || from1 == boolean .class ) {
557
+ return boolean . class ;
593
558
}
594
559
595
560
return promoteNumeric (from0 , from1 , false );
596
561
}
597
562
598
- public Type promoteEquality (Type from0 , Type from1 ) {
599
- Class <?> sort0 = from0 .clazz ;
600
- Class <?> sort1 = from1 .clazz ;
601
-
602
- if (from0 .dynamic || from1 .dynamic ) {
603
- return definition .DefType ;
563
+ public static Class <?> promoteEquality (Class <?> from0 , Class <?> from1 ) {
564
+ if (from0 == def .class || from1 == def .class ) {
565
+ return def .class ;
604
566
}
605
567
606
- if (sort0 .isPrimitive () && sort1 .isPrimitive ()) {
607
- if (sort0 == boolean .class && sort1 == boolean .class ) {
608
- return definition . booleanType ;
568
+ if (from0 .isPrimitive () && from1 .isPrimitive ()) {
569
+ if (from0 == boolean .class && from1 == boolean .class ) {
570
+ return boolean . class ;
609
571
}
610
572
611
573
return promoteNumeric (from0 , from1 , true );
612
574
}
613
575
614
- return definition . ObjectType ;
576
+ return Object . class ;
615
577
}
616
578
617
- public Type promoteConditional (Type from0 , Type from1 , Object const0 , Object const1 ) {
618
- if (from0 . equals ( from1 ) ) {
579
+ public static Class <?> promoteConditional (Class <?> from0 , Class <?> from1 , Object const0 , Object const1 ) {
580
+ if (from0 == from1 ) {
619
581
return from0 ;
620
582
}
621
583
622
- Class <?> sort0 = from0 .clazz ;
623
- Class <?> sort1 = from1 .clazz ;
624
-
625
- if (from0 .dynamic || from1 .dynamic ) {
626
- return definition .DefType ;
584
+ if (from0 == def .class || from1 == def .class ) {
585
+ return def .class ;
627
586
}
628
587
629
- if (sort0 .isPrimitive () && sort1 .isPrimitive ()) {
630
- if (sort0 == boolean .class && sort1 == boolean .class ) {
631
- return definition . booleanType ;
588
+ if (from0 .isPrimitive () && from1 .isPrimitive ()) {
589
+ if (from0 == boolean .class && from1 == boolean .class ) {
590
+ return boolean . class ;
632
591
}
633
592
634
- if (sort0 == double .class || sort1 == double .class ) {
635
- return definition . doubleType ;
636
- } else if (sort0 == float .class || sort1 == float .class ) {
637
- return definition . floatType ;
638
- } else if (sort0 == long .class || sort1 == long .class ) {
639
- return definition . longType ;
593
+ if (from0 == double .class || from1 == double .class ) {
594
+ return double . class ;
595
+ } else if (from0 == float .class || from1 == float .class ) {
596
+ return float . class ;
597
+ } else if (from0 == long .class || from1 == long .class ) {
598
+ return long . class ;
640
599
} else {
641
- if (sort0 == byte .class ) {
642
- if (sort1 == byte .class ) {
643
- return definition . byteType ;
644
- } else if (sort1 == short .class ) {
600
+ if (from0 == byte .class ) {
601
+ if (from1 == byte .class ) {
602
+ return byte . class ;
603
+ } else if (from1 == short .class ) {
645
604
if (const1 != null ) {
646
605
final short constant = (short )const1 ;
647
606
648
607
if (constant <= Byte .MAX_VALUE && constant >= Byte .MIN_VALUE ) {
649
- return definition . byteType ;
608
+ return byte . class ;
650
609
}
651
610
}
652
611
653
- return definition . shortType ;
654
- } else if (sort1 == char .class ) {
655
- return definition . intType ;
656
- } else if (sort1 == int .class ) {
612
+ return short . class ;
613
+ } else if (from1 == char .class ) {
614
+ return int . class ;
615
+ } else if (from1 == int .class ) {
657
616
if (const1 != null ) {
658
617
final int constant = (int )const1 ;
659
618
660
619
if (constant <= Byte .MAX_VALUE && constant >= Byte .MIN_VALUE ) {
661
- return definition . byteType ;
620
+ return byte . class ;
662
621
}
663
622
}
664
623
665
- return definition . intType ;
624
+ return int . class ;
666
625
}
667
- } else if (sort0 == short .class ) {
668
- if (sort1 == byte .class ) {
626
+ } else if (from0 == short .class ) {
627
+ if (from1 == byte .class ) {
669
628
if (const0 != null ) {
670
629
final short constant = (short )const0 ;
671
630
672
631
if (constant <= Byte .MAX_VALUE && constant >= Byte .MIN_VALUE ) {
673
- return definition . byteType ;
632
+ return byte . class ;
674
633
}
675
634
}
676
635
677
- return definition . shortType ;
678
- } else if (sort1 == short .class ) {
679
- return definition . shortType ;
680
- } else if (sort1 == char .class ) {
681
- return definition . intType ;
682
- } else if (sort1 == int .class ) {
636
+ return short . class ;
637
+ } else if (from1 == short .class ) {
638
+ return short . class ;
639
+ } else if (from1 == char .class ) {
640
+ return int . class ;
641
+ } else if (from1 == int .class ) {
683
642
if (const1 != null ) {
684
643
final int constant = (int )const1 ;
685
644
686
645
if (constant <= Short .MAX_VALUE && constant >= Short .MIN_VALUE ) {
687
- return definition . shortType ;
646
+ return short . class ;
688
647
}
689
648
}
690
649
691
- return definition . intType ;
650
+ return int . class ;
692
651
}
693
- } else if (sort0 == char .class ) {
694
- if (sort1 == byte .class ) {
695
- return definition . intType ;
696
- } else if (sort1 == short .class ) {
697
- return definition . intType ;
698
- } else if (sort1 == char .class ) {
699
- return definition . charType ;
700
- } else if (sort1 == int .class ) {
652
+ } else if (from0 == char .class ) {
653
+ if (from1 == byte .class ) {
654
+ return int . class ;
655
+ } else if (from1 == short .class ) {
656
+ return int . class ;
657
+ } else if (from1 == char .class ) {
658
+ return char . class ;
659
+ } else if (from1 == int .class ) {
701
660
if (const1 != null ) {
702
661
final int constant = (int )const1 ;
703
662
704
663
if (constant <= Character .MAX_VALUE && constant >= Character .MIN_VALUE ) {
705
- return definition . byteType ;
664
+ return byte . class ;
706
665
}
707
666
}
708
667
709
- return definition . intType ;
668
+ return int . class ;
710
669
}
711
- } else if (sort0 == int .class ) {
712
- if (sort1 == byte .class ) {
670
+ } else if (from0 == int .class ) {
671
+ if (from1 == byte .class ) {
713
672
if (const0 != null ) {
714
673
final int constant = (int )const0 ;
715
674
716
675
if (constant <= Byte .MAX_VALUE && constant >= Byte .MIN_VALUE ) {
717
- return definition . byteType ;
676
+ return byte . class ;
718
677
}
719
678
}
720
679
721
- return definition . intType ;
722
- } else if (sort1 == short .class ) {
680
+ return int . class ;
681
+ } else if (from1 == short .class ) {
723
682
if (const0 != null ) {
724
683
final int constant = (int )const0 ;
725
684
726
685
if (constant <= Short .MAX_VALUE && constant >= Short .MIN_VALUE ) {
727
- return definition . byteType ;
686
+ return byte . class ;
728
687
}
729
688
}
730
689
731
- return definition . intType ;
732
- } else if (sort1 == char .class ) {
690
+ return int . class ;
691
+ } else if (from1 == char .class ) {
733
692
if (const0 != null ) {
734
693
final int constant = (int )const0 ;
735
694
736
695
if (constant <= Character .MAX_VALUE && constant >= Character .MIN_VALUE ) {
737
- return definition . byteType ;
696
+ return byte . class ;
738
697
}
739
698
}
740
699
741
- return definition . intType ;
742
- } else if (sort1 == int .class ) {
743
- return definition . intType ;
700
+ return int . class ;
701
+ } else if (from1 == int .class ) {
702
+ return int . class ;
744
703
}
745
704
}
746
705
}
@@ -750,6 +709,10 @@ public Type promoteConditional(Type from0, Type from1, Object const0, Object con
750
709
// TODO: to calculate the highest upper bound for the two types and return that.
751
710
// TODO: However, for now we just return objectType that may require an extra cast.
752
711
753
- return definition .ObjectType ;
712
+ return Object .class ;
713
+ }
714
+
715
+ private AnalyzerCaster () {
716
+
754
717
}
755
718
}
0 commit comments