Search in sources :

Example 1 with Field

use of org.elasticsearch.painless.Definition.Field 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 2 with Field

use of org.elasticsearch.painless.Definition.Field 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 3 with Field

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

Field (org.elasticsearch.painless.Definition.Field)3 Struct (org.elasticsearch.painless.Definition.Struct)3 List (java.util.List)2 Map (java.util.Map)2 Method (org.elasticsearch.painless.Definition.Method)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 Comparator (java.util.Comparator)1 Comparator.comparing (java.util.Comparator.comparing)1 TreeMap (java.util.TreeMap)1 Consumer (java.util.function.Consumer)1 Collectors.toList (java.util.stream.Collectors.toList)1 Logger (org.apache.logging.log4j.Logger)1 IOUtils (org.apache.lucene.util.IOUtils)1 PathUtils (org.elasticsearch.common.io.PathUtils)1