Search in sources :

Example 11 with Method

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;
}
Also used : Definition(org.elasticsearch.painless.Definition) Method(org.elasticsearch.painless.Definition.Method) Struct(org.elasticsearch.painless.Definition.Struct) Field(org.elasticsearch.painless.Definition.Field) Sort(org.elasticsearch.painless.Definition.Sort) List(java.util.List) Map(java.util.Map)

Example 12 with Method

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());
}
Also used : FeatureTest(org.elasticsearch.painless.FeatureTest) Method(org.elasticsearch.painless.Definition.Method) Location(org.elasticsearch.painless.Location) Struct(org.elasticsearch.painless.Definition.Struct) MethodKey(org.elasticsearch.painless.Definition.MethodKey)

Example 13 with Method

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;
}
Also used : Method(org.elasticsearch.painless.Definition.Method) Method(org.elasticsearch.painless.Definition.Method)

Aggregations

Method (org.elasticsearch.painless.Definition.Method)13 MethodKey (org.elasticsearch.painless.Definition.MethodKey)5 Struct (org.elasticsearch.painless.Definition.Struct)4 Type (org.elasticsearch.painless.Definition.Type)3 CallSite (java.lang.invoke.CallSite)2 MethodHandle (java.lang.invoke.MethodHandle)2 MethodType (java.lang.invoke.MethodType)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Field (org.elasticsearch.painless.Definition.Field)2 RuntimeClass (org.elasticsearch.painless.Definition.RuntimeClass)2 FunctionRef (org.elasticsearch.painless.FunctionRef)2 Location (org.elasticsearch.painless.Location)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Modifier (java.lang.reflect.Modifier)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1