Search in sources :

Example 36 with StringAppender

use of org.mvel2.util.StringAppender in project mvel by mvel.

the class StringAppenderTest method testSubSequence.

@Test
public void testSubSequence() {
    final StringAppender stringAppender = new StringAppender("abcd");
    Assert.assertEquals("bc", stringAppender.subSequence(1, 3));
}
Also used : StringAppender(org.mvel2.util.StringAppender) Test(org.junit.Test)

Example 37 with StringAppender

use of org.mvel2.util.StringAppender in project mvel by mvel.

the class StringAppenderTest method testGetCharsSubSequence.

@Test
public void testGetCharsSubSequence() {
    final StringAppender stringAppender = new StringAppender(new StringBuffer("abcdef"));
    final char[] target = { 'g', 'h', 'i' };
    stringAppender.getChars(1, 2, target, 1);
    Assert.assertArrayEquals(new char[] { 'g', 'b', 'i' }, target);
}
Also used : StringAppender(org.mvel2.util.StringAppender) Test(org.junit.Test)

Example 38 with StringAppender

use of org.mvel2.util.StringAppender in project mvel by mvel.

the class StringAppenderTest method testAppendByteArr.

@Test
public void testAppendByteArr() {
    final StringAppender stringAppender = new StringAppender("a");
    stringAppender.append("b".getBytes());
    stringAppender.append("c".getBytes());
    Assert.assertEquals("abc", stringAppender.toString());
}
Also used : StringAppender(org.mvel2.util.StringAppender) Test(org.junit.Test)

Example 39 with StringAppender

use of org.mvel2.util.StringAppender in project mvel by mvel.

the class ASMAccessorOptimizer method _initJIT2.

private void _initJIT2() {
    if (isAdvancedDebugging()) {
        buildLog = new StringAppender();
    }
    cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    synchronized (Runtime.getRuntime()) {
        cw.visit(OPCODES_VERSION, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className = "ASMAccessorImpl_" + valueOf(cw.hashCode()).replaceAll("\\-", "_") + (System.currentTimeMillis() / 10) + ((int) (Math.random() * 100)), null, "java/lang/Object", new String[] { NAMESPACE + "compiler/Accessor" });
    }
    MethodVisitor m = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    m.visitCode();
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    m.visitInsn(RETURN);
    m.visitMaxs(1, 1);
    m.visitEnd();
    (mv = cw.visitMethod(ACC_PUBLIC, "setValue", "(Ljava/lang/Object;Ljava/lang/Object;L" + NAMESPACE + "integration/VariableResolverFactory;Ljava/lang/Object;)Ljava/lang/Object;", null, null)).visitCode();
}
Also used : StringAppender(org.mvel2.util.StringAppender) ClassWriter(org.mvel2.asm.ClassWriter) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 40 with StringAppender

use of org.mvel2.util.StringAppender in project mvel by mvel.

the class ReflectiveAccessorOptimizer method getMethod.

