Skip to content

Commit 9c6b7d8

Browse files
committed
fix: instrument JsonValue[] arguments
Close #23
1 parent 9fe8df0 commit 9c6b7d8

12 files changed

+206
-8
lines changed

src/main/java/com/flowingcode/vaadin/jsonmigration/ClassInstrumentationJacksonHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public static String getConvertedTypeDescriptor(Class<?> type) {
4848
return Type.getDescriptor(StringNode.class);
4949
} else if (JsonValue.class.isAssignableFrom(type)) {
5050
return Type.getDescriptor(JsonNode.class);
51+
} else if (JsonValue[].class.isAssignableFrom(type)) {
52+
return "[" + getConvertedTypeDescriptor(type.getComponentType());
5153
}
5254
return Type.getDescriptor(type);
5355
}

src/main/java/com/flowingcode/vaadin/jsonmigration/ClassInstrumentationUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ private List<Method> getInstrumentableMethods(Class<?> parent) {
237237

238238
private static boolean hasJsonValueParameters(Method method) {
239239
for (Class<?> paramType : method.getParameterTypes()) {
240-
if (JsonValue.class.isAssignableFrom(paramType)) {
240+
if (JsonValue.class.isAssignableFrom(paramType)
241+
|| JsonValue[].class.isAssignableFrom(paramType)) {
241242
return true;
242243
}
243244
}
@@ -582,7 +583,7 @@ private int loadParameter(MethodVisitor mv, Class<?> paramType, int localVarInde
582583
private String getMethodDescriptor(Method method, boolean convertJsonValueParams) {
583584
StringBuilder sb = new StringBuilder("(");
584585
for (Class<?> paramType : method.getParameterTypes()) {
585-
if (convertJsonValueParams && JsonValue.class.isAssignableFrom(paramType)) {
586+
if (convertJsonValueParams) {
586587
sb.append(getConvertedTypeDescriptor(paramType));
587588
} else {
588589
sb.append(Type.getDescriptor(paramType));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
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 com.flowingcode.vaadin.jsonmigration;
21+
22+
import com.vaadin.flow.component.ClientCallable;
23+
import elemental.json.JsonObject;
24+
25+
public class ClientCallable_ArrayOfJsonObject__V extends BaseClientCallable {
26+
27+
@ClientCallable
28+
public void test(JsonObject[] arg) {
29+
trace();
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
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 com.flowingcode.vaadin.jsonmigration;
21+
22+
import com.vaadin.flow.component.ClientCallable;
23+
import elemental.json.JsonObject;
24+
25+
public class ClientCallable_JsonObjectVarargs__V extends BaseClientCallable {
26+
27+
@ClientCallable
28+
public void test(JsonObject[] arg) {
29+
trace();
30+
}
31+
}

src/test/java/com/flowingcode/vaadin/jsonmigration/ClientCallablesTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ private Object invokeTestMethod(BaseClientCallable instrumented, Object... args)
7777

7878
protected abstract Object createJsonObject();
7979

80+
protected abstract Object createArrayOfJsonObject();
81+
8082
@Test
8183
public void test__V() throws Exception {
8284
ClientCallable__V instrumented =
@@ -282,4 +284,26 @@ public void test_JsonObject__V() throws Exception {
282284
invokeTestMethod(instrumented, createJsonObject());
283285
assertTrue(instrumented.hasBeenTraced());
284286
}
287+
288+
@Test
289+
public void test_ArrayOfJsonObject__V() throws Exception {
290+
ClientCallable_ArrayOfJsonObject__V instrumented =
291+
instrumentClass(ClientCallable_ArrayOfJsonObject__V.class)
292+
.getDeclaredConstructor()
293+
.newInstance();
294+
invokeTestMethod(instrumented, createArrayOfJsonObject());
295+
assertTrue(instrumented.hasBeenTraced());
296+
}
297+
298+
299+
@Test
300+
public void test_JsonObjectArgs__V() throws Exception {
301+
ClientCallable_JsonObjectVarargs__V instrumented =
302+
instrumentClass(ClientCallable_JsonObjectVarargs__V.class)
303+
.getDeclaredConstructor()
304+
.newInstance();
305+
invokeTestMethod(instrumented, createArrayOfJsonObject());
306+
assertTrue(instrumented.hasBeenTraced());
307+
}
308+
285309
}

src/test/java/com/flowingcode/vaadin/jsonmigration/ClientCallablesTest24.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.vaadin.flow.component.Component;
2626
import elemental.json.Json;
27+
import elemental.json.JsonObject;
2728
import elemental.json.JsonValue;
2829
import org.junit.Rule;
2930
import org.junit.rules.ExpectedException;
@@ -37,7 +38,7 @@ public class ClientCallablesTest24 extends ClientCallablesTest {
3738
@Override
3839
protected <T extends Component> Class<? extends T> instrumentClass(Class<T> clazz) {
3940
for (Class<?> arg : getClientCallableTestMethod(clazz).getParameterTypes()) {
40-
if (JsonValue.class.isAssignableFrom(arg)) {
41+
if (JsonValue.class.isAssignableFrom(arg) || JsonValue[].class.isAssignableFrom(arg)) {
4142
thrown.expect(IllegalArgumentException.class);
4243
thrown.expectMessage(containsString(ERRMSG));
4344
break;
@@ -74,7 +75,13 @@ protected Object createJsonArray() {
7475
}
7576

7677
@Override
77-
protected Object createJsonObject() {
78+
protected JsonObject createJsonObject() {
7879
return Json.createObject();
7980
}
81+
82+
@Override
83+
protected Object createArrayOfJsonObject() {
84+
return new JsonObject[] {createJsonObject(), createJsonObject()};
85+
}
86+
8087
}

src/test/java/com/flowingcode/vaadin/jsonmigration/ClientCallablesTest25.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ClientCallablesTest25 extends ClientCallablesTest {
4242
@Override
4343
protected <T extends Component> Class<? extends T> instrumentClass(Class<T> clazz) {
4444
for (Class<?> arg : getClientCallableTestMethod(clazz).getParameterTypes()) {
45-
if (JsonValue.class.isAssignableFrom(arg)) {
45+
if (JsonValue.class.isAssignableFrom(arg) || JsonValue[].class.isAssignableFrom(arg)) {
4646
thrown.expect(IllegalArgumentException.class);
4747
thrown.expectMessage(containsString(ERRMSG));
4848
break;
@@ -77,7 +77,13 @@ protected Object createJsonArray() {
7777
}
7878

7979
@Override
80-
protected Object createJsonObject() {
80+
protected ObjectNode createJsonObject() {
8181
return new ObjectNode(JsonNodeFactory.instance);
8282
}
83+
84+
@Override
85+
protected Object createArrayOfJsonObject() {
86+
return new ObjectNode[] {createJsonObject(), createJsonObject()};
87+
}
88+
8389
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
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 com.flowingcode.vaadin.jsonmigration;
21+
22+
import elemental.json.JsonObject;
23+
24+
public class LegacyClientCallable_ArrayOfJsonObject__V extends BaseClientCallable {
25+
26+
@LegacyClientCallable
27+
public void test(JsonObject[] arg) {
28+
trace();
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
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 com.flowingcode.vaadin.jsonmigration;
21+
22+
import elemental.json.JsonObject;
23+
24+
public class LegacyClientCallable_JsonObjectVarargs__V extends BaseClientCallable {
25+
26+
@LegacyClientCallable
27+
public void test(JsonObject[] arg) {
28+
trace();
29+
}
30+
}

src/test/java/com/flowingcode/vaadin/jsonmigration/LegacyClientCallablesTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ private static Object invokeTestMethod(BaseClientCallable instrumented, Object..
7777

7878
protected abstract Object createJsonObject();
7979

80+
protected abstract Object createArrayOfJsonObject();
81+
8082
@Test
8183
public void test__V() throws Exception {
8284
LegacyClientCallable__V instrumented =
@@ -312,4 +314,25 @@ public void test_JsonObject__V() throws Exception {
312314
invokeTestMethod(instrumented, createJsonObject());
313315
assertTrue(instrumented.hasBeenTraced());
314316
}
317+
318+
@Test
319+
public void test_ArrayOfJsonObject__V() throws Exception {
320+
LegacyClientCallable_ArrayOfJsonObject__V instrumented =
321+
instrumentClass(LegacyClientCallable_ArrayOfJsonObject__V.class)
322+
.getDeclaredConstructor()
323+
.newInstance();
324+
invokeTestMethod(instrumented, createArrayOfJsonObject());
325+
assertTrue(instrumented.hasBeenTraced());
326+
}
327+
328+
@Test
329+
public void test_JsonObjectVarargs__V() throws Exception {
330+
LegacyClientCallable_JsonObjectVarargs__V instrumented =
331+
instrumentClass(LegacyClientCallable_JsonObjectVarargs__V.class)
332+
.getDeclaredConstructor()
333+
.newInstance();
334+
invokeTestMethod(instrumented, createArrayOfJsonObject());
335+
assertTrue(instrumented.hasBeenTraced());
336+
}
337+
315338
}

0 commit comments

Comments
 (0)