Skip to content
Permalink
Browse files

Improved: Use method references instead of lambda inside variables

(OFBIZ-10817)

Lambda expressions are not meant to be stored inside variables.  In
most cases it is better to define a static method and use a method
reference instead.  Static methods have the benefits of having a more
explicit signature and some javadoc attached to them.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1864953 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Aug 12, 2019
1 parent 7c8f7a5 commit 3fb1d50625d41c8867606f0c23374361a9f7ddb8
@@ -112,8 +112,8 @@ public static Configuration makeConfiguration(BeansWrapper wrapper) {
boolean verboseTemplate = ModelWidget.widgetBoundaryCommentsEnabled(null)
|| UtilProperties.getPropertyAsBoolean("widget", "widget.freemarker.template.verbose", false);
newConfig.setTemplateExceptionHandler(verboseTemplate
? OFBizTemplateExceptionHandler.OFBIZ_DEBUG_HANDLER
: OFBizTemplateExceptionHandler.OFBIZ_DEFAULT_HANDLER);
? FreeMarkerWorker::handleTemplateExceptionVerbosily
: FreeMarkerWorker::handleTemplateException);
try {
newConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
newConfig.setSetting("number_format", "0.##########");
@@ -498,34 +498,45 @@ protected URL getURL(String name) {
}

/**
* OFBiz specific {@link TemplateExceptionHandler} interface.
* Handles template exceptions quietly.
* <p>
* This is done by suppressing the exception and replacing it by a generic char for quiet alert.
* Note that exception is still logged.
* <p>
* This implements the {@link TemplateExceptionHandler} functional interface.
*
* @param te the exception that occurred
* @param env the runtime environment of the template
* @param out this is where the output of the template is written
*/
interface OFBizTemplateExceptionHandler {

/**
* {@link TemplateExceptionHandler} that suppresses the exception and keep the rendering going on.
* It sanitizes any messages present in the stack trace prior to printing to the output writer.
*/
TemplateExceptionHandler OFBIZ_DEBUG_HANDLER = (te, env, out) -> {
try {
out.write(te.getMessage());
Debug.logError(te, module);
} catch (IOException e) {
Debug.logError(e, module);
}
};
private static void handleTemplateException(TemplateException te, Environment env, Writer out) {
try {
out.write(UtilProperties.getPropertyValue("widget", "widget.freemarker.template.exception.message", ""));
Debug.logError(te, module);
} catch (IOException e) {
Debug.logError(e, module);
}
}

/**
* {@link TemplateExceptionHandler} that suppresses the exception and replace by a generic char for quiet alert.
* As mentioned in the doc, the stack trace is still logged {@link TemplateExceptionHandler#IGNORE_HANDLER}
*/
TemplateExceptionHandler OFBIZ_DEFAULT_HANDLER = (te, env, out) -> {
try {
out.write(UtilProperties.getPropertyValue("widget", "widget.freemarker.template.exception.message",""));
Debug.logError(te, module);
} catch (IOException e) {
Debug.logError(e, module);
}
};
/**
* Handles template exceptions verbosely.
* <p>
* This is done by suppressing the exception and keeping the rendering going on. Messages
* present in the stack trace are sanitized before printing them to the output writer.
* Note that exception is still logged.
* <p>
* This implements the {@link TemplateExceptionHandler} functional interface.
*
* @param te the exception that occurred
* @param env the runtime environment of the template
* @param out this is where the output of the template is written
*/
private static void handleTemplateExceptionVerbosily(TemplateException te, Environment env, Writer out) {
try {
out.write(te.getMessage());
Debug.logError(te, module);
} catch (IOException e) {
Debug.logError(e, module);
}
}
}

0 comments on commit 3fb1d50

Please sign in to comment.
You can’t perform that action at this time.