painless: Add synthetic length property as alias to Lists, so they can really be used like arrays

This commit is contained in:
Uwe Schindler 2016-05-10 15:01:49 +02:00
parent b6a1491cd8
commit 243c9e77fa
2 changed files with 11 additions and 0 deletions

View File

@ -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<Object>", "set", null, false, objectType, new Type[] {intType, objectType}, null, null);
addMethod("List<Object>", "get", null, false, objectType, new Type[] {intType}, null, null);
addMethod("List<Object>", "remove", null, false, objectType, new Type[] {intType}, null, null);
addMethod("List<Object>", "getLength", "size", false, intType, new Type[] {}, null, null);
addConstructor("ArrayList<Object>", "new", new Type[] {}, null);
@ -1074,6 +1076,7 @@ class Definition {
new Type[] {intType, stringType});
addMethod("List<String>", "get", null, false, objectType, new Type[] {intType}, stringType, null);
addMethod("List<String>", "remove", null, false, objectType, new Type[] {intType}, stringType, null);
addMethod("List<String>", "getLength", "size", false, intType, new Type[] {}, null, null);
addConstructor("ArrayList<String>", "new", new Type[] {}, null);

View File

@ -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<String> x = new ArrayList<String>(); x.add('Hallo'); return x.length"));
assertEquals(1, exec("List<Object> x = new ArrayList<Object>(); x.add('Hallo'); return x.length"));
}
}