Search in sources :

Example 1 with NoSuchPropertyException

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

the class Reflector method setProperty.

public static void setProperty(final Map<String, Object> env, String name, final Object val) {
    final String[] names = Constants.SPLIT_PAT.split(name);
    Object target = fastGetProperty(name, names, null, Target.withEnv(env), false, 0, names.length - 1);
    if (target == null) {
        // FIXME error msg
        throw new NoSuchPropertyException("Property `" + name + "` not found in java bean:" + env);
    }
    name = names[names.length - 1];
    int arrayIndex = -1;
    String keyIndex = null;
    switch(name.charAt(name.length() - 1)) {
        case ']':
            int idx = name.indexOf("[");
            if (idx < 0) {
                throw new IllegalArgumentException("Should not happen, doesn't contains '['");
            }
            String rawName = name;
            name = name.substring(0, idx);
            arrayIndex = Integer.valueOf(rawName.substring(idx + 1, rawName.length() - 1));
            break;
        case ')':
            idx = name.indexOf("(");
            if (idx < 0) {
                throw new IllegalArgumentException("Should not happen, doesn't contains '('");
            }
            rawName = name;
            name = name.substring(0, idx);
            keyIndex = rawName.substring(idx + 1, rawName.length() - 1);
            break;
    }
    if (arrayIndex >= 0 || keyIndex != null) {
        if (!name.isEmpty()) {
            target = fastGetProperty(target, name, PropertyType.Getter);
        }
        if (arrayIndex >= 0) {
            ArrayUtils.set(target, arrayIndex, boxArg(target.getClass().getComponentType(), val));
        } else {
            ((Map) target).put(keyIndex, val);
        }
    } else if (target instanceof Map) {
        ((Map) target).put(name, val);
    } else {
        final Class<?> clazz = target.getClass();
        Map<String, PropertyFoundResult> results = getClassPropertyResults(cachedSettters, cachedSetterRq, clazz);
        try {
            PropertyFoundResult result = results.get(name);
            if (result == null) {
                result = retrieveSetterHandle(results, clazz, name);
            }
            if (result.handle != null) {
                result.handle.invoke(target, boxArg(result.handle.type().parameterType(1), val));
            } else {
                throw new NoSuchPropertyException("Setter not found for property `" + name + "` in class: " + clazz);
            }
        } catch (Throwable t) {
            if (!results.containsKey(name)) {
                putDummyHandle(name, results);
            }
            throw sneakyThrow(t);
        }
    }
}
Also used : NoSuchPropertyException(com.googlecode.aviator.exception.NoSuchPropertyException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 2 with NoSuchPropertyException

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

the class SeqPredicateFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, AviatorObject arg1) {
    if (this.propertyName != null) {
        String propertyNameStr = this.propertyName.stringValue(env);
        Object target = arg1.getValue(env);
        try {
            Object property = Reflector.getProperty(target, propertyNameStr);
            arg1 = AviatorRuntimeJavaType.valueOf(property);
        } catch (NoSuchPropertyException e) {
            throw new IllegalArgumentException("Fail to get property <" + propertyNameStr + "> from <" + arg1.desc(env) + ">", e);
        }
    }
    switch(this.opType) {
        case EQ:
            return arg1.compare(this.value, env) == 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case NEQ:
            return arg1.compare(this.value, env) != 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case LT:
            return arg1.compare(this.value, env) < 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case LE:
            return arg1.compare(this.value, env) <= 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case GE:
            return arg1.compare(this.value, env) >= 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        case GT:
            return arg1.compare(this.value, env) > 0 ? AviatorBoolean.TRUE : AviatorBoolean.FALSE;
        default:
            throw new ExpressionRuntimeException(getName() + " is not a relation operator");
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) NoSuchPropertyException(com.googlecode.aviator.exception.NoSuchPropertyException) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

NoSuchPropertyException (com.googlecode.aviator.exception.NoSuchPropertyException)2 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1