Skip to content

Commit 1f54133

Browse files
committed
build: fix checktyle erros + simplify Java version detection
1 parent ae8077e commit 1f54133

3 files changed

Lines changed: 91 additions & 28 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/Handlebars.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
*/
1818
package com.github.jknack.handlebars;
1919

20-
import com.github.jknack.handlebars.helper.I18nHelper;
21-
import com.github.jknack.handlebars.internal.Files;
22-
import com.github.jknack.handlebars.internal.Throwing;
2320
import static org.apache.commons.lang3.Validate.isTrue;
2421
import static org.apache.commons.lang3.Validate.notEmpty;
2522
import static org.apache.commons.lang3.Validate.notNull;
@@ -39,25 +36,27 @@
3936
import java.util.List;
4037
import java.util.Map.Entry;
4138
import java.util.Set;
42-
import java.util.concurrent.atomic.AtomicInteger;
39+
40+
import javax.script.Bindings;
41+
import javax.script.ScriptEngine;
42+
import javax.script.ScriptEngineManager;
4343

4444
import org.slf4j.Logger;
4545

4646
import com.github.jknack.handlebars.cache.NullTemplateCache;
4747
import com.github.jknack.handlebars.cache.TemplateCache;
4848
import com.github.jknack.handlebars.helper.DefaultHelperRegistry;
49+
import com.github.jknack.handlebars.helper.I18nHelper;
50+
import com.github.jknack.handlebars.internal.Files;
4951
import com.github.jknack.handlebars.internal.FormatterChain;
5052
import com.github.jknack.handlebars.internal.HbsParserFactory;
53+
import com.github.jknack.handlebars.internal.Throwing;
5154
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
5255
import com.github.jknack.handlebars.io.CompositeTemplateLoader;
5356
import com.github.jknack.handlebars.io.StringTemplateSource;
5457
import com.github.jknack.handlebars.io.TemplateLoader;
5558
import com.github.jknack.handlebars.io.TemplateSource;
5659

