From 243c9e77fa44d352e218ca05cb431fb38baa9aea Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Tue, 10 May 2016 15:01:49 +0200 Subject: [PATCH] painless: Add synthetic length property as alias to Lists, so they can really be used like arrays --- .../main/java/org/elasticsearch/painless/Definition.java | 3 +++ .../java/org/elasticsearch/painless/BasicAPITests.java | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java index f418edd2beb..a3ee43b00a5 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java @@ -1061,12 +1061,14 @@ class Definition { addMethod("List", "set", null, false, objectType, new Type[] {intType, objectType}, defType, new Type[] {intType, defType}); addMethod("List", "get", null, false, objectType, new Type[] {intType}, defType, null); addMethod("List", "remove", null, false, objectType, new Type[] {intType}, defType, null); + addMethod("List", "getLength", "size", false, intType, new Type[] {}, null, null); addConstructor("ArrayList", "new", new Type[] {}, null); addMethod("List", "set", null, false, objectType, new Type[] {intType, objectType}, null, null); addMethod("List", "get", null, false, objectType, new Type[] {intType}, null, null); addMethod("List", "remove", null, false, objectType, new Type[] {intType}, null, null); + addMethod("List", "getLength", "size", false, intType, new Type[] {}, null, null); addConstructor("ArrayList", "new", new Type[] {}, null); @@ -1074,6 +1076,7 @@ class Definition { new Type[] {intType, stringType}); addMethod("List", "get", null, false, objectType, new Type[] {intType}, stringType, null); addMethod("List", "remove", null, false, objectType, new Type[] {intType}, stringType, null); + addMethod("List", "getLength", "size", false, intType, new Type[] {}, null, null); addConstructor("ArrayList", "new", new Type[] {}, null); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java index 2a14dad375a..b1df5276feb 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java @@ -73,4 +73,12 @@ public class BasicAPITests extends ScriptTestCase { assertEquals(5, exec("def x = new ArrayList(); x.add(5); return x.get(0);")); assertEquals(5, exec("def x = new ArrayList(); x.add(5); def index = 0; return x.get(index);")); } + + public void testListAsArray() { + assertEquals(1, exec("def x = new ArrayList(); x.add(5); return x.length")); + assertEquals(5, exec("def x = new ArrayList(); x.add(5); return x[0]")); + assertEquals(1, exec("List x = new ArrayList(); x.add('Hallo'); return x.length")); + assertEquals(1, exec("List x = new ArrayList(); x.add('Hallo'); return x.length")); + assertEquals(1, exec("List x = new ArrayList(); x.add('Hallo'); return x.length")); + } }