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());
}
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]");
}
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;
}
Aggregations