cleaning up azure-core-serde-jackson module

This commit is contained in:
Anu Thomas Chandy 2021-02-05 17:18:01 -08:00
Родитель a44254210d
Коммит ec7540b11f
3 изменённых файлов: 0 добавлений и 234 удалений

Просмотреть файл

@ -1,35 +0,0 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
//package com.azure.core.serde.jackson;
//
//import com.azure.core.http.HttpHeaders;
//import com.fasterxml.jackson.core.JsonGenerator;
//import com.fasterxml.jackson.databind.JsonSerializer;
//import com.fasterxml.jackson.databind.SerializerProvider;
//import com.fasterxml.jackson.databind.module.SimpleModule;
//
//import java.io.IOException;
//
///**
// * Custom serializer for serializing {@code HttpHeaders} objects into Base64 strings.
// */
//final class HttpHeadersSerializer extends JsonSerializer<HttpHeaders> {
// /**
// * Gets a module wrapping this serializer as an adapter for the Jackson
// * ObjectMapper.
// *
// * @return a simple module to be plugged onto Jackson ObjectMapper.
// */
// public static SimpleModule getModule() {
// SimpleModule module = new SimpleModule();
// module.addSerializer(HttpHeaders.class, new HttpHeadersSerializer());
// return module;
// }
//
// @Override
// public void serialize(HttpHeaders value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
// jgen.writeObject(value.toMap());
// }
//}

Просмотреть файл

@ -1,71 +0,0 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.core.serde.jackson;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* This class represents a generic Java type, retaining information about generics.
*
* @param <T> The type being represented.
*/
public abstract class TypeReference<T> {
// private static final ClientLogger LOGGER = new ClientLogger(TypeReference.class);
private static final String MISSING_TYPE = "Type constructed without type information.";
private static final Map<Class<?>, TypeReference<?>> CACHE = new ConcurrentHashMap<>();
private final Type javaType;
/**
* Constructs a new {@link TypeReference} which maintains generic information.
*
* @throws IllegalArgumentException If the reference is constructed without type information.
*/
public TypeReference() {
Type superClass = this.getClass().getGenericSuperclass();
if (superClass instanceof Class) {
throw new IllegalArgumentException(MISSING_TYPE);
// throw LOGGER.logExceptionAsError(new IllegalArgumentException(MISSING_TYPE));
} else {
this.javaType = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}
}
private TypeReference(Class<T> clazz) {
this.javaType = clazz;
}
/**
* Returns the {@link Type} representing {@code T}.
*
* @return The {@link Type} representing {@code T}.
*/
public Type getJavaType() {
return javaType;
}
/**
* Creates and instance of {@link TypeReference} which maintains the generic {@code T} of the passed {@link Class}.
* <p>
* This method will cache the instance of {@link TypeReference} using the passed {@link Class} as the key. This is
* meant to be used with non-generic types such as primitive object types and POJOs, not {@code Map<String, Object>}
* or {@code List<Integer>} parameterized types.
*
* @param clazz {@link Class} that contains generic information used to create the {@link TypeReference}.
* @param <T> The generic type.
* @return Either the cached or new instance of {@link TypeReference}.
*/
@SuppressWarnings("unchecked")
public static <T> TypeReference<T> createInstance(Class<T> clazz) {
/*
* When computing the TypeReference if the key is absent ignore the parameter from the compute function. The
* compute function wildcards to T type which causes the type system to breakdown.
*/
return (TypeReference<T>) CACHE.computeIfAbsent(clazz, c -> new TypeReference<T>(clazz) { });
}
}

Просмотреть файл

