-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to allow list instance methods on the script class (#76045)
This change adds support to allow list instance methods on the script class in Painless and super classes of the script class. It works the same way as allow listing works now for other classes.
- Loading branch information
Showing
7 changed files
with
206 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
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
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
106 changes: 106 additions & 0 deletions
106
modules/lang-painless/src/test/java/org/elasticsearch/painless/ThisTests.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,106 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.painless; | ||
|
||
import org.elasticsearch.painless.spi.Whitelist; | ||
import org.elasticsearch.painless.spi.WhitelistLoader; | ||
import org.elasticsearch.script.ScriptContext; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ThisTests extends ScriptTestCase { | ||
|
||
public abstract static class ThisBaseScript { | ||
|
||
protected String baseString; | ||
|
||
public ThisBaseScript(String baseString) { | ||
this.baseString = baseString; | ||
} | ||
|
||
public String getBaseString() { | ||
return baseString; | ||
} | ||
|
||
public void setBaseString(String testString) { | ||
this.baseString = testString; | ||
} | ||
|
||
public int getBaseLength() { | ||
return baseString.length(); | ||
} | ||
} | ||
|
||
public abstract static class ThisScript extends ThisBaseScript { | ||
|
||
protected String thisString; | ||
|
||
public ThisScript(String baseString, String thisString) { | ||
super(baseString); | ||
|
||
this.thisString = thisString; | ||
} | ||
|
||
public String thisString() { | ||
return thisString; | ||
} | ||
|
||
public void thisString(String testString) { | ||
this.thisString = testString; | ||
} | ||
|
||
public int thisLength() { | ||
return thisString.length(); | ||
} | ||
|
||
public abstract Object execute(); | ||
|
||
public interface Factory { | ||
|
||
ThisScript newInstance(String baseString, String testString); | ||
} | ||
|
||
public static final String[] PARAMETERS = {}; | ||
public static final ScriptContext<ThisScript.Factory> CONTEXT = | ||
new ScriptContext<>("this_test", ThisScript.Factory.class); | ||
} | ||
|
||
@Override | ||
protected Map<ScriptContext<?>, List<Whitelist>> scriptContexts() { | ||
Map<ScriptContext<?>, List<Whitelist>> contexts = new HashMap<>(); | ||
List<Whitelist> whitelists = new ArrayList<>(Whitelist.BASE_WHITELISTS); | ||
whitelists.add(WhitelistLoader.loadFromResourceFiles(Whitelist.class, "org.elasticsearch.painless.this")); | ||
contexts.put(ThisScript.CONTEXT, whitelists); | ||
return contexts; | ||
} | ||
|
||
public Object exec(String script, String baseString, String testString) { | ||
ThisScript.Factory factory = scriptEngine.compile(null, script, ThisScript.CONTEXT, new HashMap<>()); | ||
ThisScript testThisScript = factory.newInstance(baseString, testString); | ||
return testThisScript.execute(); | ||
} | ||
|
||
public void testThisMethods() { | ||
assertEquals("basethis", exec("getBaseString() + thisString()", "base", "this")); | ||
assertEquals(8, exec("getBaseLength() + thisLength()", "yyy", "xxxxx")); | ||
|
||
List<String> result = new ArrayList<>(); | ||
result.add("this"); | ||
result.add("base"); | ||
assertEquals(result, exec("List result = []; " + | ||
"thisString('this');" + | ||
"setBaseString('base');" + | ||
"result.add(thisString()); " + | ||
"result.add(getBaseString());" + | ||
"result;", "", "")); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ainless/src/test/resources/org/elasticsearch/painless/spi/org.elasticsearch.painless.this
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,12 @@ | ||
class org.elasticsearch.painless.ThisTests$ThisBaseScript @no_import { | ||
String getBaseString(); | ||
void setBaseString(String); | ||
int getBaseLength(); | ||
} | ||
|
||
|
||
class org.elasticsearch.painless.ThisTests$ThisScript @no_import { | ||
String thisString(); | ||
void thisString(String); | ||
int thisLength(); | ||
} |