Skip to content

Commit 4d66343

Browse files
committed
Painless: Restructure Definition/Whitelist (#31879)
Create lookup package rename Definition to PainlessLookup and move to lookup package rename Definition.Method to PainlessMethod rename Definition.MethodKey to PainlessMethod rename Definition.Field to PainlessField rename Definition.Struct to PainlessClass rename Definition.Cast to PainlessCast rename Whitelist.Struct to WhitelistClass rename Whitelist.Constructor to WhitelistConstructor rename Whitelist.Method to WhitelistMethod rename Whitelist.Field to WhitelistField
1 parent 3c01f1c commit 4d66343

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1510
-1266
lines changed

modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/Whitelist.java

+9-160
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
import java.util.Objects;
2525

2626
/**
27-
* Whitelist contains data structures designed to be used to generate a white-list of Java classes,
27+
* Whitelist contains data structures designed to be used to generate a whitelist of Java classes,
2828
* constructors, methods, and fields that can be used within a Painless script at both compile-time
2929
* and run-time.
3030
*
31-
* A white-list consists of several pieces with {@link Struct}s as the top level. Each {@link Struct}
32-
* will contain zero-to-many {@link Constructor}s, {@link Method}s, and {@link Field}s which are what
33-
* will be available with a Painless script. See each individual white-list object for more detail.
31+
* A whitelist consists of several pieces with {@link WhitelistClass}s as the top level. Each
32+
* {@link WhitelistClass} will contain zero-to-many {@link WhitelistConstructor}s, {@link WhitelistMethod}s, and
33+
* {@link WhitelistField}s which are what will be available with a Painless script. See each individual
34+
* whitelist object for more detail.
3435
*/
3536
public final class Whitelist {
3637

@@ -54,166 +55,14 @@ public final class Whitelist {
5455
public static final List<Whitelist> BASE_WHITELISTS =
5556
Collections.singletonList(WhitelistLoader.loadFromResourceFiles(Whitelist.class, BASE_WHITELIST_FILES));
5657

57-
/**
58-
* Struct represents the equivalent of a Java class in Painless complete with super classes,
59-
* constructors, methods, and fields. In Painless a class is known as a struct primarily to avoid
60-
* naming conflicts internally. There must be a one-to-one mapping of struct names to Java classes.
61-
* Though, since multiple white-lists may be combined into a single white-list for a specific
62-
* {@link org.elasticsearch.script.ScriptContext}, as long as multiple structs representing the same
63-
* Java class have the same Painless type name and have legal constructor/method overloading they
64-
* can be merged together.
65-
*
66-
* Structs in Painless allow for arity overloading for constructors and methods. Arity overloading
67-
* means that multiple constructors are allowed for a single struct as long as they have a different
68-
* number of parameter types, and multiples methods with the same name are allowed for a single struct
69-
* as long as they have the same return type and a different number of parameter types.
70-
*
71-
* Structs will automatically extend other white-listed structs if the Java class they represent is a
72-
* subclass of other structs including Java interfaces.
73-
*/
74-
public static final class Struct {
75-
76-
/** Information about where this struct was white-listed from. Can be used for error messages. */
77-
public final String origin;
78-
79-
/** The Java class name this struct represents. */
80-
public final String javaClassName;
81-
82-
/**
83-
* Allow the Java class name to only be specified as the fully-qualified name.
84-
*/
85-
public final boolean onlyFQNJavaClassName;
86-
87-
/** The {@link List} of white-listed ({@link Constructor}s) available to this struct. */
88-
public final List<Constructor> whitelistConstructors;
89-
90-
/** The {@link List} of white-listed ({@link Method}s) available to this struct. */
91-
public final List<Method> whitelistMethods;
92-
93-
/** The {@link List} of white-listed ({@link Field}s) available to this struct. */
94-
public final List<Field> whitelistFields;
95-
96-
/** Standard constructor. All values must be not {@code null}. */
97-
public Struct(String origin, String javaClassName, boolean onlyFQNJavaClassName,
98-
List<Constructor> whitelistConstructors, List<Method> whitelistMethods, List<Field> whitelistFields) {
99-
this.origin = Objects.requireNonNull(origin);
100-
this.javaClassName = Objects.requireNonNull(javaClassName);
101-
this.onlyFQNJavaClassName = onlyFQNJavaClassName;
102-
103-
this.whitelistConstructors = Collections.unmodifiableList(Objects.requireNonNull(whitelistConstructors));
104-
this.whitelistMethods = Collections.unmodifiableList(Objects.requireNonNull(whitelistMethods));
105-
this.whitelistFields = Collections.unmodifiableList(Objects.requireNonNull(whitelistFields));
106-
}
107-
}
108-
109-
/**
110-
* Constructor represents the equivalent of a Java constructor available as a white-listed struct
111-
* constructor within Painless. Constructors for Painless structs may be accessed exactly as
112-
* constructors for Java classes are using the 'new' keyword. Painless structs may have multiple
113-
* constructors as long as they comply with arity overloading described for {@link Struct}.
114-
*/
115-
public static final class Constructor {
116-
117-
/** Information about where this constructor was white-listed from. Can be used for error messages. */
118-
public final String origin;
119-
120-
/**
121-
* A {@link List} of {@link String}s that are the Painless type names for the parameters of the
122-
* constructor which can be used to look up the Java constructor through reflection.
123-
*/
124-
public final List<String> painlessParameterTypeNames;
125-
126-
/** Standard constructor. All values must be not {@code null}. */
127-
public Constructor(String origin, List<String> painlessParameterTypeNames) {
128-
this.origin = Objects.requireNonNull(origin);
129-
this.painlessParameterTypeNames = Collections.unmodifiableList(Objects.requireNonNull(painlessParameterTypeNames));
130-
}
131-
}
132-
133-
/**
134-
* Method represents the equivalent of a Java method available as a white-listed struct method
135-
* within Painless. Methods for Painless structs may be accessed exactly as methods for Java classes
136-
* are using the '.' operator on an existing struct variable/field. Painless structs may have multiple
137-
* methods with the same name as long as they comply with arity overloading described for {@link Method}.
138-
*
139-
* Structs may also have additional methods that are not part of the Java class the struct represents -
140-
* these are known as augmented methods. An augmented method can be added to a struct as a part of any
141-
* Java class as long as the method is static and the first parameter of the method is the Java class
142-
* represented by the struct. Note that the augmented method's parent Java class does not need to be
143-
* white-listed.
144-
*/
145-
public static class Method {
146-
147-
/** Information about where this method was white-listed from. Can be used for error messages. */
148-
public final String origin;
149-
150-
/**
151-
* The Java class name for the owner of an augmented method. If the method is not augmented
152-
* this should be {@code null}.
153-
*/
154-
public final String javaAugmentedClassName;
155-
156-
/** The Java method name used to look up the Java method through reflection. */
157-
public final String javaMethodName;
158-
159-
/**
160-
* The Painless type name for the return type of the method which can be used to look up the Java
161-
* method through reflection.
162-
*/
163-
public final String painlessReturnTypeName;
164-
165-
/**
166-
* A {@link List} of {@link String}s that are the Painless type names for the parameters of the
167-
* method which can be used to look up the Java method through reflection.
168-
*/
169-
public final List<String> painlessParameterTypeNames;
170-
171-
/**
172-
* Standard constructor. All values must be not {@code null} with the exception of jAugmentedClass;
173-
* jAugmentedClass will be {@code null} unless the method is augmented as described in the class documentation.
174-
*/
175-
public Method(String origin, String javaAugmentedClassName, String javaMethodName,
176-
String painlessReturnTypeName, List<String> painlessParameterTypeNames) {
177-
this.origin = Objects.requireNonNull(origin);
178-
this.javaAugmentedClassName = javaAugmentedClassName;
179-
this.javaMethodName = javaMethodName;
180-
this.painlessReturnTypeName = Objects.requireNonNull(painlessReturnTypeName);
181-
this.painlessParameterTypeNames = Collections.unmodifiableList(Objects.requireNonNull(painlessParameterTypeNames));
182-
}
183-
}
184-
185-
/**
186-
* Field represents the equivalent of a Java field available as a white-listed struct field
187-
* within Painless. Fields for Painless structs may be accessed exactly as fields for Java classes
188-
* are using the '.' operator on an existing struct variable/field.
189-
*/
190-
public static class Field {
191-
192-
/** Information about where this method was white-listed from. Can be used for error messages. */
193-
public final String origin;
194-
195-
/** The Java field name used to look up the Java field through reflection. */
196-
public final String javaFieldName;
197-
198-
/** The Painless type name for the field which can be used to look up the Java field through reflection. */
199-
public final String painlessFieldTypeName;
200-
201-
/** Standard constructor. All values must be not {@code null}. */
202-
public Field(String origin, String javaFieldName, String painlessFieldTypeName) {
203-
this.origin = Objects.requireNonNull(origin);
204-
this.javaFieldName = Objects.requireNonNull(javaFieldName);
205-
this.painlessFieldTypeName = Objects.requireNonNull(painlessFieldTypeName);
206-
}
207-
}
208-
209-
/** The {@link ClassLoader} used to look up the white-listed Java classes, constructors, methods, and fields. */
58+
/** The {@link ClassLoader} used to look up the whitelisted Java classes, constructors, methods, and fields. */
21059
public final ClassLoader javaClassLoader;
21160

212-
/** The {@link List} of all the white-listed Painless structs. */
213-
public final List<Struct> whitelistStructs;
61+
/** The {@link List} of all the whitelisted Painless classes. */
62+
public final List<WhitelistClass> whitelistStructs;
21463

21564
/** Standard constructor. All values must be not {@code null}. */
216-
public Whitelist(ClassLoader javaClassLoader, List<Struct> whitelistStructs) {
65+
public Whitelist(ClassLoader javaClassLoader, List<WhitelistClass> whitelistStructs) {
21766
this.javaClassLoader = Objects.requireNonNull(javaClassLoader);
21867
this.whitelistStructs = Collections.unmodifiableList(Objects.requireNonNull(whitelistStructs));
21968
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.painless.spi;
21+
22+
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
/**
27+
* Class represents the equivalent of a Java class in Painless complete with super classes,
28+
* constructors, methods, and fields. There must be a one-to-one mapping of class names to Java
29+
* classes. Though, since multiple whitelists may be combined into a single whitelist for a
30+
* specific context, as long as multiple classes representing the same Java class have the same
31+
* class name and have legal constructor/method overloading they can be merged together.
32+
*
33+
* Classes in Painless allow for arity overloading for constructors and methods. Arity overloading
34+
* means that multiple constructors are allowed for a single class as long as they have a different
35+
* number of parameters, and multiples methods with the same name are allowed for a single class
36+
* as long as they have the same return type and a different number of parameters.
37+
*
38+
* Classes will automatically extend other whitelisted classes if the Java class they represent is a
39+
* subclass of other classes including Java interfaces.
40+
*/
41+
public final class WhitelistClass {
42+
43+
/** Information about where this class was white-listed from. Can be used for error messages. */
44+
public final String origin;
45+
46+
/** The Java class name this class represents. */
47+
public final String javaClassName;
48+
49+
/**
50+
* Allow the Java class name to only be specified as the fully-qualified name.
51+
*/
52+
public final boolean onlyFQNJavaClassName;
53+
54+
/** The {@link List} of whitelisted ({@link WhitelistConstructor}s) available to this class. */
55+
public final List<WhitelistConstructor> whitelistConstructors;
56+
57+
/** The {@link List} of whitelisted ({@link WhitelistMethod}s) available to this class. */
58+
public final List<WhitelistMethod> whitelistMethods;
59+
60+
/** The {@link List} of whitelisted ({@link WhitelistField}s) available to this class. */
61+
public final List<WhitelistField> whitelistFields;
62+
63+
/** Standard constructor. All values must be not {@code null}. */
64+
public WhitelistClass(String origin, String javaClassName, boolean onlyFQNJavaClassName,
65+
List<WhitelistConstructor> whitelistConstructors,
66+
List<WhitelistMethod> whitelistMethods,
67+
List<WhitelistField> whitelistFields) {
68+
this.origin = Objects.requireNonNull(origin);
69+
this.javaClassName = Objects.requireNonNull(javaClassName);
70+
this.onlyFQNJavaClassName = onlyFQNJavaClassName;
71+
72+
this.whitelistConstructors = Collections.unmodifiableList(Objects.requireNonNull(whitelistConstructors));
73+
this.whitelistMethods = Collections.unmodifiableList(Objects.requireNonNull(whitelistMethods));
74+
this.whitelistFields = Collections.unmodifiableList(Objects.requireNonNull(whitelistFields));
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.painless.spi;
21+
22+
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
/**
27+
* Constructor represents the equivalent of a Java constructor available as a whitelisted class
28+
* constructor within Painless. Constructors for Painless classes may be accessed exactly as
29+
* constructors for Java classes are using the 'new' keyword. Painless classes may have multiple
30+
* constructors as long as they comply with arity overloading described for {@link WhitelistClass}.
31+
*/
32+
public final class WhitelistConstructor {
33+
34+
/** Information about where this constructor was whitelisted from. Can be used for error messages. */
35+
public final String origin;
36+
37+
/**
38+
* A {@link List} of {@link String}s that are the Painless type names for the parameters of the
39+
* constructor which can be used to look up the Java constructor through reflection.
40+
*/
41+
public final List<String> painlessParameterTypeNames;
42+
43+
/** Standard constructor. All values must be not {@code null}. */
44+
public WhitelistConstructor(String origin, List<String> painlessParameterTypeNames) {
45+
this.origin = Objects.requireNonNull(origin);
46+
this.painlessParameterTypeNames = Collections.unmodifiableList(Objects.requireNonNull(painlessParameterTypeNames));
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.painless.spi;
21+
22+
import java.util.Objects;
23+
24+
/**
25+
* Field represents the equivalent of a Java field available as a whitelisted class field
26+
* within Painless. Fields for Painless classes may be accessed exactly as fields for Java classes
27+
* are using the '.' operator on an existing class variable/field.
28+
*/
29+
public class WhitelistField {
30+
31+
/** Information about where this method was whitelisted from. Can be used for error messages. */
32+
public final String origin;
33+
34+
/** The Java field name used to look up the Java field through reflection. */
35+
public final String javaFieldName;
36+
37+
/** The Painless type name for the field which can be used to look up the Java field through reflection. */
38+
public final String painlessFieldTypeName;
39+
40+
/** Standard constructor. All values must be not {@code null}. */
41+
public WhitelistField(String origin, String javaFieldName, String painlessFieldTypeName) {
42+
this.origin = Objects.requireNonNull(origin);
43+
this.javaFieldName = Objects.requireNonNull(javaFieldName);
44+
this.painlessFieldTypeName = Objects.requireNonNull(painlessFieldTypeName);
45+
}
46+
}

0 commit comments

Comments
 (0)