57-
import javax.script.Bindings;
58-
import javax.script.ScriptEngine;
59-
import javax.script.ScriptEngineManager;
60-
6160
/**
6261
* <p>
6362
* Handlebars provides the power necessary to let you build semantic templates effectively with no
@@ -177,8 +176,12 @@ public boolean equals(final Object obj) {
177176
*/
178177
public static class Utils {
179178

179+
/** Current Java version: 8, 11, 15, etc. */
180180
public static final int javaVersion = javaVersion();
181181

182+
/** Prefix for Java version: 1.8 (mostly). */
183+
private static final String VERSION_PREFIX = "1.";
184+
182185
/**
183186
* Evaluate the given object and return true is the object is considered
184187
* empty. Nulls, empty list or array and false values are considered empty.
@@ -237,21 +240,9 @@ public static CharSequence escapeExpression(final CharSequence input) {
237240
return EscapingStrategy.DEF.escape(input);
238241
}
239242

240-
private static int javaVersion() {
241-
String version = System.getProperty("java.version");
242-
if (version.startsWith("1.")) {
243-
version = version.substring(2, 3);
244-
} else {
245-
int dot = version.indexOf(".");
246-
if (dot != -1) {
247-
version = version.substring(0, dot);
248-
}
249-
}
250-
try {
251-
return Integer.parseInt(version);
252-
} catch (NumberFormatException e) {
253-
return 8;
254-
}
243+
static int javaVersion() {
244+
String version = System.getProperty("java.specification.version").trim();
245+
return Integer.parseInt(version.replace(VERSION_PREFIX, ""));
255246
}
256247
}
257248

handlebars/src/main/java/com/github/jknack/handlebars/internal/BaseTemplate.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
*/
5858
abstract class BaseTemplate implements Template {
5959

60+
/** Java 15. */
61+
private static final int JAVA_15 = 15;
62+
6063
/**
6164
* The handlebars object. Required.
6265
*/
@@ -265,18 +268,18 @@ private boolean isDefault(final Method method) {
265268

266269
private Object invokeDefaultMethod(final Method method, final Class<?> lookupClass,
267270
final Object proxy, final Object... args) throws Throwable {
268-
if (Handlebars.Utils.javaVersion >= 15) {
271+
if (Handlebars.Utils.javaVersion >= JAVA_15) {
269272
MethodType methodType = MethodType.methodType(method.getReturnType(),
270273
method.getParameterTypes());
271274
return MethodHandles.lookup()
272275
.findSpecial(lookupClass, method.getName(), methodType, lookupClass)
273276
.bindTo(proxy)
274277
.invokeWithArguments(args);
275278
} else {
276-
// Jumping through these hoops is needed because calling unreflectSpecial requires that
277-
// the lookup instance have private access to the special caller. None of the static
278-
// factory methods for Lookup will give us an instance with the access modes we need,
279-
// so we work around it by calling the private constructor via reflection.
279+
// Jumping through these hoops is needed because calling unreflectSpecial requires
280+
// that the lookup instance have private access to the special caller. None of the
281+
// static factory methods for Lookup will give us an instance with the access modes
282+
// we need, so we work around it by calling the private constructor via reflection.
280283
Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
281284
.getDeclaredConstructor(Class.class, int.class);
282285
constructor.setAccessible(true);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.jknack.handlebars;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assume.assumeTrue;
5+
6+
import org.junit.Test;
7+
8+
public class JavaVersionTest {
9+
10+
@Test
11+
public void shouldCheckVersion8() {
12+
assumeTrue(Handlebars.Utils.javaVersion() == 8);
13+
assertEquals(8, Handlebars.Utils.javaVersion());
14+
}
15+
16+
@Test
17+
public void shouldCheckVersion9() {
18+
assumeTrue(Handlebars.Utils.javaVersion() == 9);
19+
assertEquals(9, Handlebars.Utils.javaVersion());
20+
}
21+
22+
@Test
23+
public void shouldCheckVersion10() {
24+
assumeTrue(Handlebars.Utils.javaVersion() == 10);
25+
assertEquals(10, Handlebars.Utils.javaVersion());
26+
}
27+
28+
@Test
29+
public void shouldCheckVersion11() {
30+
assumeTrue(Handlebars.Utils.javaVersion() == 11);
31+
assertEquals(11, Handlebars.Utils.javaVersion());
32+
}
33+
34+
@Test
35+
public void shouldCheckVersion12() {
36+
assumeTrue(Handlebars.Utils.javaVersion() == 12);
37+
assertEquals(12, Handlebars.Utils.javaVersion());
38+
}
39+
40+
@Test
41+
public void shouldCheckVersion13() {
42+
assumeTrue(Handlebars.Utils.javaVersion() == 13);
43+
assertEquals(13, Handlebars.Utils.javaVersion());
44+
}
45+
46+
@Test
47+
public void shouldCheckVersion14() {
48+
assumeTrue(Handlebars.Utils.javaVersion() == 14);
49+
assertEquals(14, Handlebars.Utils.javaVersion());
50+
}
51+
52+
@Test
53+
public void shouldCheckVersion15() {
54+
assumeTrue(Handlebars.Utils.javaVersion() == 15);
55+
assertEquals(15, Handlebars.Utils.javaVersion());
56+
}
57+
58+
@Test
59+
public void shouldCheckVersion16() {
60+
assumeTrue(Handlebars.Utils.javaVersion() == 16);
61+
assertEquals(16, Handlebars.Utils.javaVersion());
62+
}
63+
64+
@Test
65+
public void shouldCheckVersion17() {
66+
assumeTrue(Handlebars.Utils.javaVersion() == 17);
67+
assertEquals(17, Handlebars.Utils.javaVersion());
68+
}
69+
}

0 commit comments

Comments
 (0)