Search in sources :

Example 31 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

the class AbstractOptimizer method tryStaticAccess.

/**
 * Try static access of the property, and return an instance of the Field, Method of Class if successful.
 *
 * @return - Field, Method or Class instance.
 */
protected Object tryStaticAccess() {
    int begin = cursor;
    try {
        /**
         * Try to resolve this *smartly* as a static class reference.
         *
         * This starts at the end of the token and starts to step backwards to figure out whether
         * or not this may be a static class reference.  We search for method calls simply by
         * inspecting for ()'s.  The first union area we come to where no brackets are present is our
         * test-point for a class reference.  If we find a class, we pass the reference to the
         * property accessor along  with trailing methods (if any).
         */
        boolean meth = false;
        // int end = start + length;
        int last = end;
        for (int i = end - 1; i > start; i--) {
            switch(expr[i]) {
                case '.':
                    if (!meth) {
                        try {
                            String test = new String(expr, start, (cursor = last) - start);
                            return Class.forName(test, true, pCtx != null ? pCtx.getParserConfiguration().getClassLoader() : currentThread().getContextClassLoader());
                        } catch (ClassNotFoundException e) {
                            Class cls = Class.forName(new String(expr, start, i - start), true, pCtx != null ? pCtx.getParserConfiguration().getClassLoader() : currentThread().getContextClassLoader());
                            String name = new String(expr, i + 1, end - i - 1);
                            try {
                                return cls.getField(name);
                            } catch (NoSuchFieldException nfe) {
                                for (Method m : cls.getMethods()) {
                                    if (name.equals(m.getName()))
                                        return m;
                                }
                                return null;
                            }
                        }
                    }
                    meth = false;
                    last = i;
                    break;
                case '}':
                    i--;
                    for (int d = 1; i > start && d != 0; i--) {
                        switch(expr[i]) {
                            case '}':
                                d++;
                                break;
                            case '{':
                                d--;
                                break;
                            case '"':
                            case '\'':
                                char s = expr[i];
                                while (i > start && (expr[i] != s && expr[i - 1] != '\\')) i--;
                        }
                    }
                    break;
                case ')':
                    i--;
                    for (int d = 1; i > start && d != 0; i--) {
                        switch(expr[i]) {
                            case ')':
                                d++;
                                break;
                            case '(':
                                d--;
                                break;
                            case '"':
                            case '\'':
                                char s = expr[i];
                                while (i > start && (expr[i] != s && expr[i - 1] != '\\')) i--;
                        }
                    }
                    meth = true;
                    last = i++;
                    break;
                case '\'':
                    while (--i > start) {
                        if (expr[i] == '\'' && expr[i - 1] != '\\') {
                            break;
                        }
                    }
                    break;
                case '"':
                    while (--i > start) {
                        if (expr[i] == '"' && expr[i - 1] != '\\') {
                            break;
                        }
                    }
                    break;
            }
        }
    } catch (Exception cnfe) {
        cursor = begin;
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) CompileException(org.mvel2.CompileException)

Example 32 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

the class OptimizerFactory method setDefaultOptimizer.

public static void setDefaultOptimizer(String name) {
    try {
        // noinspection unchecked
        AccessorOptimizer ao = accessorCompilers.get(defaultOptimizer = name);
        ao.init();
        setThreadAccessorOptimizer(ao.getClass());
    } catch (Exception e) {
        throw new RuntimeException("unable to instantiate accessor compiler", e);
    }
}
Also used : ASMAccessorOptimizer(org.mvel2.optimizers.impl.asm.ASMAccessorOptimizer) ReflectiveAccessorOptimizer(org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer)

Example 33 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

the class InlineCollectionNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if (accessor != null) {
        return accessor.getValue(ctx, thisValue, factory);
    } else {
        try {
            AccessorOptimizer ao = OptimizerFactory.getThreadAccessorOptimizer();
            if (collectionGraph == null)
                parseGraph(true, null, null);
            accessor = ao.optimizeCollection(AbstractParser.getCurrentThreadParserContext(), collectionGraph, egressType, expr, trailingStart, trailingOffset, ctx, thisValue, factory);
            egressType = ao.getEgressType();
            return accessor.getValue(ctx, thisValue, factory);
        } finally {
            OptimizerFactory.clearThreadAccessorOptimizer();
        }
    }
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer)

