Search in sources :

Example 1 with FunctionNotFoundException

use of com.googlecode.aviator.exception.FunctionNotFoundException in project aviatorscript by killme2008.

the class SeqEveryFunction method call.

@Override
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 AviatorBoolean.TRUE;
    }
    for (Object obj : RuntimeUtils.seq(first, env)) {
        if (!fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
            return AviatorBoolean.FALSE;
        }
    }
    return AviatorBoolean.TRUE;
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 2 with FunctionNotFoundException

use of com.googlecode.aviator.exception.FunctionNotFoundException 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());
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Collector(com.googlecode.aviator.runtime.type.Collector) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Sequence(com.googlecode.aviator.runtime.type.Sequence)

Example 3 with FunctionNotFoundException

use of com.googlecode.aviator.exception.FunctionNotFoundException 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));
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 4 with FunctionNotFoundException

use of com.googlecode.aviator.exception.FunctionNotFoundException in project aviatorscript by killme2008.

the class SeqNotAnyFunction method call.

@Override
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 AviatorBoolean.TRUE;
    }
    for (Object obj : RuntimeUtils.seq(first, env)) {
        if (fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
            return AviatorBoolean.FALSE;
        }
    }
    return AviatorBoolean.TRUE;
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 5 with FunctionNotFoundException

use of com.googlecode.aviator.exception.FunctionNotFoundException in project aviatorscript by killme2008.

the class SeqReduceFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 2);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return arg3;
    }
    AviatorObject result = arg3;
    for (Object obj : RuntimeUtils.seq(first, env)) {
        result = fun.call(env, result, AviatorRuntimeJavaType.valueOf(obj));
    }
    return result;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)7 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)6 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)6 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2