@ -3,20 +3,15 @@
package com.azure.core.serde.jackson;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Utility type exposing methods to deal with {@link Type}.
*/
public final class TypeUtil {
private static final Map<Type, Type> SUPER_TYPE_MAP = new ConcurrentHashMap<>();
/**
* Find all super classes including provided class.
*
@ -45,19 +40,6 @@ public final class TypeUtil {
return ((ParameterizedType) type).getActualTypeArguments();
}
/**
* Get the generic argument, or the first if the type has more than one.
*
* @param type the type to get arguments
* @return the generic argument, null if type is not parameterized
*/
public static Type getTypeArgument(Type type) {
if (!(type instanceof ParameterizedType)) {
return null;
}
return ((ParameterizedType) type).getActualTypeArguments()[0];
}
/**
* Get the raw class for a given type.
*
@ -73,62 +55,6 @@ public final class TypeUtil {
}
}
/**
* Get the super type for a given type.
*
* @param type the input type
* @return the direct super type
*/
public static Type getSuperType(final Type type) {
return SUPER_TYPE_MAP.computeIfAbsent(type, _type -> {
if (type instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) type;
final Type genericSuperClass = ((Class<?>) parameterizedType.getRawType()).getGenericSuperclass();
if (genericSuperClass instanceof ParameterizedType) {
/*
* Find erased generic types for the super class and replace
* with actual type arguments from the parameterized type
*/
final Type[] superTypeArguments = getTypeArguments(genericSuperClass);
final Type[] typeParameters =
((GenericDeclaration) parameterizedType.getRawType()).getTypeParameters();
int k = 0;
for (int i = 0; i != superTypeArguments.length; i++) {
for (int j = 0; i < typeParameters.length; j++) {
if (typeParameters[j].equals(superTypeArguments[i])) {
superTypeArguments[i] = parameterizedType.getActualTypeArguments()[k++];
break;
}
}
}
return createParameterizedType(((ParameterizedType) genericSuperClass).getRawType(),
superTypeArguments);
} else {
return genericSuperClass;
}
} else {
return ((Class<?>) type).getGenericSuperclass();
}
});
}
/**
* Get the super type for a type in its super type chain, which has
* a raw class that matches the specified class.
*
* @param subType the sub type to find super type for
* @param rawSuperType the raw class for the super type
* @return the super type that matches the requirement
*/
public static Type getSuperType(Type subType, Class<?> rawSuperType) {
while (subType != null && getRawClass(subType) != rawSuperType) {
subType = getSuperType(subType);
}
return subType;
}
/**
* Determines if a type is the same or a subtype for another type.
*
@ -140,60 +66,6 @@ public final class TypeUtil {
return getRawClass(superType).isAssignableFrom(getRawClass(subType));
}
/**
* Create a parameterized type from a raw class and its type arguments.
*
* @param rawClass the raw class to construct the parameterized type
* @param genericTypes the generic arguments
* @return the parameterized type
*/
public static ParameterizedType createParameterizedType(Type rawClass, Type... genericTypes) {
return new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return genericTypes;
}
@Override
public Type getRawType() {
return rawClass;
}
@Override
public Type getOwnerType() {
return null;
}
};
}
/**
* Returns whether the rest response expects to have any body (by checking if the body parameter type is set to
* Void, in which case no body is expected).
*
* @param restResponseReturnType The RestResponse subtype containing the type arguments we are inspecting.
* @return True if a body is expected, false if a Void body is expected.
*/
public static boolean restResponseTypeExpectsBody(ParameterizedType restResponseReturnType) {
return getRestResponseBodyType(restResponseReturnType) != Void.class;
}
/**
* Returns the body type expected in the rest response.
*
* @param restResponseReturnType The RestResponse subtype containing the type arguments we are inspecting.
* @return The type of the body.
*/
public static Type getRestResponseBodyType(Type restResponseReturnType) {
// if this type has type arguments, then we look at the last one to determine if it expects a body
final Type[] restResponseTypeArguments = TypeUtil.getTypeArguments(restResponseReturnType);
if (restResponseTypeArguments != null && restResponseTypeArguments.length > 0) {
return restResponseTypeArguments[restResponseTypeArguments.length - 1];
} else {
// no generic type on this RestResponse sub-type, so we go up to parent
return getRestResponseBodyType(TypeUtil.getSuperType(restResponseReturnType));
}
}
// Private Ctr
private TypeUtil() {
}