private Object getMethod(Object ctx, String name, Object[] args, Class[] argTypes, ExecutableStatement[] es) throws Exception {
    if (first && variableFactory != null && variableFactory.isResolveable(name)) {
        Object ptr = variableFactory.getVariableResolver(name).getValue();
        if (ptr instanceof Method) {
            ctx = ((Method) ptr).getDeclaringClass();
            name = ((Method) ptr).getName();
        } else if (ptr instanceof MethodStub) {
            ctx = ((MethodStub) ptr).getClassReference();
            name = ((MethodStub) ptr).getMethodName();
        } else if (ptr instanceof FunctionInstance) {
            FunctionInstance func = (FunctionInstance) ptr;
            if (!name.equals(func.getFunction().getName())) {
                getBeanProperty(ctx, name);
                addAccessorNode(new DynamicFunctionAccessor(es));
            } else {
                addAccessorNode(new FunctionAccessor(func, es));
            }
            return func.call(ctx, thisRef, variableFactory, args);
        } else {
            throw new OptimizationFailure("attempt to optimize a method call for a reference that does not point to a method: " + name + " (reference is type: " + (ctx != null ? ctx.getClass().getName() : null) + ")");
        }
        first = false;
    }
    if (ctx == null && currType == null) {
        throw new PropertyAccessException("null pointer or function not found: " + name, this.expr, this.start, pCtx);
    }
    boolean classTarget = false;
    Class<?> cls = currType != null ? currType : ((classTarget = ctx instanceof Class) ? (Class<?>) ctx : ctx.getClass());
    currType = null;
    Method m;
    Class[] parameterTypes = null;
    /**
     * Try to find an instance method from the class target.
     */
    if ((m = getBestCandidate(argTypes, name, cls, cls.getMethods(), false, classTarget)) != null) {
        parameterTypes = m.getParameterTypes();
    }
    if (m == null && classTarget) {
        /**
         * If we didn't find anything, maybe we're looking for the actual java.lang.Class methods.
         */
        if ((m = getBestCandidate(argTypes, name, cls, Class.class.getMethods(), false)) != null) {
            parameterTypes = m.getParameterTypes();
        }
    }
    // If we didn't find anything and the declared class is different from the actual one try also with the actual one
    if (m == null && ctx != null && cls != ctx.getClass() && !(ctx instanceof Class)) {
        cls = ctx.getClass();
        if ((m = getBestCandidate(argTypes, name, cls, cls.getMethods(), false, classTarget)) != null) {
            parameterTypes = m.getParameterTypes();
        }
    }
    if (m == null) {
        StringAppender errorBuild = new StringAppender();
        if ("size".equals(name) && args.length == 0 && cls.isArray()) {
            addAccessorNode(new ArrayLength());
            return getLength(ctx);
        }
        for (int i = 0; i < args.length; i++) {
            errorBuild.append(args[i] != null ? args[i].getClass().getName() : null);
            if (i < args.length - 1)
                errorBuild.append(", ");
        }
        throw new PropertyAccessException("unable to resolve method: " + cls.getName() + "." + name + "(" + errorBuild.toString() + ") [arglength=" + args.length + "]", this.expr, this.st, pCtx);
    }
    if (es != null) {
        ExecutableStatement cExpr;
        for (int i = 0; i < es.length; i++) {
            cExpr = es[i];
            if (cExpr.getKnownIngressType() == null) {
                cExpr.setKnownIngressType(paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
                cExpr.computeTypeConversionRule();
            }
            if (!cExpr.isConvertableIngressEgress()) {
                args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
            }
        }
    } else {
        /**
         * Coerce any types if required.
         */
        for (int i = 0; i < args.length; i++) args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
    }
    Method method = getWidenedTarget(cls, m);
    Object o = ctx != null ? method.invoke(ctx, normalizeArgsForVarArgs(parameterTypes, args, m.isVarArgs())) : null;
    if (hasNullMethodHandler()) {
        addAccessorNode(new MethodAccessorNH(method, (ExecutableStatement[]) es, getNullMethodHandler()));
        if (o == null)
            o = getNullMethodHandler().getProperty(m.getName(), ctx, variableFactory);
    } else {
        addAccessorNode(new MethodAccessor(method, (ExecutableStatement[]) es));
    }
    /**
     * return the response.
     */
    currType = toNonPrimitiveType(method.getReturnType());
    return o;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) DynamicFunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFunctionAccessor) DynamicFunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.DynamicFunctionAccessor) FunctionAccessor(org.mvel2.optimizers.impl.refl.nodes.FunctionAccessor) OptimizationFailure(org.mvel2.OptimizationFailure) PropertyAccessException(org.mvel2.PropertyAccessException) ArrayLength(org.mvel2.optimizers.impl.refl.nodes.ArrayLength) Method(java.lang.reflect.Method) MethodAccessorNH(org.mvel2.optimizers.impl.refl.nodes.MethodAccessorNH) FunctionInstance(org.mvel2.ast.FunctionInstance) MethodStub(org.mvel2.util.MethodStub) StringAppender(org.mvel2.util.StringAppender)

Aggregations

StringAppender (org.mvel2.util.StringAppender)37 Test (org.junit.Test)13 MethodVisitor (org.mvel2.asm.MethodVisitor)6 Method (java.lang.reflect.Method)5 CompileException (org.mvel2.CompileException)5 ClassWriter (org.mvel2.asm.ClassWriter)4 Function (org.mvel2.ast.Function)4 MethodStub (org.mvel2.util.MethodStub)4 HashMap (java.util.HashMap)3 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)3 IOException (java.io.IOException)2 DecimalFormat (java.text.DecimalFormat)2 LinkedList (java.util.LinkedList)2 Random (java.util.Random)2 ErrorDetail (org.mvel2.ErrorDetail)2 OptimizationFailure (org.mvel2.OptimizationFailure)2 ParserContext (org.mvel2.ParserContext)2 ScriptRuntimeException (org.mvel2.ScriptRuntimeException)2 UnresolveablePropertyException (org.mvel2.UnresolveablePropertyException)2 FunctionInstance (org.mvel2.ast.FunctionInstance)2