Search in sources :

Example 1 with Struct

use of org.elasticsearch.painless.Definition.Struct in project elasticsearch by elastic.

the class ENewObj method analyze.

@Override
void analyze(Locals locals) {
    final Type type;
    try {
        type = Definition.getType(this.type);
    } catch (IllegalArgumentException exception) {
        throw createError(new IllegalArgumentException("Not a type [" + this.type + "]."));
    }
    Struct struct = type.struct;
    constructor = struct.constructors.get(new Definition.MethodKey("<init>", arguments.size()));
    if (constructor != null) {
        Type[] types = new Type[constructor.arguments.size()];
        constructor.arguments.toArray(types);
        if (constructor.arguments.size() != arguments.size()) {
            throw createError(new IllegalArgumentException("When calling constructor on type [" + struct.name + "]" + " expected [" + constructor.arguments.size() + "] arguments, but found [" + arguments.size() + "]."));
        }
        for (int argument = 0; argument < arguments.size(); ++argument) {
            AExpression expression = arguments.get(argument);
            expression.expected = types[argument];
            expression.internal = true;
            expression.analyze(locals);
            arguments.set(argument, expression.cast(locals));
        }
        statement = true;
        actual = type;
    } else {
        throw createError(new IllegalArgumentException("Unknown new call on type [" + struct.name + "]."));
    }
}
Also used : Type(org.elasticsearch.painless.Definition.Type) Struct(org.elasticsearch.painless.Definition.Struct)

Example 2 with Struct

use of org.elasticsearch.painless.Definition.Struct in project elasticsearch by elastic.

the class NodeToStringTests method testPSubField.

public void testPSubField() {
    Location l = new Location(getTestName(), 0);
    Struct s = Definition.getType(Boolean.class.getSimpleName()).struct;
    Field f = s.staticMembers.get("TRUE");
    PSubField node = new PSubField(l, f);
    node.prefix = new EStatic(l, "Boolean");
    assertEquals("(PSubField (EStatic Boolean) TRUE)", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubField (EStatic Boolean) TRUE))", new PSubNullSafeCallInvoke(l, node).toString());
}
Also used : Field(org.elasticsearch.painless.Definition.Field) Location(org.elasticsearch.painless.Location) Struct(org.elasticsearch.painless.Definition.Struct)

Example 3 with Struct

use of org.elasticsearch.painless.Definition.Struct in project elasticsearch by elastic.

the class PainlessDocGenerator method main.

