-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate Painless Whitelist Loading from the Painless Definition #26540
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
42ac913
Added whitelist class.
jdconrad 926f3fd
Update definition to use whitelist.
jdconrad c01c7df
Fix definition copy struct checks.
jdconrad 10a0e18
Started to add JavaDocs to whitelist.
jdconrad 6f66bb8
More docs.
jdconrad 6e8cf28
Merge remote-tracking branch 'origin/master' into whitelist6
jdconrad 44c741d
Merge branch 'master' into whitelist6
jdconrad aa196d5
Merge branch 'master' into whitelist6
jdconrad 27b887d
Merge branch 'master' into whitelist7
jdconrad 264630d
Merge branch 'master' into whitelist7
jdconrad c55781e
More docs.
jdconrad 6229639
More docs.
jdconrad b64e2a0
Merge branch 'master' into whitelist7
jdconrad 03c558d
Merge branch 'master' into whitelist7
jdconrad 8206702
More docs/fixes to names.
jdconrad d745492
More docs.
jdconrad d061641
More docs.
jdconrad 6ebc0a6
More docs.
jdconrad d2177b8
More docs.
jdconrad 071f3c1
Merge branch 'master' into whitelist7
jdconrad 4074967
Update whitelist files with new formatting.
jdconrad 34ae74d
Merge branch 'master' into whitelist7
jdconrad fe0af04
Define dynamic type as singular.
jdconrad 1f72a78
Merge branch 'master' into whitelist7
jdconrad 94f47f7
Progress.
jdconrad 3dcb39a
Merge branch 'master' into whitelist9
jdconrad 2eb4503
Updates to whitelist.
jdconrad 40afc5d
Partially updated Definition to work with multiple whitelists.
jdconrad 50dc439
checkpoint
jdconrad 69b574f
Merge branch 'master' into whitelist9
jdconrad 8e739e8
checkpoint
jdconrad ec407b2
Merge branch 'master' into whitelist9
jdconrad cbabad2
Updated method loading in Definition for whitelists.
jdconrad d650610
Updated fields in Definition to use Whitelist.
jdconrad 3f304ed
Add super classes to structs.
jdconrad bcd428f
Automatically figure out super classes/interfaces for structs.
jdconrad f9ea879
Updated whitelist files and added automatic generation of extends.
jdconrad bccf35b
Fix bugs.
jdconrad 182144f
Merge branch 'master' into whitelist9
jdconrad b5df427
Merge branch 'master' into whitelist9
jdconrad 99afd47
Responce to PR comments.
jdconrad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
706 changes: 359 additions & 347 deletions
706
modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java
Large diffs are not rendered by default.
Oops, something went wrong.
198 changes: 198 additions & 0 deletions
198
modules/lang-painless/src/main/java/org/elasticsearch/painless/Whitelist.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.painless; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Whitelist contains data structures designed to be used to generate a white-list of Java classes, | ||
* constructors, methods, and fields that can be used within a Painless script at both compile-time | ||
* and run-time. | ||
* | ||
* A white-list consists of several pieces with {@link Struct}s as the top level. Each {@link Struct} | ||
* will contain zero-to-many {@link Constructor}s, {@link Method}s, and {@link Field}s which are what | ||
* will be available with a Painless script. See each individual white-list object for more detail. | ||
*/ | ||
public final class Whitelist { | ||
|
||
/** | ||
* Struct represents the equivalent of a Java class in Painless complete with super classes, | ||
* constructors, methods, and fields. In Painless a class is known as a struct primarily to avoid | ||
* naming conflicts internally. There must be a one-to-one mapping of struct names to Java classes. | ||
* Though, since multiple white-lists may be combined into a single white-list for a specific | ||
* {@link org.elasticsearch.script.ScriptContext}, as long as multiple structs representing the same | ||
* Java class have the same Painless type name and have legal constructor/method overloading they | ||
* can be merged together. | ||
* | ||
* Structs in Painless allow for arity overloading for constructors and methods. Arity overloading | ||
* means that multiple constructors are allowed for a single struct as long as they have a different | ||
* number of parameter types, and multiples methods with the same name are allowed for a single struct | ||
* as long as they have the same return type and a different number of parameter types. | ||
* | ||
* Structs will automatically extend other white-listed structs if the Java class they represent is a | ||
* subclass of other structs including Java interfaces. | ||
*/ | ||
public static final class Struct { | ||
|
||
/** Information about where this struct was white-listed from. Can be used for error messages. */ | ||
public final String origin; | ||
|
||
/** The Painless name of this struct which will also be the name of a type in a Painless script. */ | ||
public final String painlessTypeName; | ||
|
||
/** The Java class name this struct represents. */ | ||
public final String javaClassName; | ||
|
||
/** The {@link List} of white-listed ({@link Constructor}s) available to this struct. */ | ||
public final List<Constructor> whitelistConstructors; | ||
|
||
/** The {@link List} of white-listed ({@link Method}s) available to this struct. */ | ||
public final List<Method> whitelistMethods; | ||
|
||
/** The {@link List} of white-listed ({@link Field}s) available to this struct. */ | ||
public final List<Field> whitelistFields; | ||
|
||
/** Standard constructor. All values must be not {@code null}. */ | ||
public Struct(String origin, String painlessTypeName, String javaClassName, | ||
List<Constructor> whitelistConstructors, List<Method> whitelistMethods, List<Field> whitelistFields) { | ||
this.origin = Objects.requireNonNull(origin); | ||
this.painlessTypeName = Objects.requireNonNull(painlessTypeName); | ||
this.javaClassName = Objects.requireNonNull(javaClassName); | ||
|
||
this.whitelistConstructors = Collections.unmodifiableList(Objects.requireNonNull(whitelistConstructors)); | ||
this.whitelistMethods = Collections.unmodifiableList(Objects.requireNonNull(whitelistMethods)); | ||
this.whitelistFields = Collections.unmodifiableList(Objects.requireNonNull(whitelistFields)); | ||
} | ||
} | ||
|
||
/** | ||
* Constructor represents the equivalent of a Java constructor available as a white-listed struct | ||
* constructor within Painless. Constructors for Painless structs may be accessed exactly as | ||
* constructors for Java classes are using the 'new' keyword. Painless structs may have multiple | ||
* constructors as long as they comply with arity overloading described for {@link Struct}. | ||
*/ | ||
public static final class Constructor { | ||
|
||
/** Information about where this constructor was white-listed from. Can be used for error messages. */ | ||
public final String origin; | ||
|
||
/** | ||
* A {@link List} of {@link String}s that are the Painless type names for the parameters of the | ||
* constructor which can be used to look up the Java constructor through reflection. | ||
*/ | ||
public final List<String> painlessParameterTypeNames; | ||
|
||
/** Standard constructor. All values must be not {@code null}. */ | ||
public Constructor(String origin, List<String> painlessParameterTypeNames) { | ||
this.origin = Objects.requireNonNull(origin); | ||
this.painlessParameterTypeNames = Collections.unmodifiableList(Objects.requireNonNull(painlessParameterTypeNames)); | ||
} | ||
} | ||
|
||
/** | ||
* Method represents the equivalent of a Java method available as a white-listed struct method | ||
* within Painless. Methods for Painless structs may be accessed exactly as methods for Java classes | ||
* are using the '.' operator on an existing struct variable/field. Painless structs may have multiple | ||
* methods with the same name as long as they comply with arity overloading described for {@link Method}. | ||
* | ||
* Structs may also have additional methods that are not part of the Java class the struct represents - | ||
* these are known as augmented methods. An augmented method can be added to a struct as a part of any | ||
* Java class as long as the method is static and the first parameter of the method is the Java class | ||
* represented by the struct. Note that the augmented method's parent Java class does not need to be | ||
* white-listed. | ||
*/ | ||
public static class Method { | ||
|
||
/** Information about where this method was white-listed from. Can be used for error messages. */ | ||
public final String origin; | ||
|
||
/** | ||
* The Java class name for the owner of an augmented method. If the method is not augmented | ||
* this should be {@code null}. | ||
*/ | ||
public final String javaAugmentedClassName; | ||
|
||
/** The Java method name used to look up the Java method through reflection. */ | ||
public final String javaMethodName; | ||
|
||
/** | ||
* The Painless type name for the return type of the method which can be used to look up the Java | ||
* method through reflection. | ||
*/ | ||
public final String painlessReturnTypeName; | ||
|
||
/** | ||
* A {@link List} of {@link String}s that are the Painless type names for the parameters of the | ||
* method which can be used to look up the Java method through reflection. | ||
*/ | ||
public final List<String> painlessParameterTypeNames; | ||
|
||
/** | ||
* Standard constructor. All values must be not {@code null} with the exception of jAugmentedClass; | ||
* jAugmentedClass will be {@code null} unless the method is augmented as described in the class documentation. | ||
*/ | ||
public Method(String origin, String javaAugmentedClassName, String javaMethodName, | ||
String painlessReturnTypeName, List<String> painlessParameterTypeNames) { | ||
this.origin = Objects.requireNonNull(origin); | ||
this.javaAugmentedClassName = javaAugmentedClassName; | ||
this.javaMethodName = javaMethodName; | ||
this.painlessReturnTypeName = Objects.requireNonNull(painlessReturnTypeName); | ||
this.painlessParameterTypeNames = Collections.unmodifiableList(Objects.requireNonNull(painlessParameterTypeNames)); | ||
} | ||
} | ||
|
||
/** | ||
* Field represents the equivalent of a Java field available as a white-listed struct field | ||
* within Painless. Fields for Painless structs may be accessed exactly as fields for Java classes | ||
* are using the '.' operator on an existing struct variable/field. | ||
*/ | ||
public static class Field { | ||
|
||
/** Information about where this method was white-listed from. Can be used for error messages. */ | ||
public final String origin; | ||
|
||
/** The Java field name used to look up the Java field through reflection. */ | ||
public final String javaFieldName; | ||
|
||
/** The Painless type name for the field which can be used to look up the Java field through reflection. */ | ||
public final String painlessFieldTypeName; | ||
|
||
/** Standard constructor. All values must be not {@code null}. */ | ||
public Field(String origin, String javaFieldName, String painlessFieldTypeName) { | ||
this.origin = Objects.requireNonNull(origin); | ||
this.javaFieldName = Objects.requireNonNull(javaFieldName); | ||
this.painlessFieldTypeName = Objects.requireNonNull(painlessFieldTypeName); | ||
} | ||
} | ||
|
||
/** The {@link ClassLoader} used to look up the white-listed Java classes, constructors, methods, and fields. */ | ||
public final ClassLoader javaClassLoader; | ||
|
||
/** The {@link List} of all the white-listed Painless structs. */ | ||
public final List<Struct> whitelistStructs; | ||
|
||
/** Standard constructor. All values must be not {@code null}. */ | ||
public Whitelist(ClassLoader javaClassLoader, List<Struct> whitelistStructs) { | ||
this.javaClassLoader = Objects.requireNonNull(javaClassLoader); | ||
this.whitelistStructs = Collections.unmodifiableList(Objects.requireNonNull(whitelistStructs)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should consider (in a followup) renaming Struct to Class. We use the same name everywhere else as the java class (eg Method), so I think we should be consistent, and it will decrease confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.