From 54f33d0d901f8eac87f0634098f1e3502801cc22 Mon Sep 17 00:00:00 2001 From: Will Godbe Date: Wed, 26 Aug 2020 13:34:06 -0700 Subject: [PATCH 1/5] Add TypeReference class to signalr java client --- .../com/microsoft/signalr/TypeReference.java | 35 +++++++++++++++++++ .../microsoft/signalr/HubConnectionTest.java | 2 -- .../signalr/JsonHubProtocolTest.java | 2 -- .../signalr/MessagePackHubProtocolTest.java | 2 -- 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java diff --git a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java new file mode 100644 index 000000000000..5ae8388ad3ca --- /dev/null +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java @@ -0,0 +1,35 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +package com.microsoft.signalr; + +import java.lang.reflect.Type; +import java.lang.reflect.ParameterizedType; + +public class TypeReference { + + private final Type type; + + /** + * Creates a new instance of {@link TypeReference}. + * + * To get the Type of Class Foo, use the following syntax: + *
{@code
+     * Type fooType = (new TypeReference() { }).getType();
+     * 
+ */ + public TypeReference() { + Type superclass = getClass().getGenericSuperclass(); + if (superclass instanceof Class) { + throw new RuntimeException("Missing type parameter."); + } + this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; + } + + /** + * Gets the referenced type. + */ + public Type getType() { + return this.type; + } +} \ No newline at end of file diff --git a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java index 89b5a0efdd9e..402a21c74b91 100644 --- a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java +++ b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java @@ -18,8 +18,6 @@ import org.junit.jupiter.api.Test; -import com.fasterxml.jackson.core.type.TypeReference; - import io.reactivex.Completable; import io.reactivex.Observable; import io.reactivex.Single; diff --git a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/JsonHubProtocolTest.java b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/JsonHubProtocolTest.java index 92febe6ad0bd..1b707d6a53e6 100644 --- a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/JsonHubProtocolTest.java +++ b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/JsonHubProtocolTest.java @@ -13,8 +13,6 @@ import org.junit.jupiter.api.Test; -import com.fasterxml.jackson.core.type.TypeReference; - class JsonHubProtocolTest { private JsonHubProtocol jsonHubProtocol = new JsonHubProtocol(); diff --git a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/MessagePackHubProtocolTest.java b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/MessagePackHubProtocolTest.java index 0b508d6e210f..37df89fabfb2 100644 --- a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/MessagePackHubProtocolTest.java +++ b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/MessagePackHubProtocolTest.java @@ -19,8 +19,6 @@ import org.junit.jupiter.api.Test; -import com.fasterxml.jackson.core.type.TypeReference; - class MessagePackHubProtocolTest { private MessagePackHubProtocol messagePackHubProtocol = new MessagePackHubProtocol(); From 27d114419bde9446f9ff0b2b7b7ac5fcb3788f4e Mon Sep 17 00:00:00 2001 From: Will Godbe Date: Wed, 26 Aug 2020 13:36:41 -0700 Subject: [PATCH 2/5] Fix syntax --- .../src/main/java/com/microsoft/signalr/TypeReference.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java index 5ae8388ad3ca..6c95e79ddbcd 100644 --- a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java @@ -16,7 +16,7 @@ public class TypeReference { * To get the Type of Class Foo, use the following syntax: *
{@code
      * Type fooType = (new TypeReference() { }).getType();
-     * 
+ * } */ public TypeReference() { Type superclass = getClass().getGenericSuperclass(); From d87530ddf8bb7016fbf7a9df27c135f9c5fec316 Mon Sep 17 00:00:00 2001 From: Will Godbe Date: Wed, 26 Aug 2020 14:35:35 -0700 Subject: [PATCH 3/5] Add comments --- .../main/java/com/microsoft/signalr/HubConnection.java | 9 +++++++++ .../main/java/com/microsoft/signalr/TypeReference.java | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java index b0bcbb364829..0976ae44dfc4 100644 --- a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java @@ -718,6 +718,7 @@ public Single invoke(Class returnType, String method, Object... args) /** * Invokes a hub method on the server using the specified method name and arguments. + * A Type can be retrieved from a Class by using {@link TypeReference} * * @param returnType The expected return type. * @param method The name of the server method to invoke. @@ -1097,6 +1098,7 @@ public Subscription on(String target, Action8 Subscription on(String target, Action1 callback, Type param1) { /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. + * A Type can be retrieved from a Class by using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1133,6 +1136,7 @@ public Subscription on(String target, Action2 callback, Type pa /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. + * A Type can be retrieved from a Class by using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1157,6 +1161,7 @@ public Subscription on(String target, Action3 callback, /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. + * A Type can be retrieved from a Class by using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1183,6 +1188,7 @@ public Subscription on(String target, Action4 c /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. + * A Type can be retrieved from a Class by using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1212,6 +1218,7 @@ public Subscription on(String target, Action5 Subscription on(String target, Action6 Subscription on(String target, Action7 { private final Type type; From cff942246362b87ae1a9627d1c5a70c47fb8f300 Mon Sep 17 00:00:00 2001 From: William Godbe Date: Wed, 26 Aug 2020 14:38:08 -0700 Subject: [PATCH 4/5] Update src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java Co-authored-by: Brennan --- .../src/main/java/com/microsoft/signalr/TypeReference.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java index e3e411819656..9fad029eecce 100644 --- a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java @@ -7,7 +7,7 @@ import java.lang.reflect.ParameterizedType; /** - * A utility for getting a Java Type from a literal Class + * A utility for getting a Java Type from a literal Class. */ public class TypeReference { @@ -35,4 +35,4 @@ public TypeReference() { public Type getType() { return this.type; } -} \ No newline at end of file +} From 0fd8a1c6ffc5bd4a6d5a1159365037396cf46db4 Mon Sep 17 00:00:00 2001 From: Will Godbe Date: Wed, 26 Aug 2020 15:18:34 -0700 Subject: [PATCH 5/5] Feedback --- .../com/microsoft/signalr/HubConnection.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java index 0976ae44dfc4..0aa4e4c336ca 100644 --- a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java @@ -718,7 +718,7 @@ public Single invoke(Class returnType, String method, Object... args) /** * Invokes a hub method on the server using the specified method name and arguments. - * A Type can be retrieved from a Class by using {@link TypeReference} + * A Type can be retrieved using {@link TypeReference} * * @param returnType The expected return type. * @param method The name of the server method to invoke. @@ -1098,7 +1098,7 @@ public Subscription on(String target, Action8 Subscription on(String target, Action1 callback, Type param1) { /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. - * A Type can be retrieved from a Class by using {@link TypeReference} + * A Type can be retrieved using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1136,7 +1136,7 @@ public Subscription on(String target, Action2 callback, Type pa /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. - * A Type can be retrieved from a Class by using {@link TypeReference} + * A Type can be retrieved using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1161,7 +1161,7 @@ public Subscription on(String target, Action3 callback, /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. - * A Type can be retrieved from a Class by using {@link TypeReference} + * A Type can be retrieved using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1188,7 +1188,7 @@ public Subscription on(String target, Action4 c /** * Registers a handler that will be invoked when the hub method with the specified method name is invoked. * Should be used for generic classes and Parameterized Collections, like List or Map. - * A Type can be retrieved from a Class by using {@link TypeReference} + * A Type can be retrieved using {@link TypeReference} * * @param target The name of the hub method to define. * @param callback The handler that will be raised when the hub method is invoked. @@ -1218,7 +1218,7 @@ public Subscription on(String target, Action5 Subscription on(String target, Action6 Subscription on(String target, Action7