public static void main(String[] args) throws IOException {
    Path apiRootPath = PathUtils.get(args[0]);
    // Blow away the last execution and recreate it from scratch
    IOUtils.rm(apiRootPath);
    Files.createDirectories(apiRootPath);
    Path indexPath = apiRootPath.resolve("index.asciidoc");
    logger.info("Starting to write [index.asciidoc]");
    try (PrintStream indexStream = new PrintStream(Files.newOutputStream(indexPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), false, StandardCharsets.UTF_8.name())) {
        emitGeneratedWarning(indexStream);
        List<Type> types = Definition.allSimpleTypes().stream().sorted(comparing(t -> t.name)).collect(toList());
        for (Type type : types) {
            if (type.sort.primitive) {
                // Primitives don't have methods to reference
                continue;
            }
            if ("def".equals(type.name)) {
                // def is special but doesn't have any methods all of its own.
                continue;
            }
            indexStream.print("include::");
            indexStream.print(type.struct.name);
            indexStream.println(".asciidoc[]");
            Path typePath = apiRootPath.resolve(type.struct.name + ".asciidoc");
            logger.info("Writing [{}.asciidoc]", type.name);
            try (PrintStream typeStream = new PrintStream(Files.newOutputStream(typePath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), false, StandardCharsets.UTF_8.name())) {
                emitGeneratedWarning(typeStream);
                typeStream.print("[[");
                emitAnchor(typeStream, type.struct);
                typeStream.print("]]++");
                typeStream.print(type.name);
                typeStream.println("++::");
                Consumer<Field> documentField = field -> PainlessDocGenerator.documentField(typeStream, field);
                Consumer<Method> documentMethod = method -> PainlessDocGenerator.documentMethod(typeStream, method);
                type.struct.staticMembers.values().stream().sorted(FIELD_NAME).forEach(documentField);
                type.struct.members.values().stream().sorted(FIELD_NAME).forEach(documentField);
                type.struct.staticMethods.values().stream().sorted(METHOD_NAME.thenComparing(NUMBER_OF_ARGS)).forEach(documentMethod);
                type.struct.constructors.values().stream().sorted(NUMBER_OF_ARGS).forEach(documentMethod);
                Map<String, Struct> inherited = new TreeMap<>();
                type.struct.methods.values().stream().sorted(METHOD_NAME.thenComparing(NUMBER_OF_ARGS)).forEach(method -> {
                    if (method.owner == type.struct) {
                        documentMethod(typeStream, method);
                    } else {
                        inherited.put(method.owner.name, method.owner);
                    }
                });
                if (false == inherited.isEmpty()) {
                    typeStream.print("* Inherits methods from ");
                    boolean first = true;
                    for (Struct inheritsFrom : inherited.values()) {
                        if (first) {
                            first = false;
                        } else {
                            typeStream.print(", ");
                        }
                        typeStream.print("++");
                        emitStruct(typeStream, inheritsFrom);
                        typeStream.print("++");
                    }
                    typeStream.println();
                }
            }
        }
    }
    logger.info("Done writing [index.asciidoc]");
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) Field(org.elasticsearch.painless.Definition.Field) Augmentation(org.elasticsearch.painless.api.Augmentation) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) Type(org.elasticsearch.painless.Definition.Type) StandardCharsets(java.nio.charset.StandardCharsets) Struct(org.elasticsearch.painless.Definition.Struct) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) Method(org.elasticsearch.painless.Definition.Method) TreeMap(java.util.TreeMap) ESLoggerFactory(org.elasticsearch.common.logging.ESLoggerFactory) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) Comparator.comparing(java.util.Comparator.comparing) Comparator(java.util.Comparator) Path(java.nio.file.Path) PathUtils(org.elasticsearch.common.io.PathUtils) PrintStream(java.io.PrintStream) Method(org.elasticsearch.painless.Definition.Method) TreeMap(java.util.TreeMap) Struct(org.elasticsearch.painless.Definition.Struct) Field(org.elasticsearch.painless.Definition.Field) Type(org.elasticsearch.painless.Definition.Type)

Example 4 with Struct

use of org.elasticsearch.painless.Definition.Struct in project elasticsearch by elastic.

the class PCallInvoke method analyze.

@Override
void analyze(Locals locals) {
    prefix.analyze(locals);
    prefix.expected = prefix.actual;
    prefix = prefix.cast(locals);
    if (prefix.actual.sort == Sort.ARRAY) {
        throw createError(new IllegalArgumentException("Illegal call [" + name + "] on array type."));
    }
    Struct struct = prefix.actual.struct;
    if (prefix.actual.sort.primitive) {
        struct = Definition.getType(prefix.actual.sort.boxed.getSimpleName()).struct;
    }
    MethodKey methodKey = new MethodKey(name, arguments.size());
    Method method = prefix instanceof EStatic ? struct.staticMethods.get(methodKey) : struct.methods.get(methodKey);
    if (method != null) {
        sub = new PSubCallInvoke(location, method, prefix.actual, arguments);
    } else if (prefix.actual.sort == Sort.DEF) {
        sub = new PSubDefCall(location, name, arguments);
    } else {
        throw createError(new IllegalArgumentException("Unknown call [" + name + "] with [" + arguments.size() + "] arguments on type [" + struct.name + "]."));
    }
    if (nullSafe) {
        sub = new PSubNullSafeCallInvoke(location, sub);
    }
    sub.expected = expected;
    sub.explicit = explicit;
    sub.analyze(locals);
    actual = sub.actual;
    statement = true;
}
Also used : Method(org.elasticsearch.painless.Definition.Method) Struct(org.elasticsearch.painless.Definition.Struct) MethodKey(org.elasticsearch.painless.Definition.MethodKey)

Example 5 with Struct

use of org.elasticsearch.painless.Definition.Struct 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)

Aggregations

Struct (org.elasticsearch.painless.Definition.Struct)8 Method (org.elasticsearch.painless.Definition.Method)4 Location (org.elasticsearch.painless.Location)4 List (java.util.List)3 Map (java.util.Map)3 Field (org.elasticsearch.painless.Definition.Field)3 MethodKey (org.elasticsearch.painless.Definition.MethodKey)2 Type (org.elasticsearch.painless.Definition.Type)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 StandardOpenOption (java.nio.file.StandardOpenOption)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 Comparator (java.util.Comparator)1 Comparator.comparing (java.util.Comparator.comparing)1 TreeMap (java.util.TreeMap)1