19
19
20
20
package org .elasticsearch .painless ;
21
21
22
- import org .apache .lucene .util .Constants ;
23
22
import org .apache .lucene .util .SetOnce ;
24
23
25
24
import java .lang .invoke .MethodHandle ;
26
25
import java .lang .invoke .MethodHandles ;
27
26
import java .lang .invoke .MethodType ;
28
27
import java .lang .reflect .Modifier ;
29
- import java .time .LocalDate ;
30
28
import java .util .ArrayList ;
31
- import java .util .Arrays ;
32
29
import java .util .Collection ;
33
30
import java .util .Collections ;
34
31
import java .util .HashMap ;
35
32
import java .util .List ;
36
33
import java .util .Map ;
37
34
import java .util .Objects ;
38
- import java .util .PrimitiveIterator ;
39
- import java .util .Spliterator ;
40
35
import java .util .Stack ;
41
36
42
37
/**
@@ -65,37 +60,37 @@ public final class Definition {
65
60
/**
66
61
* Whitelist that is "built in" to Painless and required by all scripts.
67
62
*/
68
- public static final Definition BUILTINS = new Definition (
63
+ public static final Definition DEFINITION = new Definition (
69
64
Collections .singletonList (WhitelistLoader .loadFromResourceFiles (Definition .class , DEFINITION_FILES )));
70
65
71
66
/** Some native types as constants: */
72
- public static final Type VOID_TYPE = BUILTINS . getType ( "void" ) ;
73
- public static final Type BOOLEAN_TYPE = BUILTINS . getType ( "boolean" ) ;
74
- public static final Type BOOLEAN_OBJ_TYPE = BUILTINS . getType ( "Boolean" ) ;
75
- public static final Type BYTE_TYPE = BUILTINS . getType ( "byte" ) ;
76
- public static final Type BYTE_OBJ_TYPE = BUILTINS . getType ( "Byte" ) ;
77
- public static final Type SHORT_TYPE = BUILTINS . getType ( "short" ) ;
78
- public static final Type SHORT_OBJ_TYPE = BUILTINS . getType ( "Short" ) ;
79
- public static final Type INT_TYPE = BUILTINS . getType ( "int" ) ;
80
- public static final Type INT_OBJ_TYPE = BUILTINS . getType ( "Integer" ) ;
81
- public static final Type LONG_TYPE = BUILTINS . getType ( "long" ) ;
82
- public static final Type LONG_OBJ_TYPE = BUILTINS . getType ( "Long" ) ;
83
- public static final Type FLOAT_TYPE = BUILTINS . getType ( "float" ) ;
84
- public static final Type FLOAT_OBJ_TYPE = BUILTINS . getType ( "Float" ) ;
85
- public static final Type DOUBLE_TYPE = BUILTINS . getType ( "double" ) ;
86
- public static final Type DOUBLE_OBJ_TYPE = BUILTINS . getType ( "Double" ) ;
87
- public static final Type CHAR_TYPE = BUILTINS . getType ( "char" ) ;
88
- public static final Type CHAR_OBJ_TYPE = BUILTINS . getType ( "Character" ) ;
89
- public static final Type OBJECT_TYPE = BUILTINS . getType ( "Object" ) ;
90
- public static final Type DEF_TYPE = BUILTINS . getType ( "def" ) ;
91
- public static final Type NUMBER_TYPE = BUILTINS . getType ( "Number" ) ;
92
- public static final Type STRING_TYPE = BUILTINS . getType ( "String" ) ;
93
- public static final Type EXCEPTION_TYPE = BUILTINS . getType ( "Exception" ) ;
94
- public static final Type PATTERN_TYPE = BUILTINS . getType ( "Pattern" ) ;
95
- public static final Type MATCHER_TYPE = BUILTINS . getType ( "Matcher" ) ;
96
- public static final Type ITERATOR_TYPE = BUILTINS . getType ( "Iterator" ) ;
97
- public static final Type ARRAY_LIST_TYPE = BUILTINS . getType ( "ArrayList" ) ;
98
- public static final Type HASH_MAP_TYPE = BUILTINS . getType ( "HashMap" ) ;
67
+ public final Type voidType ;
68
+ public final Type booleanType ;
69
+ public final Type BooleanType ;
70
+ public final Type byteType ;
71
+ public final Type ByteType ;
72
+ public final Type shortType ;
73
+ public final Type ShortType ;
74
+ public final Type intType ;
75
+ public final Type IntegerType ;
76
+ public final Type longType ;
77
+ public final Type LongType ;
78
+ public final Type floatType ;
79
+ public final Type FloatType ;
80
+ public final Type doubleType ;
81
+ public final Type DoubleType ;
82
+ public final Type charType ;
83
+ public final Type CharacterType ;
84
+ public final Type ObjectType ;
85
+ public final Type DefType ;
86
+ public final Type NumberType ;
87
+ public final Type StringType ;
88
+ public final Type ExceptionType ;
89
+ public final Type PatternType ;
90
+ public final Type MatcherType ;
91
+ public final Type IteratorType ;
92
+ public final Type ArrayListType ;
93
+ public final Type HashMapType ;
99
94
100
95
public static final class Type {
101
96
public final String name ;
@@ -438,58 +433,58 @@ public Struct getStruct() {
438
433
439
434
/** Returns whether or not a non-array type exists. */
440
435
public boolean isSimpleType (final String name ) {
441
- return BUILTINS . structsMap .containsKey (name );
436
+ return structsMap .containsKey (name );
442
437
}
443
438
444
439
/** Gets the type given by its name */
445
440
public Type getType (final String name ) {
446
- return BUILTINS . getTypeInternal (name );
441
+ return getTypeInternal (name );
447
442
}
448
443
449
444
/** Creates an array type from the given Struct. */
450
445
public Type getType (final Struct struct , final int dimensions ) {
451
- return BUILTINS . getTypeInternal (struct , dimensions );
446
+ return getTypeInternal (struct , dimensions );
452
447
}
453
448
454
- public static Type getBoxedType (Type unboxed ) {
449
+ public Type getBoxedType (Type unboxed ) {
455
450
if (unboxed .clazz == boolean .class ) {
456
- return BOOLEAN_OBJ_TYPE ;
451
+ return BooleanType ;
457
452
} else if (unboxed .clazz == byte .class ) {
458
- return BYTE_OBJ_TYPE ;
453
+ return ByteType ;
459
454
} else if (unboxed .clazz == short .class ) {
460
- return SHORT_OBJ_TYPE ;
455
+ return ShortType ;
461
456
} else if (unboxed .clazz == char .class ) {
462
- return CHAR_OBJ_TYPE ;
457
+ return CharacterType ;
463
458
} else if (unboxed .clazz == int .class ) {
464
- return INT_OBJ_TYPE ;
459
+ return IntegerType ;
465
460
} else if (unboxed .clazz == long .class ) {
466
- return LONG_OBJ_TYPE ;
461
+ return LongType ;
467
462
} else if (unboxed .clazz == float .class ) {
468
- return FLOAT_OBJ_TYPE ;
463
+ return FloatType ;
469
464
} else if (unboxed .clazz == double .class ) {
470
- return DOUBLE_OBJ_TYPE ;
465
+ return DoubleType ;
471
466
}
472
467
473
468
return unboxed ;
474
469
}
475
470
476
- public static Type getUnboxedType (Type boxed ) {
471
+ public Type getUnboxedType (Type boxed ) {
477
472
if (boxed .clazz == Boolean .class ) {
478
- return BOOLEAN_TYPE ;
473
+ return booleanType ;
479
474
} else if (boxed .clazz == Byte .class ) {
480
- return BYTE_TYPE ;
475
+ return byteType ;
481
476
} else if (boxed .clazz == Short .class ) {
482
- return SHORT_TYPE ;
477
+ return shortType ;
483
478
} else if (boxed .clazz == Character .class ) {
484
- return CHAR_TYPE ;
479
+ return charType ;
485
480
} else if (boxed .clazz == Integer .class ) {
486
- return INT_TYPE ;
481
+ return intType ;
487
482
} else if (boxed .clazz == Long .class ) {
488
- return LONG_TYPE ;
483
+ return longType ;
489
484
} else if (boxed .clazz == Float .class ) {
490
- return FLOAT_TYPE ;
485
+ return floatType ;
491
486
} else if (boxed .clazz == Double .class ) {
492
- return DOUBLE_TYPE ;
487
+ return doubleType ;
493
488
}
494
489
495
490
return boxed ;
@@ -508,12 +503,12 @@ public static boolean isConstantType(Type constant) {
508
503
}
509
504
510
505
public RuntimeClass getRuntimeClass (Class <?> clazz ) {
511
- return BUILTINS . runtimeMap .get (clazz );
506
+ return runtimeMap .get (clazz );
512
507
}
513
508
514
509
/** Collection of all simple types. Used by {@code PainlessDocGenerator} to generate an API reference. */
515
- static Collection <Type > allSimpleTypes () {
516
- return BUILTINS . simpleTypesMap .values ();
510
+ Collection <Type > allSimpleTypes () {
511
+ return simpleTypesMap .values ();
517
512
}
518
513
519
514
// INTERNAL IMPLEMENTATION:
@@ -522,6 +517,8 @@ static Collection<Type> allSimpleTypes() {
522
517
private final Map <String , Struct > structsMap ;
523
518
private final Map <String , Type > simpleTypesMap ;
524
519
520
+ public AnalyzerCaster caster ;
521
+
525
522
private Definition (List <Whitelist > whitelists ) {
526
523
structsMap = new HashMap <>();
527
524
simpleTypesMap = new HashMap <>();
@@ -648,6 +645,36 @@ private Definition(List<Whitelist> whitelists) {
648
645
for (final Map .Entry <String ,Struct > entry : structsMap .entrySet ()) {
649
646
entry .setValue (entry .getValue ().freeze ());
650
647
}
648
+
649
+ voidType = getType ("void" );
650
+ booleanType = getType ("boolean" );
651
+ BooleanType = getType ("Boolean" );
652
+ byteType = getType ("byte" );
653
+ ByteType = getType ("Byte" );
654
+ shortType = getType ("short" );
655
+ ShortType = getType ("Short" );
656
+ intType = getType ("int" );
657
+ IntegerType = getType ("Integer" );
658
+ longType = getType ("long" );
659
+ LongType = getType ("Long" );
660
+ floatType = getType ("float" );
661
+ FloatType = getType ("Float" );
662
+ doubleType = getType ("double" );
663
+ DoubleType = getType ("Double" );
664
+ charType = getType ("char" );
665
+ CharacterType = getType ("Character" );
666
+ ObjectType = getType ("Object" );
667
+ DefType = getType ("def" );
668
+ NumberType = getType ("Number" );
669
+ StringType = getType ("String" );
670
+ ExceptionType = getType ("Exception" );
671
+ PatternType = getType ("Pattern" );
672
+ MatcherType = getType ("Matcher" );
673
+ IteratorType = getType ("Iterator" );
674
+ ArrayListType = getType ("ArrayList" );
675
+ HashMapType = getType ("HashMap" );
676
+
677
+ caster = new AnalyzerCaster (this );
651
678
}
652
679
653
680
private void addStruct (ClassLoader whitelistClassLoader , Whitelist .Struct whitelistStruct ) {
0 commit comments