Skip to content

Commit

Permalink
[ClientCore] Introduce Union type (#43778)
Browse files Browse the repository at this point in the history
  • Loading branch information
mssfang authored Jan 31, 2025
1 parent 23c9409 commit e745564
Show file tree
Hide file tree
Showing 11 changed files with 1,285 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/clientcore/core/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<suppress files="io.clientcore.core.implementation.util.ImplUtils.java" checks="MissingJavadocTypeCheck" />
<suppress files="io.clientcore.core.implementation.instrumentation.Slf4jLoggerShim.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
<suppress files="io.clientcore.core.implementation.util.EnvironmentConfiguration.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
<suppress files="io.clientcore.core.util.Union.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
<suppress files="io.clientcore.core.implementation.ReflectionSerializable.java" checks="com.azure.tools.checkstyle.checks.JavadocThrowsChecks" />
<suppress files="io.clientcore.core.implementation.http.serializer.HttpResponseBodyDecoder.java" checks="com.azure.tools.checkstyle.checks.JavadocThrowsChecks" />
<suppress files="io.clientcore.core.http.client.DefaultHttpClientBuilder.java" checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" />
Expand Down
6 changes: 6 additions & 0 deletions sdk/clientcore/core/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
<Class name="io.clientcore.core.implementation.util.XmlSerializer" />
<Class name="io.clientcore.core.serialization.xml.XmlReader" />
<Class name="io.clientcore.core.shared.HttpClientTests" />
<Class name="io.clientcore.core.implementation.GenericParameterizedType" />
<Class name="io.clientcore.core.util.SharedExecutorService" />
<Class name="io.clientcore.core.util.Union" />
<Class name="io.clientcore.core.util.binarydata.FileBinaryData" />
<Class name="io.clientcore.core.util.binarydata.InputStreamBinaryData" />
<Class name="io.clientcore.core.util.binarydata.ListByteBufferBinaryData" />
Expand Down Expand Up @@ -284,6 +286,10 @@
<Bug pattern="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" />
<Class name="io.clientcore.core.http.client.implementation.InputStreamTimeoutResponseSubscriber" />
</Match>
<Match>
<Bug pattern="RV_RETURN_VALUE_IGNORED_INFERRED" />
<Class name="io.clientcore.core.util.UnionTests" />
</Match>
<Match>
<Bug pattern="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT" />
<Class name="io.clientcore.core.serialization.xml.XmlReaderCodesnippetsTests" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package io.clientcore.core.implementation;

import io.clientcore.core.instrumentation.logging.ClientLogger;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* A {@link ParameterizedType} implementation that allows for reference type arguments.
*/
public final class GenericParameterizedType implements ParameterizedType {
private static final ClientLogger LOGGER = new ClientLogger(GenericParameterizedType.class);

private final Class<?> raw;
private final Type[] args;
private String cachedToString;

/**
* Creates a new instance of {@link GenericParameterizedType}.
*
* @param raw The raw type.
* @param args The type arguments.
*/
public GenericParameterizedType(Class<?> raw, Type... args) {
this.raw = raw;

if (args == null) {
throw LOGGER.logThrowableAsError(new IllegalArgumentException("args cannot be null"));
}

Type[] argsCopy = new Type[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i] == null) {
throw LOGGER.logThrowableAsError(
new IllegalArgumentException("args cannot contain null: null value in index " + i));
}
argsCopy[i] = args[i];
}
this.args = argsCopy;
}

@Override
public Type[] getActualTypeArguments() {
return args;
}

@Override
public Type getRawType() {
return raw;
}

@Override
public Type getOwnerType() {
return null;
}

@Override
public String toString() {
if (cachedToString == null) {
cachedToString = raw.getTypeName() + "<"
+ Arrays.stream(args).map(Type::getTypeName).collect(Collectors.joining(", ")) + ">";
}
return cachedToString;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GenericParameterizedType that = (GenericParameterizedType) o;
return Objects.equals(raw, that.raw) && Objects.deepEquals(args, that.args);
}

@Override
public int hashCode() {
return Objects.hash(raw, Arrays.hashCode(args));
}
}
Loading

0 comments on commit e745564

Please sign in to comment.