Skip to content
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

Painless: Add Static Methods Shortcut #33440

Merged
merged 7 commits into from
Sep 8, 2018
Merged

Conversation

jdconrad
Copy link
Contributor

@jdconrad jdconrad commented Sep 5, 2018

Static methods in Painless can be whitelisted using the static section with the same format as Painless bindings except using the 'from' keyword instead of the 'bound_to' keyword. The following format is used:

static {
    double max(double, double) from java.lang.Math
}

and will be called as

...
return max(5, var)

@jdconrad jdconrad added >enhancement :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v7.0.0 v6.5.0 labels Sep 5, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@jdconrad jdconrad mentioned this pull request Sep 5, 2018
23 tasks
@jdconrad
Copy link
Contributor Author

jdconrad commented Sep 6, 2018

@elasticmachine test this please

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, a few minor requests.

@@ -61,12 +61,19 @@
/** The {@link List} of all the whitelisted Painless classes. */
public final List<WhitelistClass> whitelistClasses;

/** The {@link List} of all the whitelisted static Painless methods. */
public final List<WhitelistMethod> whitelistStatics;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: all these names start with "whitelist" but isn't that implied since they are in the whitelist class? Also, bindings are also in the static section, so statics seems to broad, unless this is staticMethods and bindings below are staticBindings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change this in a separate PR as we discussed.

addPainlessStatic(targetClass, methodName, returnType, typeParameters);
}

public void addPainlessStatic(Class<?> targetClass, String methodName, Class<?> returnType, List<Class<?>> typeParameters) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be public? It's only called above right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this set of methods to be used for loading defaults and I want them to remain public to indicate they can be used to build a custom lookup for either tests or possibly other sources of loading.

@@ -937,6 +1089,11 @@ public void addPainlessBinding(Class<?> targetClass, String methodName, Class<?>
}

String painlessMethodKey = buildPainlessMethodKey(methodName, constructorTypeParametersSize + methodTypeParametersSize);

if (painlessMethodKeysToPainlessStatics.containsKey(painlessMethodKey)) {
throw new IllegalArgumentException("binding and static cannot have the same name [" + methodName + "]");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static -> static methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to imported methods for all of these related to this feature. Will rename bindings to classbindings in the instancebindings PR or a separate PR.

@@ -177,5 +177,6 @@ class org.elasticsearch.painless.FeatureTest no_import {

# for testing
static {
float staticAddFloatsTest(float, float) from org.elasticsearch.painless.FeatureTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(For a followup): can we move these test specific cases out of the base whitelist, since we have whitelist spi (I know we didn't have that when FeatureTest was added above)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjernst Yes, in fact given that we can do custom whitelists now, is there any reason not to move FeatureTest and BindingTest to the test framework and use a custom whitelist with those during the unit tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by test framework? These are just part of painless tests right?

Copy link
Contributor Author

@jdconrad jdconrad Sep 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was that since FeatureTest and BindingTest are part src rather than test because we whitelist them in the base whitelists so they need to be distributed as part of src. Is it correct that if we have a custom whitelist for them and add that for unit tests they could be moved to test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they need to be part of src, or in the base whitelist. They can be exposed to tests through the compiler/contexts created in ScriptTestCase.

@jdconrad
Copy link
Contributor Author

jdconrad commented Sep 7, 2018

@rjernst Thanks for the review! Will commit as soon as CI passes.

@jdconrad jdconrad merged commit facec18 into elastic:master Sep 8, 2018
jdconrad added a commit that referenced this pull request Sep 8, 2018
Allow static methods to be imported in Painless and called using just the method name.
jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Sep 9, 2018
* master:
  CORE: Make Pattern Exclusion Work with Aliases (elastic#33518)
  Reverse logic for CCR license checks (elastic#33549)
  Add latch countdown on failure in CCR license tests (elastic#33548)
  HLRC: Add put stored script support to high-level rest client (elastic#31323)
  Create temporary directory if needed in CCR test
  Add license checks for auto-follow implementation (elastic#33496)
  Bootstrap a new history_uuid when force allocating a stale primary (elastic#33432)
  INGEST: Remove Outdated TODOs (elastic#33458)
  Logging: Clean up skipping test
  Logging: Skip test if it'd fail
  CRUD: AwaitsFix entire wait_for_refresh close test
  Painless: Add Imported Static Method (elastic#33440)
@jimczi jimczi added v7.0.0-beta1 and removed v7.0.0 labels Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement v6.5.0 v7.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants