Skip to content

Commit fcc7b2a

Browse files
Fix index expression issue in Java
1 parent d988d41 commit fcc7b2a

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

Java/Object Validator/lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tasks.named('test') {
4747
}
4848

4949
archivesBaseName = 'com.quicksilver.objectvalidator'
50-
version = '2.1.0'
50+
version = '2.1.1'
5151
description = 'A library providing functions that let users validate the values in objects with JSON-formatted rules.'
5252
group = 'io.github.quicksilver0218'
5353

Java/Object Validator/lib/src/main/java/com/quicksilver/objectvalidator/runtime/Condition.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ private static void handleKey(Map<?, ?> map, String key, List<Object> values) {
4545

4646
private static void handleValues(List<Object> values, String name, List<Object> newValues) throws ReflectiveOperationException {
4747
for (Object value : values)
48-
if (value == null)
49-
newValues.add(null);
50-
else if (value instanceof Map m)
51-
handleKey(m, name, newValues);
52-
else {
53-
try {
48+
switch (value) {
49+
case null -> newValues.add(null);
50+
case Map<?, ?> m -> handleKey(m, name, newValues);
51+
default -> {
5452
if (value instanceof Object[] || value instanceof List)
55-
handleIndex(value, Integer.parseInt(name), newValues);
56-
} catch (NumberFormatException unused) {}
57-
handleField(value, name, newValues);
53+
try {
54+
handleIndex(value, Integer.parseInt(name), newValues);
55+
return;
56+
} catch (NumberFormatException unused) {}
57+
handleField(value, name, newValues);
58+
}
5859
}
5960
}
6061

@@ -68,50 +69,50 @@ public boolean check(Object root, String rootFieldExpression, HashSet<String> pa
6869
fullName += name;
6970
if (fullName.equals("*"))
7071
for (Object o : values)
71-
if (o == null)
72-
newValues.add(null);
73-
else if (o instanceof Object[] a)
74-
for (Object item : a)
75-
newValues.add(item);
76-
else if (o instanceof Iterable e)
77-
for (Object item : e)
78-
newValues.add(item);
79-
else
80-
throw new RuntimeException("Unsupported type for iteration: " + o.getClass());
72+
switch (o) {
73+
case null -> newValues.add(null);
74+
case Object[] a -> {
75+
for (Object item : a)
76+
newValues.add(item);
77+
}
78+
case Iterable<?> e -> {
79+
for (Object item : e)
80+
newValues.add(item);
81+
}
82+
default -> throw new RuntimeException("Unsupported type for iteration: " + o.getClass());
83+
}
8184
else if (name.length() >= 3 && name.substring(name.length() - 3, name.length() - 1).equals("//"))
8285
fullName = fullName.substring(0, fullName.length() - 3) + fullName.substring(fullName.length() - 2);
8386
else if (name.length() >= 2 && name.charAt(name.length() - 2) == '/') {
8487
fullName = fullName.substring(0, fullName.length() - 2);
8588
switch (Character.toUpperCase(name.charAt(name.length() - 1))) {
86-
case 'C':
89+
case 'C' -> {
8790
fullName += ".";
8891
continue;
89-
case 'F':
92+
}
93+
case 'F' -> {
9094
for (Object o : values)
9195
if (o == null)
9296
newValues.add(null);
9397
else
9498
handleField(o, fullName, newValues);
95-
break;
96-
case 'I':
99+
}
100+
case 'I' -> {
97101
for (Object o : values)
98102
if (o == null)
99103
newValues.add(null);
100104
else
101105
handleIndex(o, Integer.parseInt(fullName), newValues);
102-
break;
103-
case 'K':
106+
}
107+
case 'K' -> {
104108
for (Object o : values)
105109
if (o == null)
106110
newValues.add(null);
107111
else
108112
handleKey((Map<?, ?>)o, fullName, newValues);
109-
break;
110-
case '*':
111-
handleValues(values, fullName + "*", newValues);
112-
break;
113-
default:
114-
throw new RuntimeException("Unsupported suffix: " + name.charAt(name.length() - 1));
113+
}
114+
case '*' -> handleValues(values, fullName + "*", newValues);
115+
default -> throw new RuntimeException("Unsupported suffix: " + name.charAt(name.length() - 1));
115116
}
116117
} else
117118
handleValues(values, fullName, newValues);

0 commit comments

Comments
 (0)