Skip to content

Commit

Permalink
fix initialization of not matching number fields
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzardo committed Jun 5, 2024
1 parent 776bc77 commit 740a58f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,33 @@ public Class<?> getJavaClass() {
builder.field(defineAndSet.name, type, value);
continue;
}

if (value instanceof Number) {
if (type == int.class || type == Integer.class) {
builder.field(defineAndSet.name, type, ((Number) value).intValue());
continue;
}
if (type == long.class || type == Long.class) {
builder.field(defineAndSet.name, type, ((Number) value).longValue());
continue;
}
if (type == float.class || type == Float.class) {
builder.field(defineAndSet.name, type, ((Number) value).floatValue());
continue;
}
if (type == double.class || type == Double.class) {
builder.field(defineAndSet.name, type, ((Number) value).doubleValue());
continue;
}
if (type == short.class || type == Short.class) {
builder.field(defineAndSet.name, type, ((Number) value).shortValue());
continue;
}
if (type == byte.class || type == Byte.class) {
builder.field(defineAndSet.name, type, ((Number) value).byteValue());
continue;
}
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/wizzardo/tools/evaluation/EvalToolsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,38 @@ public void test_generate_class() {
Assert.assertNotNull(Arrays.stream(fields).filter(it-> it.getName().equals("name")).findFirst().orElse(null));
}

@Test
public void test_generate_class_2() throws InstantiationException, IllegalAccessException {
Map<String, Object> model = new HashMap<String, Object>();

Expression e = EvalTools.prepare("" +
" static class SimpleClass {\n" +
" double value = 1;" +
" int count = 2;" +
" boolean flag = true;" +
" }\n" +
"SimpleClass.class"
);

Object result = e.get(new HashMap<>());

Assert.assertNotNull(result);
Assert.assertTrue(result instanceof Class);
Assert.assertEquals("SimpleClass", ((Class<?>) result).getSimpleName());
Field[] fields = ((Class<?>) result).getDeclaredFields();
Assert.assertTrue(fields.length >= 3);
Field valueField = Arrays.stream(fields).filter(it -> it.getName().equals("value")).findFirst().orElse(null);
Field countField = Arrays.stream(fields).filter(it -> it.getName().equals("count")).findFirst().orElse(null);
Field flagField = Arrays.stream(fields).filter(it -> it.getName().equals("flag")).findFirst().orElse(null);
Assert.assertNotNull(valueField);
Assert.assertNotNull(countField);
Assert.assertNotNull(flagField);
Object instance = ((Class<?>) result).newInstance();
Assert.assertEquals(1.0, valueField.get(instance));
Assert.assertEquals(2, countField.get(instance));
Assert.assertEquals(true, flagField.get(instance));
}

@Test
public void test_line_number() {
Map<String, Object> model = new HashMap<String, Object>();
Expand Down

0 comments on commit 740a58f

Please sign in to comment.