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 <brecon@microsoft.com>

* Feedback

Co-authored-by: Brennan <brecon@microsoft.com>
This commit is contained in:
William Godbe 2020-08-27 15:56:51 -07:00 committed by GitHub
parent 7b8fe53234
commit a54c5baf7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 6 deletions

View File

@ -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.

View File

@ -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<T> {
private final Type type;
/**
* Creates a new instance of {@link TypeReference}.
*
* To get the Type of Class Foo, use the following syntax:
* <pre>{@code
* Type fooType = (new TypeReference<Foo>() { }).getType();
* }</pre>
*/
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;
}
}

View File

@ -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;

View File

@ -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();

View File

@ -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();