From a54c5baf7a1fbf90487f375b8dc93d55216b7944 Mon Sep 17 00:00:00 2001 From: William Godbe Date: Thu, 27 Aug 2020 15:56:51 -0700 Subject: [PATCH] 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 * Feedback Co-authored-by: Brennan --- .../com/microsoft/signalr/HubConnection.java | 9 +++++ .../com/microsoft/signalr/TypeReference.java | 38 +++++++++++++++++++ .../microsoft/signalr/HubConnectionTest.java | 2 - .../signalr/JsonHubProtocolTest.java | 2 - .../signalr/MessagePackHubProtocolTest.java | 2 - 5 files changed, 47 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/HubConnection.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java index b0bcbb3648..0aa4e4c336 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 class HubConnection implements AutoCloseable { /** * Invokes a hub method on the server using the specified method name and arguments. + * A Type can be retrieved using {@link TypeReference} * * @param returnType The expected return type. * @param method The name of the server method to invoke. @@ -1097,6 +1098,7 @@ public class HubConnection implements AutoCloseable { /** * 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 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. @@ -1113,6 +1115,7 @@ public class HubConnection implements AutoCloseable { /** * 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 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 class HubConnection implements AutoCloseable { /** * 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 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 class HubConnection implements AutoCloseable { /** * 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 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 class HubConnection implements AutoCloseable { /** * 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 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 class HubConnection implements AutoCloseable { /** * 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 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. @@ -1243,6 +1250,7 @@ public class HubConnection implements AutoCloseable { /** * 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 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. @@ -1277,6 +1285,7 @@ public class HubConnection implements AutoCloseable { /** * 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 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. 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 0000000000..9fad029eec --- /dev/null +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/TypeReference.java @@ -0,0 +1,38 @@ +// 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; + +/** + * A utility for getting a Java Type from a literal Class. + */ +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; + } +} 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 89b5a0efdd..402a21c74b 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 java.util.concurrent.atomic.AtomicReference; 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 92febe6ad0..1b707d6a53 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 java.util.List; 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 0b508d6e21..37df89fabf 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 java.util.TreeMap; import org.junit.jupiter.api.Test; -import com.fasterxml.jackson.core.type.TypeReference; - class MessagePackHubProtocolTest { private MessagePackHubProtocol messagePackHubProtocol = new MessagePackHubProtocol();