Skip to content

Commit 2c059b9

Browse files
committed
Update WebService.
1 parent 53685af commit 2c059b9

6 files changed

Lines changed: 33 additions & 85 deletions

File tree

kilo-client/src/main/java/org/httprpc/kilo/beans/BeanAdapter.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,15 @@ public static Object adapt(Object value) {
629629
* <li>{@link UUID}</li>
630630
* </ul>
631631
* <p>
632-
* If the target type is an {@link Enum}, the resulting value is the first
633-
* constant whose string representation matches the value's string
634-
* representation.
635-
* <p>
636632
* If the target type is an array, the provided value must be an array or
637633
* {@link Collection}. The return value is an array of the same length as
638634
* the provided value whose elements have been coerced to the array's
639635
* component type.
640636
* <p>
637+
* If the target type is an {@link Enum}, the resulting value is the first
638+
* constant whose string representation matches the value's string
639+
* representation.
640+
* <p>
641641
* If none of the previous conditions apply, the provided value is assumed
642642
* to be a map. If the if the target type is a {@link Record}, the
643643
* resulting value is instantiated via the type's canonical constructor
@@ -856,8 +856,6 @@ private static Object toRawType(Object value, Class<?> type) {
856856
return Period.parse(value.toString());
857857
} else if (type == UUID.class) {
858858
return UUID.fromString(value.toString());
859-
} else if (type.isEnum()) {
860-
return toEnum(value.toString(), type);
861859
} else if (type.isArray()) {
862860
if (value.getClass().isArray()) {
863861
return toArray(new ArrayAdapter(value), type);
@@ -866,6 +864,8 @@ private static Object toRawType(Object value, Class<?> type) {
866864
} else {
867865
throw new IllegalArgumentException("Value is not an array or collection.");
868866
}
867+
} else if (type.isEnum()) {
868+
return toEnum(value.toString(), type);
869869
} else {
870870
if (!(value instanceof Map<?, ?> map)) {
871871
throw new IllegalArgumentException("Value is not a map.");
@@ -880,6 +880,20 @@ private static Object toRawType(Object value, Class<?> type) {
880880
}
881881
}
882882

883+
private static Object toArray(Collection<?> collection, Class<?> type) {
884+
var componentType = type.getComponentType();
885+
886+
var array = Array.newInstance(componentType, collection.size());
887+
888+
var i = 0;
889+
890+
for (var element : collection) {
891+
Array.set(array, i++, toRawType(element, componentType));
892+
}
893+
894+
return array;
895+
}
896+
883897
private static Object toEnum(String value, Class<?> type) {
884898
var fields = type.getDeclaredFields();
885899

@@ -905,20 +919,6 @@ private static Object toEnum(String value, Class<?> type) {
905919
throw new IllegalArgumentException("Invalid value.");
906920
}
907921

908-
private static Object toArray(Collection<?> collection, Class<?> type) {
909-
var componentType = type.getComponentType();
910-
911-
var array = Array.newInstance(componentType, collection.size());
912-
913-
var i = 0;
914-
915-
for (var element : collection) {
916-
Array.set(array, i++, toRawType(element, componentType));
917-
}
918-
919-
return array;
920-
}
921-
922922
private static Object toRecord(Map<?, ?> map, Class<?> type) {
923923
var properties = getProperties(type);
924924

kilo-client/src/test/java/org/httprpc/kilo/beans/BeanAdapterTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,17 @@ public void testUUIDCoercion() {
269269
assertEquals(uuid, BeanAdapter.coerce(uuid.toString(), UUID.class));
270270
}
271271

272-
@Test
273-
public void testEnumCoercion() {
274-
assertEquals(DayOfWeek.MONDAY, BeanAdapter.coerce(DayOfWeek.MONDAY.toString(), DayOfWeek.class));
275-
}
276-
277272
@Test
278273
public void testArrayCoercion() {
279274
assertArrayEquals(new int[]{1, 2, 3}, (int[]) BeanAdapter.coerce(new String[] {"1", "2", "3"}, Integer.TYPE.arrayType()));
280275
assertArrayEquals(new int[]{1, 2, 3}, (int[]) BeanAdapter.coerce(listOf("1", "2", "3"), Integer.TYPE.arrayType()));
281276
}
282277

278+
@Test
279+
public void testEnumCoercion() {
280+
assertEquals(DayOfWeek.MONDAY, BeanAdapter.coerce(DayOfWeek.MONDAY.toString(), DayOfWeek.class));
281+
}
282+
283283
@Test
284284
public void testListCoercion() {
285285
assertInstanceOf(List.class, BeanAdapter.coerce(listOf(), List.class));

kilo-server/src/main/java/org/httprpc/kilo/IndexServlet.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public void init() {
5050

5151
var fieldType = field.getType();
5252

53-
if (WebService.class.isAssignableFrom(fieldType)
54-
&& field.getAnnotation(WebService.Instance.class) != null) {
53+
if (WebService.class.isAssignableFrom(fieldType) && field.getAnnotation(WebService.Instance.class) != null) {
5554
field.setAccessible(true);
5655

5756
try {

kilo-server/src/main/java/org/httprpc/kilo/WebService.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.xml.transform.TransformerException;
3535
import javax.xml.transform.dom.DOMSource;
3636
import javax.xml.transform.stream.StreamResult;
37+
import java.io.File;
3738
import java.io.IOException;
3839
import java.lang.annotation.ElementType;
3940
import java.lang.annotation.Retention;
@@ -46,6 +47,7 @@
4647
import java.lang.reflect.ParameterizedType;
4748
import java.lang.reflect.Type;
4849
import java.net.URI;
50+
import java.net.URL;
4951
import java.nio.charset.StandardCharsets;
5052
import java.nio.file.Files;
5153
import java.nio.file.Path;
@@ -1485,9 +1487,10 @@ private TypeDescriptor describeGenericType(Type type) {
14851487
private TypeDescriptor describeRawType(Class<?> type) {
14861488
if (type.isPrimitive()
14871489
|| type == Object.class
1488-
|| type == Boolean.class
14891490
|| Number.class.isAssignableFrom(type)
1490-
|| String.class.isAssignableFrom(type)
1491+
|| type == Boolean.class
1492+
|| type == Character.class
1493+
|| CharSequence.class.isAssignableFrom(type)
14911494
|| type == Void.class
14921495
|| Date.class.isAssignableFrom(type)
14931496
|| type == Instant.class
@@ -1497,17 +1500,12 @@ private TypeDescriptor describeRawType(Class<?> type) {
14971500
|| type == Duration.class
14981501
|| type == Period.class
14991502
|| type == UUID.class
1500-
|| type == URI.class
1501-
|| type == Path.class
1502-
|| type == Part.class
1503+
|| type == URL.class || type == URI.class
1504+
|| type == File.class || type == Path.class
15031505
|| type == Document.class) {
15041506
return new TypeDescriptor(type, true);
15051507
} else if (type.isArray()) {
15061508
return new IterableTypeDescriptor(describeRawType(type.getComponentType()));
1507-
} else if (Iterable.class.isAssignableFrom(type)) {
1508-
return new IterableTypeDescriptor(describeRawType(Object.class));
1509-
} else if (Map.class.isAssignableFrom(type)) {
1510-
return new MapTypeDescriptor(describeRawType(Object.class), describeRawType(Object.class));
15111509
} else if (type.isEnum()) {
15121510
return describeEnumeration(type);
15131511
} else {

kilo-test/src/main/java/org/httprpc/kilo/test/TestService.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
import java.time.LocalDateTime;
3737
import java.time.LocalTime;
3838
import java.time.Period;
39-
import java.util.ArrayList;
4039
import java.util.Arrays;
41-
import java.util.HashMap;
42-
import java.util.HashSet;
4340
import java.util.Iterator;
4441
import java.util.List;
4542
import java.util.Map;
@@ -86,15 +83,6 @@ public double getE() {
8683
}
8784
}
8885

89-
public static class TestList extends ArrayList<Integer> {
90-
}
91-
92-
public static class TestMap extends HashMap<String, Double> {
93-
}
94-
95-
public static class TestSet extends HashSet<Double> {
96-
}
97-
9886
public interface Response {
9987
@Required
10088
String getString();
@@ -271,24 +259,6 @@ public E testGetE() {
271259
return null;
272260
}
273261

274-
@RequestMethod("GET")
275-
@ResourcePath("list")
276-
public TestList testGetList() {
277-
return new TestList();
278-
}
279-
280-
@RequestMethod("GET")
281-
@ResourcePath("map")
282-
public TestMap testGetMap() {
283-
return new TestMap();
284-
}
285-
286-
@RequestMethod("GET")
287-
@ResourcePath("set")
288-
public TestSet testGetSet() {
289-
return new TestSet();
290-
}
291-
292262
@RequestMethod("POST")
293263
public void testPost(@Required Integer number, List<String> strings) {
294264
if (strings.size() != number) {

kilo-test/src/test/resources/org/httprpc/kilo/test/api/test.html

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,6 @@
225225
<!-- -->
226226
<li><a href="#/test/math/sum">/test/math/sum</a></li>
227227
<!-- -->
228-
<li><a href="#/test/set">/test/set</a></li>
229-
<!-- -->
230228
<li><a href="#/test/varargs">/test/varargs</a></li>
231229
<!-- -->
232230
<li><a href="#/test/xml">/test/xml</a></li>
@@ -932,10 +930,6 @@ <h2><code>/test/invalid-result</code></h2>
932930
<section id="/test/list">
933931
<h2><code>/test/list</code></h2>
934932
<!-- -->
935-
<pre><span class="method GET">GET</span> : [Object]</pre>
936-
<!-- -->
937-
<!-- -->
938-
<!-- -->
939933
<pre><span class="method POST">POST</span> : [String]</pre>
940934
<!-- -->
941935
<!-- -->
@@ -966,10 +960,6 @@ <h2><code>/test/list</code></h2>
966960
<section id="/test/map">
967961
<h2><code>/test/map</code></h2>
968962
<!-- -->
969-
<pre><span class="method GET">GET</span> : [Object: Object]</pre>
970-
<!-- -->
971-
<!-- -->
972-
<!-- -->
973963
<pre><span class="method POST">POST</span> : [String: Double]</pre>
974964
<!-- -->
975965
<!-- -->
@@ -1062,15 +1052,6 @@ <h2><code>/test/math/sum</code></h2>
10621052
<!-- -->
10631053
</section>
10641054
<!-- -->
1065-
<section id="/test/set">
1066-
<h2><code>/test/set</code></h2>
1067-
<!-- -->
1068-
<pre><span class="method GET">GET</span> : [Object]</pre>
1069-
<!-- -->
1070-
<!-- -->
1071-
<!-- -->
1072-
</section>
1073-
<!-- -->
10741055
<section id="/test/varargs">
10751056
<h2><code>/test/varargs</code></h2>
10761057
<!-- -->

0 commit comments

Comments
 (0)