Search in sources :

Example 1 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project nexus-public by sonatype.

the class JexlEngine method expandExceptionDetail.

/**
 * Returns detail about the given JEXL exception, expanded to make it more readable.
 */
public static String expandExceptionDetail(final JexlException e) {
    StringBuilder detailBuilder = new StringBuilder(e.getMessage());
    JexlInfo info = e.getInfo();
    if (info != null) {
        // remove condensed header, replaced below with something more readable
        Matcher matcher = JEXL_CONDENSED_INFO_HEADER.matcher(detailBuilder);
        if (matcher.find()) {
            detailBuilder.delete(matcher.start(), matcher.end());
        }
        // add more detail if we have it and it's not already part of the message
        Optional<String> detail = ofNullable(info.getDetail()).map(Object::toString);
        if (detail.isPresent() && detailBuilder.indexOf(detail.get()) < 0) {
            addContext(detailBuilder, format("in '%s'", detail.get()));
        }
        // finally add the location in a more readable form
        addContext(detailBuilder, format("at line %d column %d", info.getLine(), info.getColumn()));
    }
    return detailBuilder.toString();
}
Also used : Matcher(java.util.regex.Matcher) JexlInfo(org.apache.commons.jexl3.JexlInfo)

Example 2 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project nexus-public by sonatype.

the class JexlSelectorTest method testPrettyExceptionMsgNoDetail.

@Test
public void testPrettyExceptionMsgNoDetail() {
    // Setup
    String expected = "at line 2 column 4";
    JexlInfo info = new JexlInfo("", 2, 4);
    // Mocked because JexlException modifies msg internally after construction
    JexlException ex = mock(JexlException.class);
    doReturn(info).when(ex).getInfo();
    doReturn("").when(ex).getMessage();
    // Execute
    String returned = JexlEngine.expandExceptionDetail(ex);
    // Verify
    assertNotNull("Returned string was not set.", returned);
    assertEquals(expected, returned);
}
Also used : JexlException(org.apache.commons.jexl3.JexlException) JexlInfo(org.apache.commons.jexl3.JexlInfo) Test(org.junit.Test)

Example 3 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commons-jexl by apache.

the class Interpreter method visit.

@Override
protected Object visit(final ASTJxltLiteral node, final Object data) {
    TemplateEngine.TemplateExpression tp = (TemplateEngine.TemplateExpression) node.jjtGetValue();
    if (tp == null) {
        final TemplateEngine jxlt = jexl.jxlt();
        JexlInfo info = node.jexlInfo();
        if (this.block != null) {
            info = new JexlNode.Info(node, info);
        }
        tp = jxlt.parseExpression(info, node.getLiteral(), frame != null ? frame.getScope() : null);
        node.jjtSetValue(tp);
    }
    if (tp != null) {
        return tp.evaluate(frame, context);
    }
    return null;
}
Also used : JexlNode(org.apache.commons.jexl3.parser.JexlNode) JexlInfo(org.apache.commons.jexl3.JexlInfo)

Example 4 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commons-jexl by apache.

the class Engine method doCreateInstance.

/**
 * Creates a new instance of an object using the most appropriate constructor
 * based on the arguments.
 * @param clazz the class to instantiate
 * @param args  the constructor arguments
 * @return the created object instance or null on failure when silent
 */
protected Object doCreateInstance(final Object clazz, final Object... args) {
    JexlException xjexl = null;
    Object result = null;
    final JexlInfo info = debug ? createInfo() : null;
    try {
        JexlMethod ctor = uberspect.getConstructor(clazz, args);
        if (ctor == null && arithmetic.narrowArguments(args)) {
            ctor = uberspect.getConstructor(clazz, args);
        }
        if (ctor != null) {
            result = ctor.invoke(clazz, args);
        } else {
            xjexl = new JexlException.Method(info, clazz.toString(), args);
        }
    } catch (final JexlException xany) {
        xjexl = xany;
    } catch (final Exception xany) {
        xjexl = new JexlException.Method(info, clazz.toString(), args, xany);
    }
    if (xjexl != null) {
        if (silent) {
            if (logger.isWarnEnabled()) {
                logger.warn(xjexl.getMessage(), xjexl.getCause());
            }
            return null;
        }
        throw xjexl.clean();
    }
    return result;
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlException(org.apache.commons.jexl3.JexlException) JexlInfo(org.apache.commons.jexl3.JexlInfo) JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlException(org.apache.commons.jexl3.JexlException)

Example 5 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commons-jexl by apache.

the class Engine method invokeMethod.

@Override
public Object invokeMethod(final Object obj, final String meth, final Object... args) {
    JexlException xjexl = null;
    Object result = null;
    final JexlInfo info = debug ? createInfo() : null;
    try {
        JexlMethod method = uberspect.getMethod(obj, meth, args);
        if (method == null && arithmetic.narrowArguments(args)) {
            method = uberspect.getMethod(obj, meth, args);
        }
        if (method != null) {
            result = method.invoke(obj, args);
        } else {
            xjexl = new JexlException.Method(info, meth, args);
        }
    } catch (final JexlException xany) {
        xjexl = xany;
    } catch (final Exception xany) {
        xjexl = new JexlException.Method(info, meth, args, xany);
    }
    if (xjexl != null) {
        if (!silent) {
            throw xjexl.clean();
        }
        if (logger.isWarnEnabled()) {
            logger.warn(xjexl.getMessage(), xjexl.getCause());
        }
    }
    return result;
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlException(org.apache.commons.jexl3.JexlException) JexlInfo(org.apache.commons.jexl3.JexlInfo) JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlException(org.apache.commons.jexl3.JexlException)

Aggregations

JexlInfo (org.apache.commons.jexl3.JexlInfo)13 JexlException (org.apache.commons.jexl3.JexlException)7 IOException (java.io.IOException)2 JexlFeatures (org.apache.commons.jexl3.JexlFeatures)2 JexlScript (org.apache.commons.jexl3.JexlScript)2 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)2 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)2 Test (org.junit.Test)2 Matcher (java.util.regex.Matcher)1 JexlEngine (org.apache.commons.jexl2.JexlEngine)1 JexlInfo (org.apache.commons.jexl2.JexlInfo)1 Uberspect (org.apache.commons.jexl2.introspection.Uberspect)1 UberspectImpl (org.apache.commons.jexl2.introspection.UberspectImpl)1 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)1 JexlEngine (org.apache.commons.jexl3.JexlEngine)1 LexicalScope (org.apache.commons.jexl3.internal.LexicalScope)1 Scope (org.apache.commons.jexl3.internal.Scope)1 JexlUberspect (org.apache.commons.jexl3.introspection.JexlUberspect)1 JexlNode (org.apache.commons.jexl3.parser.JexlNode)1 Parser (org.apache.commons.jexl3.parser.Parser)1