LUCENE-6454: Added distinction between member variable and method in expression helper VariableContext

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1677022 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan Ernst 2015-04-30 16:43:34 +00:00
parent 89b84f7f46
commit f829d6be3e
3 changed files with 40 additions and 4 deletions

View File

@ -88,6 +88,10 @@ New Features
* LUCENE-6083: Add SpanWithinQuery and SpanContainingQuery that return
spans inside of / containing another spans. (Paul Elschot via Robert Muir)
* LUCENE-6454: Added distinction between member variable and method in
expression helper VariableContext
(Jack Conradson via Ryan Ernst)
Optimizations
* LUCENE-6379: IndexWriter.deleteDocuments(Query...) now detects if

View File

@ -41,9 +41,14 @@ public class VariableContext {
STR_INDEX,
/**
* Brackets containg an integer index (ie an array).
* Brackets containing an integer index (ie an array).
*/
INT_INDEX
INT_INDEX,
/**
* Parenthesis represent a member method to be called.
*/
METHOD
}
/**
@ -93,8 +98,13 @@ public class VariableContext {
// i points to start of member name
private static int addMember(final char[] text, int i, List<VariableContext> contexts) {
int j = i + 1;
while (j < text.length && text[j] != '[' && text[j] != '.') ++j; // find first array or member access
contexts.add(new VariableContext(Type.MEMBER, new String(text, i, j - i), -1));
while (j < text.length && text[j] != '[' && text[j] != '.' && text[j] != '(') ++j; // find first array, member access, or method call
if (j + 1 < text.length && text[j] == '(' && text[j + 1] == ')') {
contexts.add(new VariableContext(Type.METHOD, new String(text, i, j - i), -1));
j += 2; //move past the parenthesis
} else {
contexts.add(new VariableContext(Type.MEMBER, new String(text, i, j - i), -1));
}
return j;
}

View File

@ -22,6 +22,7 @@ import org.apache.lucene.util.LuceneTestCase;
import static org.apache.lucene.expressions.js.VariableContext.Type.MEMBER;
import static org.apache.lucene.expressions.js.VariableContext.Type.STR_INDEX;
import static org.apache.lucene.expressions.js.VariableContext.Type.INT_INDEX;
import static org.apache.lucene.expressions.js.VariableContext.Type.METHOD;
public class TestVariableContext extends LuceneTestCase {
@ -66,4 +67,25 @@ public class TestVariableContext extends LuceneTestCase {
assertEquals(x[2].type, INT_INDEX);
assertEquals(x[2].integer, 1);
}
public void testMethodWithMember() {
VariableContext[] x = VariableContext.parse("m.m()");
assertEquals(2, x.length);
assertEquals(x[1].type, METHOD);
assertEquals(x[1].text, "m");
}
public void testMethodWithStrIndex() {
VariableContext[] x = VariableContext.parse("member['blah'].getMethod()");
assertEquals(3, x.length);
assertEquals(x[2].type, METHOD);
assertEquals(x[2].text, "getMethod");
}
public void testMethodWithNumericalIndex() {
VariableContext[] x = VariableContext.parse("member[0].getMethod()");
assertEquals(3, x.length);
assertEquals(x[2].type, METHOD);
assertEquals(x[2].text, "getMethod");
}
}