Skip to content

Commit e745564

Browse files
authored
[ClientCore] Introduce Union type (Azure#43778)
1 parent 23c9409 commit e745564

File tree

11 files changed

+1285
-0
lines changed

11 files changed

+1285
-0
lines changed

sdk/clientcore/core/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<suppress files="io.clientcore.core.implementation.util.ImplUtils.java" checks="MissingJavadocTypeCheck" />
2020
<suppress files="io.clientcore.core.implementation.instrumentation.Slf4jLoggerShim.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
2121
<suppress files="io.clientcore.core.implementation.util.EnvironmentConfiguration.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
22+
<suppress files="io.clientcore.core.util.Union.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
2223
<suppress files="io.clientcore.core.implementation.ReflectionSerializable.java" checks="com.azure.tools.checkstyle.checks.JavadocThrowsChecks" />
2324
<suppress files="io.clientcore.core.implementation.http.serializer.HttpResponseBodyDecoder.java" checks="com.azure.tools.checkstyle.checks.JavadocThrowsChecks" />
2425
<suppress files="io.clientcore.core.http.client.DefaultHttpClientBuilder.java" checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" />

sdk/clientcore/core/spotbugs-exclude.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
<Class name="io.clientcore.core.implementation.util.XmlSerializer" />
3636
<Class name="io.clientcore.core.serialization.xml.XmlReader" />
3737
<Class name="io.clientcore.core.shared.HttpClientTests" />
38+
<Class name="io.clientcore.core.implementation.GenericParameterizedType" />
3839
<Class name="io.clientcore.core.util.SharedExecutorService" />
40+
<Class name="io.clientcore.core.util.Union" />
3941
<Class name="io.clientcore.core.util.binarydata.FileBinaryData" />
4042
<Class name="io.clientcore.core.util.binarydata.InputStreamBinaryData" />
4143
<Class name="io.clientcore.core.util.binarydata.ListByteBufferBinaryData" />
@@ -284,6 +286,10 @@
284286
<Bug pattern="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" />
285287
<Class name="io.clientcore.core.http.client.implementation.InputStreamTimeoutResponseSubscriber" />
286288
</Match>
289+
<Match>
290+
<Bug pattern="RV_RETURN_VALUE_IGNORED_INFERRED" />
291+
<Class name="io.clientcore.core.util.UnionTests" />
292+
</Match>
287293
<Match>
288294
<Bug pattern="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT" />
289295
<Class name="io.clientcore.core.serialization.xml.XmlReaderCodesnippetsTests" />
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package io.clientcore.core.implementation;
5+
6+
import io.clientcore.core.instrumentation.logging.ClientLogger;
7+
8+
import java.lang.reflect.ParameterizedType;
9+
import java.lang.reflect.Type;
10+
import java.util.Arrays;
11+
import java.util.Objects;
12+
import java.util.stream.Collectors;
13+
14+
/**
15+
* A {@link ParameterizedType} implementation that allows for reference type arguments.
16+
*/
17+
public final class GenericParameterizedType implements ParameterizedType {
18+
private static final ClientLogger LOGGER = new ClientLogger(GenericParameterizedType.class);
19+
20+
private final Class<?> raw;
21+
private final Type[] args;
22+
private String cachedToString;
23+
24+
/**
25+
* Creates a new instance of {@link GenericParameterizedType}.
26+
*
27+
* @param raw The raw type.
28+
* @param args The type arguments.
29+
*/
30+
public GenericParameterizedType(Class<?> raw, Type... args) {
31+
this.raw = raw;
32+
33+
if (args == null) {
34+
throw LOGGER.logThrowableAsError(new IllegalArgumentException("args cannot be null"));
35+
}
36+
37+
Type[] argsCopy = new Type[args.length];
38+
for (int i = 0; i < args.length; i++) {
39+
if (args[i] == null) {
40+
throw LOGGER.logThrowableAsError(
41+
new IllegalArgumentException("args cannot contain null: null value in index " + i));
42+
}
43+
argsCopy[i] = args[i];
44+
}
45+
this.args = argsCopy;
46+
}
47+
48+
@Override
49+
public Type[] getActualTypeArguments() {
50+
return args;
51+
}
52+
53+
@Override
54+
public Type getRawType() {
55+
return raw;
56+
}
57+
58+
@Override
59+
public Type getOwnerType() {
60+
return null;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
if (cachedToString == null) {
66+
cachedToString = raw.getTypeName() + "<"
67+
+ Arrays.stream(args).map(Type::getTypeName).collect(Collectors.joining(", ")) + ">";
68+
}
69+
return cachedToString;
70+
}
71+
72+
@Override
73+
public boolean equals(Object o) {
74+
if (this == o) {
75+
return true;
76+
}
77+
if (o == null || getClass() != o.getClass()) {
78+
return false;
79+
}
80+
GenericParameterizedType that = (GenericParameterizedType) o;
81+
return Objects.equals(raw, that.raw) && Objects.deepEquals(args, that.args);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return Objects.hash(raw, Arrays.hashCode(args));
87+
}
88+
}

0 commit comments

Comments
 (0)