Skip to content

Commit af038f3

Browse files
author
jefffischer
committed
BroadleafCommerce/QA#1662 - Patch the method with the bug in EvaluationUtil with the fixed version using bytecode manipulation at class load time.
1 parent 1c8f486 commit af038f3

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Common Libraries
4+
* %%
5+
* Copyright (C) 2009 - 2016 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.common.web.patch;
21+
22+
import java.math.BigDecimal;
23+
import java.math.BigInteger;
24+
25+
/**
26+
* Source for this patch is taken from https://github.com/thymeleaf/thymeleaf/commit/fc360d0d50df5090f93f6922012463055f28896d.
27+
* This is part of https://github.com/thymeleaf/thymeleaf/issues/449, which will be resolved in 2.1.5-RELEASE (not released yet).
28+
* This fix benefits the performance of many Broadleaf templates that utilize string concatenation in expressions.
29+
*
30+
* When Thymeleaf 2.1.5 is released, the Broadleaf dependency should be updated and this patch should be removed.
31+
*
32+
* @author Jeff Fischer
33+
*/
34+
public class EvaluationUtilPatch {
35+
36+
public static BigDecimal evaluateAsNumber(final Object object) {
37+
38+
if (object == null) {
39+
return null;
40+
}
41+
42+
if (object instanceof Number) {
43+
if (object instanceof BigDecimal) {
44+
return (BigDecimal)object;
45+
} else if (object instanceof BigInteger) {
46+
return new BigDecimal((BigInteger)object);
47+
} else if (object instanceof Short) {
48+
return new BigDecimal(((Short)object).intValue());
49+
} else if (object instanceof Integer) {
50+
return new BigDecimal(((Integer)object).intValue());
51+
} else if (object instanceof Long) {
52+
return new BigDecimal(((Long)object).longValue());
53+
} else if (object instanceof Float) {
54+
//noinspection UnpredictableBigDecimalConstructorCall
55+
return new BigDecimal(((Float)object).doubleValue());
56+
} else if (object instanceof Double) {
57+
//noinspection UnpredictableBigDecimalConstructorCall
58+
return new BigDecimal(((Double)object).doubleValue());
59+
}
60+
} else if (object instanceof String && ((String)object).length() > 0) {
61+
final char c0 = ((String)object).charAt(0);
62+
// This test will avoid trying to create the BigDecimal most of the times, which
63+
// will improve performance by avoiding lots of NumberFormatExceptions
64+
if ((c0 >= '0' && c0 <= '9') || c0 == '+' || c0 == '-') {
65+
try {
66+
return new BigDecimal(((String)object).trim());
67+
} catch (final NumberFormatException ignored) {
68+
return null;
69+
}
70+
}
71+
}
72+
73+
return null;
74+
75+
}
76+
77+
}

common/src/main/resources/bl-common-applicationContext.xml

+13
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,26 @@
126126
<constructor-arg name="moduleName" value="Conditional Field Annotation Transformation" />
127127
</bean>
128128

129+
<!-- NOTE: Remove this transformer when Thymeleaf 2.1.5 is released. See the Javadocs for org.broadleafcommerce.common.web.patch.EvaluationUtilPatch -->
130+
<bean id="blEvaluationUtilPatchClassTransformer" class="org.broadleafcommerce.common.extensibility.jpa.copy.DirectCopyClassTransformer">
131+
<constructor-arg name="moduleName" value="EvaluationUtil Patch Transformation" />
132+
<property name="xformTemplates">
133+
<map>
134+
<entry key="org.thymeleaf.util.EvaluationUtil"
135+
value="org.broadleafcommerce.common.web.patch.EvaluationUtilPatch"/>
136+
</map>
137+
</property>
138+
</bean>
139+
<!-- END NOTE -->
140+
129141
<!-- to be overriden by 3rd party modules -->
130142
<bean id="blMergedClassTransformers" class="org.springframework.beans.factory.config.ListFactoryBean">
131143
<property name="sourceList">
132144
<list>
133145
<ref bean="blAnnotationDirectCopyClassTransformer"/>
134146
<ref bean="blEntityMarkerClassTransformer"/>
135147
<ref bean="blConditionalFieldAnnotationClassTransformer"/>
148+
<ref bean="blEvaluationUtilPatchClassTransformer"/>
136149
</list>
137150
</property>
138151
</bean>

0 commit comments

Comments
 (0)