Example 34 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mikebrock.

the class ASTNode method optimize.

private Object optimize(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if ((fields & DEOP) != 0) {
        fields ^= DEOP;
    }
    AccessorOptimizer optimizer;
    Object retVal = null;
    if ((fields & NOJIT) != 0 || factory != null && factory.isResolveable(nameCache)) {
        optimizer = getAccessorCompiler(SAFE_REFLECTIVE);
    } else {
        optimizer = getDefaultAccessorCompiler();
    }
    ParserContext pCtx;
    if ((fields & PCTX_STORED) != 0) {
        pCtx = (ParserContext) literal;
    } else {
        pCtx = new ParserContext(new ParserConfiguration(getInjectedImports(factory), null));
    }
    try {
        pCtx.optimizationNotify();
        setAccessor(optimizer.optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, egressType));
    } catch (OptimizationNotSupported ne) {
        setAccessor((optimizer = getAccessorCompiler(SAFE_REFLECTIVE)).optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, null));
    }
    if (accessor == null) {
        return get(expr, start, offset, ctx, factory, thisValue);
    }
    if (retVal == null) {
        retVal = optimizer.getResultOptPass();
    }
    if (egressType == null) {
        egressType = optimizer.getEgressType();
    }
    return retVal;
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer) ParserContext(org.mvel2.ParserContext) OptimizationNotSupported(org.mvel2.optimizers.OptimizationNotSupported) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 35 with Accessor

use of org.mvel2.compiler.Accessor in project mvel by mvel.

the class ASTNode method optimize.

private Object optimize(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if ((fields & DEOP) != 0) {
        fields ^= DEOP;
    }
    AccessorOptimizer optimizer;
    Object retVal = null;
    if ((fields & NOJIT) != 0 || factory != null && factory.isResolveable(getName())) {
        optimizer = getAccessorCompiler(SAFE_REFLECTIVE);
    } else {
        optimizer = getDefaultAccessorCompiler();
    }
    ParserContext pCtx;
    if ((fields & PCTX_STORED) != 0) {
        pCtx = (ParserContext) literal;
    } else {
        pCtx = new ParserContext(new ParserConfiguration(getInjectedImports(factory), null));
    }
    try {
        pCtx.optimizationNotify();
        setAccessor(optimizer.optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, egressType));
    } catch (OptimizationNotSupported ne) {
        setAccessor((optimizer = getAccessorCompiler(SAFE_REFLECTIVE)).optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, null));
    }
    if (accessor == null) {
        return get(expr, start, offset, ctx, factory, thisValue, pCtx);
    }
    if (retVal == null) {
        retVal = optimizer.getResultOptPass();
    }
    if (egressType == null) {
        egressType = optimizer.getEgressType();
    }
    return retVal;
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer) ParserContext(org.mvel2.ParserContext) OptimizationNotSupported(org.mvel2.optimizers.OptimizationNotSupported) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

Accessor (org.mvel2.compiler.Accessor)16 CompileException (org.mvel2.CompileException)11 PropertyTools.getFieldOrAccessor (org.mvel2.util.PropertyTools.getFieldOrAccessor)11 PropertyTools.getFieldOrWriteAccessor (org.mvel2.util.PropertyTools.getFieldOrWriteAccessor)11 IOException (java.io.IOException)10 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)10 Map (java.util.Map)9 PropertyAccessException (org.mvel2.PropertyAccessException)9 AccessorNode (org.mvel2.compiler.AccessorNode)9 ExprValueAccessor (org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor)9 IndexedVariableAccessor (org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor)9 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)9 StaticReferenceAccessor (org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)9 VariableAccessor (org.mvel2.optimizers.impl.refl.nodes.VariableAccessor)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)8 AccessorOptimizer (org.mvel2.optimizers.AccessorOptimizer)8 FieldAccessor (org.mvel2.optimizers.impl.refl.nodes.FieldAccessor)8 MapAccessor (org.mvel2.optimizers.impl.refl.nodes.MapAccessor)8 List (java.util.List)7