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();
}
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);
}
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;
}
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;
}
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;
}
Aggregations