Skip to content

Commit a54c5ba

Browse files
Add TypeReference class to signalr java client (#25286)
* Add TypeReference class to signalr java client * Fix syntax * Add comments * Update src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java Co-authored-by: Brennan <[email protected]> * Feedback Co-authored-by: Brennan <[email protected]>
1 parent 7b8fe53 commit a54c5ba

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ public <T> Single<T> invoke(Class<T> returnType, String method, Object... args)
718718

719719
/**
720720
* Invokes a hub method on the server using the specified method name and arguments.
721+
* A Type can be retrieved using {@link TypeReference}
721722
*
722723
* @param returnType The expected return type.
723724
* @param method The name of the server method to invoke.
@@ -1097,6 +1098,7 @@ public <T1, T2, T3, T4, T5, T6, T7, T8> Subscription on(String target, Action8<T
10971098
/**
10981099
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
10991100
* Should be used for generic classes and Parameterized Collections, like List or Map.
1101+
* A Type can be retrieved using {@link TypeReference}
11001102
*
11011103
* @param target The name of the hub method to define.
11021104
* @param callback The handler that will be raised when the hub method is invoked.
@@ -1113,6 +1115,7 @@ public <T1> Subscription on(String target, Action1<T1> callback, Type param1) {
11131115
/**
11141116
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
11151117
* Should be used for generic classes and Parameterized Collections, like List or Map.
1118+
* A Type can be retrieved using {@link TypeReference}
11161119
*
11171120
* @param target The name of the hub method to define.
11181121
* @param callback The handler that will be raised when the hub method is invoked.
@@ -1133,6 +1136,7 @@ public <T1, T2> Subscription on(String target, Action2<T1, T2> callback, Type pa
11331136
/**
11341137
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
11351138
* Should be used for generic classes and Parameterized Collections, like List or Map.
1139+
* A Type can be retrieved using {@link TypeReference}
11361140
*
11371141
* @param target The name of the hub method to define.
11381142
* @param callback The handler that will be raised when the hub method is invoked.
@@ -1157,6 +1161,7 @@ public <T1, T2, T3> Subscription on(String target, Action3<T1, T2, T3> callback,
11571161
/**
11581162
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
11591163
* Should be used for generic classes and Parameterized Collections, like List or Map.
1164+
* A Type can be retrieved using {@link TypeReference}
11601165
*
11611166
* @param target The name of the hub method to define.
11621167
* @param callback The handler that will be raised when the hub method is invoked.
@@ -1183,6 +1188,7 @@ public <T1, T2, T3, T4> Subscription on(String target, Action4<T1, T2, T3, T4> c
11831188
/**
11841189
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
11851190
* Should be used for generic classes and Parameterized Collections, like List or Map.
1191+
* A Type can be retrieved using {@link TypeReference}
11861192
*
11871193
* @param target The name of the hub method to define.
11881194
* @param callback The handler that will be raised when the hub method is invoked.
@@ -1212,6 +1218,7 @@ public <T1, T2, T3, T4, T5> Subscription on(String target, Action5<T1, T2, T3, T
12121218
/**
12131219
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
12141220
* Should be used for generic classes and Parameterized Collections, like List or Map.
1221+
* A Type can be retrieved using {@link TypeReference}
12151222
*
12161223
* @param target The name of the hub method to define.
12171224
* @param callback The handler that will be raised when the hub method is invoked.
@@ -1243,6 +1250,7 @@ public <T1, T2, T3, T4, T5, T6> Subscription on(String target, Action6<T1, T2, T
12431250
/**
12441251
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
12451252
* Should be used for generic classes and Parameterized Collections, like List or Map.
1253+
* A Type can be retrieved using {@link TypeReference}
12461254
*
12471255
* @param target The name of the hub method to define.
12481256
* @param callback The handler that will be raised when the hub method is invoked.
@@ -1277,6 +1285,7 @@ public <T1, T2, T3, T4, T5, T6, T7> Subscription on(String target, Action7<T1, T
12771285
/**
12781286
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
12791287
* Should be used for generic classes and Parameterized Collections, like List or Map.
1288+
* A Type can be retrieved using {@link TypeReference}
12801289
*
12811290
* @param target The name of the hub method to define.
12821291
* @param callback The handler that will be raised when the hub method is invoked.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
package com.microsoft.signalr;
5+
6+
import java.lang.reflect.Type;
7+
import java.lang.reflect.ParameterizedType;
8+
9+
/**
10+
* A utility for getting a Java Type from a literal Class.
11+
*/
12+
public class TypeReference<T> {
13+
14+
private final Type type;
15+
16+
/**
17+
* Creates a new instance of {@link TypeReference}.
18+
*
19+
* To get the Type of Class Foo, use the following syntax:
20+
* <pre>{@code
21+
* Type fooType = (new TypeReference<Foo>() { }).getType();
22+
* }</pre>
23+
*/
24+
public TypeReference() {
25+
Type superclass = getClass().getGenericSuperclass();
26+
if (superclass instanceof Class) {
27+
throw new RuntimeException("Missing type parameter.");
28+
}
29+
this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0];
30+
}
31+
32+
/**
33+
* Gets the referenced type.
34+
*/
35+
public Type getType() {
36+
return this.type;
37+
}
38+
}

src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
import com.fasterxml.jackson.core.type.TypeReference;
22-
2321
import io.reactivex.Completable;
2422
import io.reactivex.Observable;
2523
import io.reactivex.Single;

src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/JsonHubProtocolTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
import org.junit.jupiter.api.Test;
1515

16-
import com.fasterxml.jackson.core.type.TypeReference;
17-
1816
class JsonHubProtocolTest {
1917
private JsonHubProtocol jsonHubProtocol = new JsonHubProtocol();
2018

src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/MessagePackHubProtocolTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import org.junit.jupiter.api.Test;
2121

22-
import com.fasterxml.jackson.core.type.TypeReference;
23-
2422
class MessagePackHubProtocolTest {
2523
private MessagePackHubProtocol messagePackHubProtocol = new MessagePackHubProtocol();
2624

0 commit comments

Comments
 (0)