use of org.elasticsearch.painless.Definition.Type in project elasticsearch by elastic.
the class SFunction method generateSignature.
void generateSignature() {
try {
rtnType = Definition.getType(rtnTypeStr);
} catch (IllegalArgumentException exception) {
throw createError(new IllegalArgumentException("Illegal return type [" + rtnTypeStr + "] for function [" + name + "]."));
}
if (paramTypeStrs.size() != paramNameStrs.size()) {
throw createError(new IllegalStateException("Illegal tree structure."));
}
Class<?>[] paramClasses = new Class<?>[this.paramTypeStrs.size()];
List<Type> paramTypes = new ArrayList<>();
for (int param = 0; param < this.paramTypeStrs.size(); ++param) {
try {
Type paramType = Definition.getType(this.paramTypeStrs.get(param));
paramClasses[param] = paramType.clazz;
paramTypes.add(paramType);
parameters.add(new Parameter(location, paramNameStrs.get(param), paramType));
} catch (IllegalArgumentException exception) {
throw createError(new IllegalArgumentException("Illegal parameter type [" + this.paramTypeStrs.get(param) + "] for function [" + name + "]."));
}
}
org.objectweb.asm.commons.Method method = new org.objectweb.asm.commons.Method(name, MethodType.methodType(rtnType.clazz, paramClasses).toMethodDescriptorString());
this.method = new Method(name, null, false, rtnType, paramTypes, method, Modifier.STATIC | Modifier.PRIVATE, null);
}
use of org.elasticsearch.painless.Definition.Type in project elasticsearch by elastic.
the class EElvis method analyze.
@Override
void analyze(Locals locals) {
if (expected != null && expected.sort.primitive) {
throw createError(new IllegalArgumentException("Evlis operator cannot return primitives"));
}
lhs.expected = expected;
lhs.explicit = explicit;
lhs.internal = internal;
rhs.expected = expected;
rhs.explicit = explicit;
rhs.internal = internal;
actual = expected;
lhs.analyze(locals);
rhs.analyze(locals);
if (lhs.isNull) {
throw createError(new IllegalArgumentException("Extraneous elvis operator. LHS is null."));
}
if (lhs.constant != null) {
throw createError(new IllegalArgumentException("Extraneous elvis operator. LHS is a constant."));
}
if (lhs.actual.sort.primitive) {
throw createError(new IllegalArgumentException("Extraneous elvis operator. LHS is a primitive."));
}
if (rhs.isNull) {
throw createError(new IllegalArgumentException("Extraneous elvis operator. RHS is null."));
}
if (expected == null) {
final Type promote = AnalyzerCaster.promoteConditional(lhs.actual, rhs.actual, lhs.constant, rhs.constant);
lhs.expected = promote;
rhs.expected = promote;
actual = promote;
}
lhs = lhs.cast(locals);
rhs = rhs.cast(locals);
}
use of org.elasticsearch.painless.Definition.Type in project elasticsearch by elastic.
the class EInstanceof method analyze.
@Override
void analyze(Locals locals) {
final Type type;
// ensure the specified type is part of the definition
try {
type = Definition.getType(this.type);
} catch (IllegalArgumentException exception) {
throw createError(new IllegalArgumentException("Not a type [" + this.type + "]."));
}
// map to wrapped type for primitive types
resolvedType = type.sort.primitive ? type.sort.boxed : type.clazz;
// analyze and cast the expression
expression.analyze(locals);
expression.expected = expression.actual;
expression = expression.cast(locals);
// record if the expression returns a primitive
primitiveExpression = expression.actual.sort.primitive;
// map to wrapped type for primitive types
expressionType = expression.actual.sort.primitive ? expression.actual.sort.boxed : type.clazz;
actual = Definition.BOOLEAN_TYPE;
}
use of org.elasticsearch.painless.Definition.Type 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.Type in project elasticsearch by elastic.
the class PainlessDocGenerator method documentMethod.
/**
* Document a method.
*/
private static void documentMethod(PrintStream stream, Method method) {
stream.print("* ++[[");
emitAnchor(stream, method);
stream.print("]]");
if (false == method.augmentation && Modifier.isStatic(method.modifiers)) {
stream.print("static ");
}
if (false == method.name.equals("<init>")) {
emitType(stream, method.rtn);
stream.print(' ');
}
String javadocRoot = javadocRoot(method);
emitJavadocLink(stream, javadocRoot, method);
stream.print('[');
stream.print(methodName(method));
stream.print("](");
boolean first = true;
for (Type arg : method.arguments) {
if (first) {
first = false;
} else {
stream.print(", ");
}
emitType(stream, arg);
}
stream.print(")++");
if (javadocRoot.equals("java8")) {
stream.print(" (");
emitJavadocLink(stream, "java9", method);
stream.print("[java 9])");
}
stream.println();
}
Aggregations