use of org.elasticsearch.painless.Definition.Method in project elasticsearch by elastic.
the class PField method analyze.
@Override
void analyze(Locals locals) {
prefix.analyze(locals);
prefix.expected = prefix.actual;
prefix = prefix.cast(locals);
Sort sort = prefix.actual.sort;
if (sort == Sort.ARRAY) {
sub = new PSubArrayLength(location, prefix.actual.name, value);
} else if (sort == Sort.DEF) {
sub = new PSubDefField(location, value);
} else {
Struct struct = prefix.actual.struct;
Field field = prefix instanceof EStatic ? struct.staticMembers.get(value) : struct.members.get(value);
if (field != null) {
sub = new PSubField(location, field);
} else {
Method getter = struct.methods.get(new Definition.MethodKey("get" + Character.toUpperCase(value.charAt(0)) + value.substring(1), 0));
if (getter == null) {
getter = struct.methods.get(new Definition.MethodKey("is" + Character.toUpperCase(value.charAt(0)) + value.substring(1), 0));
}
Method setter = struct.methods.get(new Definition.MethodKey("set" + Character.toUpperCase(value.charAt(0)) + value.substring(1), 1));
if (getter != null || setter != null) {
sub = new PSubShortcut(location, value, prefix.actual.name, getter, setter);
} else {
EConstant index = new EConstant(location, value);
index.analyze(locals);
if (Map.class.isAssignableFrom(prefix.actual.clazz)) {
sub = new PSubMapShortcut(location, struct, index);
}
if (List.class.isAssignableFrom(prefix.actual.clazz)) {
sub = new PSubListShortcut(location, struct, index);
}
}
}
}
if (sub == null) {
throw createError(new IllegalArgumentException("Unknown field [" + value + "] for type [" + prefix.actual.name + "]."));
}
if (nullSafe) {
sub = new PSubNullSafeField(location, sub);
}
sub.write = write;
sub.read = read;
sub.expected = expected;
sub.explicit = explicit;
sub.analyze(locals);
actual = sub.actual;
}
use of org.elasticsearch.painless.Definition.Method in project elasticsearch by elastic.
the class NodeToStringTests method testPSubShortcut.
public void testPSubShortcut() {
Location l = new Location(getTestName(), 0);
Struct s = Definition.getType(FeatureTest.class.getName()).struct;
Method getter = s.methods.get(new MethodKey("getX", 0));
Method setter = s.methods.get(new MethodKey("setX", 1));
PSubShortcut node = new PSubShortcut(l, "x", FeatureTest.class.getName(), getter, setter);
node.prefix = new EVariable(l, "a");
assertEquals("(PSubShortcut (EVariable a) x)", node.toString());
assertEquals("(PSubNullSafeCallInvoke (PSubShortcut (EVariable a) x))", new PSubNullSafeCallInvoke(l, node).toString());
}
use of org.elasticsearch.painless.Definition.Method in project elasticsearch by elastic.
the class FunctionRef method lookup.
/**
* Looks up {@code type::call} from the whitelist, and returns a matching method.
*/
private static Definition.Method lookup(Definition.Type expected, String type, String call, boolean receiverCaptured) {
// check its really a functional interface
// for e.g. Comparable
Method method = expected.struct.getFunctionalMethod();
if (method == null) {
throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " + "to [" + expected.name + "], not a functional interface");
}
// lookup requested method
Definition.Struct struct = Definition.getType(type).struct;
final Definition.Method impl;
// ctor ref
if ("new".equals(call)) {
impl = struct.constructors.get(new Definition.MethodKey("<init>", method.arguments.size()));
} else {
// look for a static impl first
Definition.Method staticImpl = struct.staticMethods.get(new Definition.MethodKey(call, method.arguments.size()));
if (staticImpl == null) {
// otherwise a virtual impl
final int arity;
if (receiverCaptured) {
// receiver captured
arity = method.arguments.size();
} else {
// receiver passed
arity = method.arguments.size() - 1;
}
impl = struct.methods.get(new Definition.MethodKey(call, arity));
} else {
impl = staticImpl;
}
}
if (impl == null) {
throw new IllegalArgumentException("Unknown reference [" + type + "::" + call + "] matching " + "[" + expected + "]");
}
return impl;
}
Aggregations