use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqCompsitePredFunFunction method createFunction.
private static AviatorFunction createFunction(final Map<String, Object> env, final AviatorObject[] args, final LogicOp op) {
return new AbstractFunction() {
private static final long serialVersionUID = -3935448162130773442L;
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
switch(op) {
case AND:
boolean ret = true;
for (AviatorObject obj : args) {
AviatorFunction fn = FunctionUtils.getFunction(obj, env, 1);
if (fn == null)
throw new IllegalArgumentException("Expect " + obj.desc(env) + " as a function.");
ret = fn.call(env, arg1) == AviatorBoolean.TRUE;
if (!ret)
break;
}
return AviatorBoolean.valueOf(ret);
case OR:
ret = false;
for (AviatorObject obj : args) {
AviatorFunction fn = FunctionUtils.getFunction(obj, env, 1);
if (fn == null)
throw new IllegalArgumentException("Expect " + obj.desc(env) + " as a function.");
ret = fn.call(env, arg1) == AviatorBoolean.TRUE;
if (ret)
break;
}
return AviatorBoolean.valueOf(ret);
}
return AviatorBoolean.FALSE;
}
@Override
public String getName() {
return op == LogicOp.AND ? "seq.and" : "seq.or";
}
};
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqCountFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
Object value = arg1.getValue(env);
if (value == null) {
return AviatorLong.valueOf(0);
}
Class<?> clazz = value.getClass();
int size = -1;
if (Collection.class.isAssignableFrom(clazz)) {
Collection<?> col = (Collection<?>) value;
size = col.size();
} else if (Map.class.isAssignableFrom(clazz)) {
size = ((Map) value).size();
} else if (CharSequence.class.isAssignableFrom(clazz)) {
size = ((CharSequence) value).length();
} else if (clazz.isArray()) {
size = ArrayUtils.getLength(value);
} else if (Range.class.isAssignableFrom(clazz)) {
size = ((Range) value).size();
} else {
size = 0;
for (Object e : RuntimeUtils.seq(value, env)) {
size++;
}
}
return AviatorLong.valueOf(size);
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqFilterFunction method call.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object first = arg1.getValue(env);
AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
if (fun == null) {
throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
}
if (first == null) {
return AviatorNil.NIL;
}
Sequence seq = RuntimeUtils.seq(first, env);
Collector collector = seq.newCollector(0);
for (Object obj : seq) {
if (fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
collector.add(obj);
}
}
return AviatorRuntimeJavaType.valueOf(collector.getRawContainer());
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class JavaMethodReflectionFunctionMissing method onFunctionMissing.
@Override
public AviatorObject onFunctionMissing(final String name, final Map<String, Object> env, final AviatorObject... args) {
if (args == null || args.length < 1) {
throw new FunctionNotFoundException("Function not found: " + name + ", could not resolve with no arguments");
}
Object firstArg = args[0].getValue(env);
if (firstArg == null) {
throw new FunctionNotFoundException("Function not found: " + name + ", the first argument is null");
}
Class<?> clazz = firstArg.getClass();
Object[] jArgs = new Object[args.length - 1];
for (int i = 1; i < args.length; i++) {
jArgs[i - 1] = args[i].getValue(env);
}
return FunctionUtils.wrapReturn(Reflector.invokeInstanceMethod(clazz, name, firstArg, Reflector.getInstanceMethods(clazz, name), jArgs));
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class ClassMethodFunction method variadicCall.
@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
Object[] jArgs = null;
Object target = null;
if (this.isStatic || this.handle != null) {
jArgs = new Object[args.length];
for (int i = 0; i < args.length; i++) {
jArgs[i] = args[i].getValue(env);
}
} else {
if (args.length < 1) {
throw new IllegalArgumentException("Class<" + this.clazz + "> instance method " + this.methodName + " needs at least one argument as instance.");
}
jArgs = new Object[args.length - 1];
target = args[0].getValue(env);
for (int i = 1; i < args.length; i++) {
jArgs[i - 1] = args[i].getValue(env);
}
}
if (this.handle != null) {
try {
return FunctionUtils.wrapReturn(this.handle.invokeWithArguments(Reflector.boxArgs(this.pTypes, jArgs)));
} catch (Throwable t) {
throw Reflector.sneakyThrow(t);
}
} else {
return FunctionUtils.wrapReturn(this.isStatic ? Reflector.invokeStaticMethod(this.clazz, this.methodName, this.methods, jArgs) : Reflector.invokeInstanceMethod(this.clazz, this.methodName, target, this.methods, jArgs));
}
}
Aggregations