Search in sources :

Example 96 with AviatorObject

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";
        }
    };
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) Map(java.util.Map)

Example 97 with AviatorObject

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);
}
Also used : Collection(java.util.Collection) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map)

Example 98 with AviatorObject

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());
}
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 99 with AviatorObject

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

Example 100 with AviatorObject

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

Aggregations

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)110 Test (org.junit.Test)51 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)25 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)22 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)16 HashMap (java.util.HashMap)12 Map (java.util.Map)11 List (java.util.List)10 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)9 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 Env (com.googlecode.aviator.utils.Env)6 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)5 LinkedList (java.util.LinkedList)5 Collection (java.util.Collection)4 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3