Skip to content

Commit a980583

Browse files
authored
Expose class information for TypeReference<T> (Azure#24568)
1 parent 92d47eb commit a980583

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.22.0-beta.2 (Unreleased)
44

55
### Features Added
6+
- Added `getJavaClass` method to retrieve the representing instance of the `TypeReference` created.
67

78
### Breaking Changes
89

sdk/core/azure-core/src/main/java/com/azure/core/util/serializer/TypeReference.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.core.util.serializer;
55

6+
import com.azure.core.implementation.TypeUtil;
67
import com.azure.core.util.logging.ClientLogger;
78

89
import java.lang.reflect.ParameterizedType;
@@ -41,23 +42,27 @@ public abstract class TypeReference<T> {
4142
private static final Map<Class<?>, TypeReference<?>> CACHE = new ConcurrentHashMap<>();
4243

4344
private final Type javaType;
45+
private final Class<T> clazz;
4446

4547
/**
4648
* Constructs a new {@link TypeReference} which maintains generic information.
4749
*
4850
* @throws IllegalArgumentException If the reference is constructed without type information.
4951
*/
52+
@SuppressWarnings("unchecked")
5053
public TypeReference() {
5154
Type superClass = this.getClass().getGenericSuperclass();
5255
if (superClass instanceof Class) {
5356
throw LOGGER.logExceptionAsError(new IllegalArgumentException(MISSING_TYPE));
5457
} else {
5558
this.javaType = ((ParameterizedType) superClass).getActualTypeArguments()[0];
5659
}
60+
this.clazz = (Class<T>) TypeUtil.getRawClass(javaType);
5761
}
5862

5963
private TypeReference(Class<T> clazz) {
6064
this.javaType = clazz;
65+
this.clazz = clazz;
6166
}
6267

6368
/**
@@ -88,4 +93,14 @@ public static <T> TypeReference<T> createInstance(Class<T> clazz) {
8893
*/
8994
return (TypeReference<T>) CACHE.computeIfAbsent(clazz, c -> new TypeReference<T>(clazz) { });
9095
}
96+
97+
/**
98+
* Returns the {@link Class} representing instance of the {@link TypeReference} created.
99+
*
100+
* @return The {@link Class} representing instance of the {@link TypeReference} created
101+
* using the {@link TypeReference#createInstance(Class)}, otherwise returns {@code null}.
102+
*/
103+
public Class<T> getJavaClass() {
104+
return this.clazz;
105+
}
91106
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.core.util.serializer;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertThrows;
13+
14+
public class TypeReferenceTests {
15+
16+
@Test
17+
public void createGenericTypeReference() {
18+
final TypeReference<HashMap<String, Object>> typeReference = new TypeReference<HashMap<String, Object>>() { };
19+
final Map<String, Object> expectedJavaType = new HashMap<String, Object>() { };
20+
assertEquals(expectedJavaType.getClass().getGenericSuperclass(), typeReference.getJavaType());
21+
assertEquals(HashMap.class, typeReference.getJavaClass());
22+
}
23+
24+
@Test
25+
public void createFactoryInstance() {
26+
TypeReference<Integer> typeReference = TypeReference.createInstance(int.class);
27+
assertEquals(int.class, typeReference.getJavaType());
28+
assertEquals(int.class, typeReference.getJavaClass());
29+
}
30+
31+
@SuppressWarnings("rawtypes")
32+
@Test
33+
public void createTypeReferenceWithoutType() {
34+
IllegalArgumentException thrown
35+
= assertThrows(IllegalArgumentException.class, () -> new TypeReference() { });
36+
assertEquals("Type constructed without type information.", thrown.getMessage());
37+
}
38+
}

0 commit comments

Comments
 (0)