From 46d48c312b8f414498a6b9dd4b395d7017da5fc1 Mon Sep 17 00:00:00 2001 From: Shawn Yang Date: Sat, 13 Jul 2024 19:38:30 +0800 Subject: [PATCH] chore(java): merge reflect.Types into TypeRef (#1731) ## What does this PR do? This PR moves `org.apache.fury.reflect.Types` into TypeRef. Types is used in Fury type system, by merge `org.apache.fury.reflect.Types`, we can reduce ambiguation in fury type system ## Related issues #1553 #1690 ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark --- LICENSE | 1 - .../java/org/apache/fury/reflect/TypeRef.java | 402 ++++++++++++++++-- .../java/org/apache/fury/reflect/Types.java | 365 ---------------- .../src/main/resources/META-INF/LICENSE | 1 - .../fury-core/native-image.properties | 6 +- 5 files changed, 370 insertions(+), 405 deletions(-) delete mode 100644 java/fury-core/src/main/java/org/apache/fury/reflect/Types.java diff --git a/LICENSE b/LICENSE index a5d943cea3..7683b460e2 100644 --- a/LICENSE +++ b/LICENSE @@ -218,7 +218,6 @@ The text of each license is the standard Apache 2.0 license. Files: java/fury-core/src/main/java/org/apache/fury/util/Preconditions.java java/fury-core/src/main/java/org/apache/fury/reflect/TypeParameter.java - java/fury-core/src/main/java/org/apache/fury/reflect/Types.java java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java java/fury-core/src/main/java/org/apache/fury/util/concurrency/DirectExecutorService.java diff --git a/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java b/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java index 3707f15a6f..168698793f 100644 --- a/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java +++ b/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java @@ -14,12 +14,11 @@ package org.apache.fury.reflect; -import static org.apache.fury.reflect.Types.asTypeVariableKeyOrNull; -import static org.apache.fury.reflect.Types.newArrayType; -import static org.apache.fury.reflect.Types.typeVariablesEquals; - +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedType; import java.lang.reflect.Array; import java.lang.reflect.GenericArrayType; +import java.lang.reflect.GenericDeclaration; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -31,17 +30,18 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.annotation.CheckForNull; import org.apache.fury.type.TypeUtils; // Mostly derived from Guava 32.1.2 com.google.common.reflect.TypeToken // https://github.com/google/guava/blob/9f6a3840/guava/src/com/google/common/reflect/TypeToken.java public class TypeRef { - private final Type type; private transient Class rawType; - private transient Map typeMappings; + private transient Map typeMappings; /** * Constructs a new type token of {@code T}. @@ -217,15 +217,14 @@ public TypeRef resolveType(Type iteratorReturnType) { return of(iteratorReturnType); } Type invariantContext = WildcardCapturer.capture(type); - Map mappings = resolveTypeMappings(invariantContext); + Map mappings = resolveTypeMappings(invariantContext); return resolveType0(iteratorReturnType, mappings); } - private TypeRef resolveType0( - Type iteratorReturnType, Map mappings) { + private TypeRef resolveType0(Type iteratorReturnType, Map mappings) { if (iteratorReturnType instanceof TypeVariable) { TypeVariable typeVariable = (TypeVariable) iteratorReturnType; - Type type = mappings.get(new Types.TypeVariableKey(typeVariable)); + Type type = mappings.get(new TypeVariableKey(typeVariable)); if (type == null) { return of(typeVariable); } @@ -243,7 +242,7 @@ private TypeRef resolveType0( resolvedArgs[i] = resolveType0(args[i], mappings).type; } - return of(new Types.ParameterizedTypeImpl(resolvedOwner, resolvedRawType, resolvedArgs)); + return of(new ParameterizedTypeImpl(resolvedOwner, resolvedRawType, resolvedArgs)); } else if (iteratorReturnType instanceof GenericArrayType) { Type componentType = ((GenericArrayType) iteratorReturnType).getGenericComponentType(); Type resolvedComponentType = resolveType0(componentType, mappings).type; @@ -252,23 +251,23 @@ private TypeRef resolveType0( return of(iteratorReturnType); } - private Map resolveTypeMappings() { - Map cachedMappings = this.typeMappings; + private Map resolveTypeMappings() { + Map cachedMappings = this.typeMappings; if (cachedMappings != null) { return cachedMappings; } - Map typeMappings = resolveTypeMappings(type); + Map typeMappings = resolveTypeMappings(type); this.typeMappings = typeMappings; return typeMappings; } - private static Map resolveTypeMappings(Type contextType) { - Map result = new HashMap<>(); + private static Map resolveTypeMappings(Type contextType) { + Map result = new HashMap<>(); populateTypeMapping(result, contextType); return result; } - private static void populateTypeMapping(Map storage, Type... types) { + private static void populateTypeMapping(Map storage, Type... types) { for (Type type : types) { if (type == null) { continue; @@ -287,7 +286,7 @@ private static void populateTypeMapping(Map storage storageFiller: for (int i = 0; i < vars.length; i++) { TypeVariable typeVariable = vars[i]; - Types.TypeVariableKey key = new Types.TypeVariableKey(typeVariable); + TypeVariableKey key = new TypeVariableKey(typeVariable); if (storage.containsKey(key)) { continue; } @@ -354,7 +353,7 @@ public TypeRef getSupertype(Class superclass) { return of(newArrayType(componentSupertype.type)); } - Map mappings = resolveTypeMappings(); + Map mappings = resolveTypeMappings(); @SuppressWarnings("unchecked") // resolved supertype TypeRef supertype = (TypeRef) resolveType0(toGenericType(superclass), mappings); @@ -363,7 +362,7 @@ public TypeRef getSupertype(Class superclass) { private static Type toGenericType(Class cls) { if (cls.isArray()) { - return Types.newArrayType( + return newArrayType( // If we are passed with int[].class, don't turn it to GenericArrayType toGenericType(cls.getComponentType())); } @@ -373,7 +372,7 @@ private static Type toGenericType(Class cls) { ? toGenericType(cls.getEnclosingClass()) : null; if (typeParams.length > 0 || (ownerType != null && ownerType != cls.getEnclosingClass())) { - return new Types.ParameterizedTypeImpl(ownerType, cls, typeParams); + return new ParameterizedTypeImpl(ownerType, cls, typeParams); } else { return cls; } @@ -417,7 +416,7 @@ public final TypeRef getSubtype(Class subclass) { // requireNonNull is safe because we call getArraySubtype only when isArray(). TypeRef componentSubtype = of(componentType).getSubtype(subclassComponentType); // If we are passed with int[].class, don't turn it to GenericArrayType - return of(Types.newArrayType(componentSubtype.type)); + return of(newArrayType(componentSubtype.type)); } Class rawType = getRawType(); @@ -454,17 +453,17 @@ public final TypeRef getSubtype(Class subclass) { return result; } - Map mappings = new HashMap<>(); + Map mappings = new HashMap<>(); populateTypeMappings(mappings, supertypeWithArgsFromSubtype, type); return (TypeRef) resolveType0(genericSubtype.type, mappings); } private void populateTypeMappings( - Map mappings, Type supertypeWithArgsFromSubtype, Type toType) { + Map mappings, Type supertypeWithArgsFromSubtype, Type toType) { if (supertypeWithArgsFromSubtype instanceof TypeVariable) { - Types.TypeVariableKey typeVariableKey = - new Types.TypeVariableKey((TypeVariable) supertypeWithArgsFromSubtype); + TypeVariableKey typeVariableKey = + new TypeVariableKey((TypeVariable) supertypeWithArgsFromSubtype); mappings.put(typeVariableKey, toType); } else if (supertypeWithArgsFromSubtype instanceof WildcardType) { if (!(toType instanceof WildcardType)) { @@ -555,7 +554,7 @@ public final boolean isSubtypeOf(Type supertype) { for (int i = 0; i < typeParameters.length; i++) { TypeVariable typeParameter = typeParameters[i]; - Map mappings = resolveTypeMappings(); + Map mappings = resolveTypeMappings(); TypeRef subtypeParam = resolveType0(typeParameter, mappings); if (!subtypeParam.is(supertypeArgs[i], typeParameter)) { @@ -619,7 +618,7 @@ private Stream> getGenericInterfaces() { if (type instanceof WildcardType) { return boundsAsInterfaces(((WildcardType) type).getUpperBounds()); } - Map mappings = resolveTypeMappings(); + Map mappings = resolveTypeMappings(); return Arrays.stream(getRawType().getGenericInterfaces()) .map( interfaceType -> { @@ -644,7 +643,7 @@ private TypeRef getGenericSuperclass() { return null; } - Map mappings = resolveTypeMappings(); + Map mappings = resolveTypeMappings(); @SuppressWarnings("unchecked") // interface of T TypeRef superToken = (TypeRef) resolveType0(superclass, mappings); return superToken; @@ -705,7 +704,7 @@ private static WildcardType canonicalizeWildcardType( } upperBounds.add(canonicalizeWildcardsInType(bound)); } - return new Types.WildcardTypeImpl(type.getLowerBounds(), upperBounds.toArray(new Type[0])); + return new WildcardTypeImpl(type.getLowerBounds(), upperBounds.toArray(new Type[0])); } private static Type canonicalizeWildcardsInType(Type type) { @@ -721,10 +720,10 @@ private static Type canonicalizeWildcardsInType(Type type) { ? canonicalizeWildcardType(typeVars[i], (WildcardType) typeArg) : canonicalizeWildcardsInType(typeArg); } - return new Types.ParameterizedTypeImpl(parameterizedType.getOwnerType(), rawType, typeArgs); + return new ParameterizedTypeImpl(parameterizedType.getOwnerType(), rawType, typeArgs); } if (type instanceof GenericArrayType) { - return Types.newArrayType( + return newArrayType( canonicalizeWildcardsInType(((GenericArrayType) type).getGenericComponentType())); } return type; @@ -793,7 +792,7 @@ public final TypeRef where(TypeParameter typeParam, TypeRef typeArg if (type instanceof WildcardType) { // fast path return of(type); } - Types.TypeVariableKey typeVariableKey = new Types.TypeVariableKey(typeParam.typeVariable); + TypeVariableKey typeVariableKey = new TypeVariableKey(typeParam.typeVariable); @SuppressWarnings("unchecked") TypeRef result = (TypeRef) resolveType0(type, Collections.singletonMap(typeVariableKey, typeArg.type)); @@ -861,7 +860,7 @@ final Type capture0(Type type) { typeArgs[i] = forTypeVariable(typeVars[i]).capture0(typeArgs[i]); } Type ownerType = parameterizedType.getOwnerType(); - return new Types.ParameterizedTypeImpl( + return new ParameterizedTypeImpl( ownerType == null ? null : capture0(ownerType), rawType, typeArgs); } if (type instanceof WildcardType) { @@ -878,7 +877,7 @@ TypeVariable captureAsTypeVariable(Type[] upperBounds) { String name = "capture of ? extends " + Stream.of(upperBounds).map(Type::toString).collect(Collectors.joining("&")); - return new Types.TypeVariableImpl<>(WildcardCapturer.class, name, upperBounds); + return new TypeVariableImpl<>(WildcardCapturer.class, name, upperBounds); } private WildcardCapturer forTypeVariable(TypeVariable typeParam) { @@ -930,4 +929,337 @@ TypeVariable captureAsTypeVariable(Type[] upperBounds) { }; } } + + private enum ClassOwnership { + OWNED_BY_ENCLOSING_CLASS { + @Override + Class getOwnerType(Class rawType) { + return rawType.getEnclosingClass(); + } + }, + LOCAL_CLASS_HAS_NO_OWNER { + @Override + Class getOwnerType(Class rawType) { + return rawType.isLocalClass() ? null : rawType.getEnclosingClass(); + } + }; + + abstract Class getOwnerType(Class rawType); + + static final ClassOwnership JVM_BEHAVIOR = detectJvmBehaviour(); + + private static ClassOwnership detectJvmBehaviour() { + class LocalClass {} + + LocalClass localClassInstance = new LocalClass() {}; + Class subclass = localClassInstance.getClass(); + ParameterizedType parameterizedType = (ParameterizedType) subclass.getGenericSuperclass(); + for (ClassOwnership behavior : ClassOwnership.values()) { + if (behavior.getOwnerType(LocalClass.class) == parameterizedType.getOwnerType()) { + return behavior; + } + } + throw new AssertionError(); + } + } + + static class ParameterizedTypeImpl implements ParameterizedType { + private final Type[] actualTypeArguments; + private final Type rawType; + private final Type ownerType; + + public ParameterizedTypeImpl(Type ownerType, Type rawType, Type[] actualTypeArguments) { + if (ownerType == null) { + this.ownerType = getOwnerTypeFromRawType((Class) rawType); + } else { + this.ownerType = ownerType; + } + this.rawType = rawType; + this.actualTypeArguments = actualTypeArguments; + } + + @Override + public Type getOwnerType() { + return ownerType; + } + + @Override + public Type getRawType() { + return rawType; + } + + @Override + public Type[] getActualTypeArguments() { + return actualTypeArguments; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof ParameterizedType)) { + return false; + } + + ParameterizedType that = (ParameterizedType) o; + + if (!Arrays.equals(actualTypeArguments, that.getActualTypeArguments())) { + return false; + } + if (!Objects.equals(rawType, that.getRawType())) { + return false; + } + return Objects.equals(ownerType, that.getOwnerType()); + } + + @Override + public int hashCode() { + int result = Arrays.hashCode(actualTypeArguments); + result = 31 * result + (rawType != null ? rawType.hashCode() : 0); + result = 31 * result + (ownerType != null ? ownerType.hashCode() : 0); + return result; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append(typeName(rawType)).append('<'); + int i = 0; + for (Type typeArgument : actualTypeArguments) { + if (i++ != 0) { + builder.append(", "); + } + builder.append(typeName(typeArgument)); + } + return builder.append('>').toString(); + } + } + + static class GenericArrayTypeImpl implements GenericArrayType { + private final Type genericComponentType; + + public GenericArrayTypeImpl(Type genericComponentType) { + this.genericComponentType = genericComponentType; + } + + @Override + public Type getGenericComponentType() { + return genericComponentType; + } + + @Override + public String toString() { + return resolveTypeName(genericComponentType) + "[]"; + } + + @Override + public int hashCode() { + return genericComponentType.hashCode(); + } + + @Override + public boolean equals(@CheckForNull Object obj) { + if (obj instanceof GenericArrayType) { + GenericArrayType that = (GenericArrayType) obj; + return Objects.equals(getGenericComponentType(), that.getGenericComponentType()); + } + return false; + } + } + + static class WildcardTypeImpl implements WildcardType { + private final Type[] upperBounds; + private final Type[] lowerBounds; + + public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { + this.upperBounds = upperBounds; + this.lowerBounds = lowerBounds; + } + + @Override + public Type[] getUpperBounds() { + return upperBounds; + } + + @Override + public Type[] getLowerBounds() { + return lowerBounds; + } + + @Override + public boolean equals(@CheckForNull Object obj) { + if (obj instanceof WildcardType) { + WildcardType that = (WildcardType) obj; + return Arrays.equals(lowerBounds, that.getLowerBounds()) + && Arrays.equals(upperBounds, that.getUpperBounds()); + } + return false; + } + + @Override + public int hashCode() { + return Arrays.hashCode(lowerBounds) ^ Arrays.hashCode(upperBounds); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("?"); + for (Type lowerBound : lowerBounds) { + builder.append(" super ").append(resolveTypeName(lowerBound)); + } + for (Type upperBound : upperBounds) { + if (!upperBound.equals(Object.class)) { + builder.append(" extends ").append(resolveTypeName(upperBound)); + } + } + return builder.toString(); + } + } + + static class TypeVariableImpl implements TypeVariable { + private final D genericDeclaration; + private final String name; + private final Type[] upperBounds; + + TypeVariableImpl(D genericDeclaration, String name, Type[] upperBounds) { + this.genericDeclaration = genericDeclaration; + this.name = name; + this.upperBounds = upperBounds; + } + + @Override + public Type[] getBounds() { + return upperBounds; + } + + @Override + public D getGenericDeclaration() { + return genericDeclaration; + } + + @Override + public String getName() { + return name; + } + + @Override + public AnnotatedType[] getAnnotatedBounds() { + return new AnnotatedType[0]; + } + + @Override + public T getAnnotation(Class annotationClass) { + return null; + } + + @Override + public Annotation[] getAnnotations() { + return new Annotation[0]; + } + + @Override + public Annotation[] getDeclaredAnnotations() { + return new Annotation[0]; + } + + @Override + public String toString() { + return name; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof TypeVariable)) { + return false; + } + + TypeVariable that = (TypeVariable) o; + return Objects.equals(genericDeclaration, that.getGenericDeclaration()) + && Objects.equals(name, that.getName()) + && Arrays.equals(upperBounds, that.getBounds()); + } + + @Override + public int hashCode() { + int result = genericDeclaration != null ? genericDeclaration.hashCode() : 0; + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + Arrays.hashCode(upperBounds); + return result; + } + } + + static class TypeVariableKey { + private final TypeVariable typeVariable; + + public TypeVariableKey(TypeVariable typeVariable) { + this.typeVariable = typeVariable; + } + + @Override + public boolean equals(Object o) { + return o instanceof TypeVariableKey + && typeVariablesEquals(typeVariable, ((TypeVariableKey) o).typeVariable); + } + + @Override + public int hashCode() { + Annotation[] declaredAnnotations = typeVariable.getDeclaredAnnotations(); + String name = typeVariable.getName(); + + int result = 1; + result = + 31 * result + (declaredAnnotations != null ? Arrays.hashCode(declaredAnnotations) : 0); + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + } + + static Type newArrayType(Type componentType) { + if (componentType instanceof WildcardType) { + WildcardType wildcard = (WildcardType) componentType; + Type[] lowerBounds = wildcard.getLowerBounds(); + if (lowerBounds.length == 1) { + return new WildcardTypeImpl(new Type[] {lowerBounds[0]}, new Type[] {Object.class}); + } else { + Type[] upperBounds = wildcard.getUpperBounds(); + return new WildcardTypeImpl(new Type[0], new Type[] {upperBounds[0]}); + } + } + return componentType instanceof Class + ? Array.newInstance((Class) componentType, 0).getClass() + : new GenericArrayTypeImpl(componentType); + } + + static boolean typeVariablesEquals(TypeVariable a, Object bobj) { + if (bobj instanceof TypeVariable) { + TypeVariable b = (TypeVariable) bobj; + return a.getGenericDeclaration().equals(b.getGenericDeclaration()) + && a.getName().equals(b.getName()); + } else { + return false; + } + } + + static TypeVariableKey asTypeVariableKeyOrNull(Type t) { + if (t instanceof TypeVariable) { + return new TypeVariableKey((TypeVariable) t); + } + return null; + } + + private static Type getOwnerTypeFromRawType(Class rawType) { + return ClassOwnership.JVM_BEHAVIOR.getOwnerType(rawType); + } + + private static String resolveTypeName(Type type) { + return type instanceof Class ? ((Class) type).getName() : type.toString(); + } + + static String typeName(Type type) { + return (type instanceof Class) ? ((Class) type).getName() : type.toString(); + } } diff --git a/java/fury-core/src/main/java/org/apache/fury/reflect/Types.java b/java/fury-core/src/main/java/org/apache/fury/reflect/Types.java deleted file mode 100644 index 5bedac0e05..0000000000 --- a/java/fury-core/src/main/java/org/apache/fury/reflect/Types.java +++ /dev/null @@ -1,365 +0,0 @@ -/* - * Copyright (C) 2007 The Guava Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ - -package org.apache.fury.reflect; - -import java.lang.annotation.Annotation; -import java.lang.reflect.AnnotatedType; -import java.lang.reflect.Array; -import java.lang.reflect.GenericArrayType; -import java.lang.reflect.GenericDeclaration; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; -import java.lang.reflect.WildcardType; -import java.util.Arrays; -import java.util.Objects; -import javax.annotation.CheckForNull; - -// Derived from Guava 32.1.2 com.google.common.reflect.Types -// https://github.com/google/guava/blob/9f6a3840/guava/src/com/google/common/reflect/Types.java -class Types { - public static Type newArrayType(Type componentType) { - if (componentType instanceof WildcardType) { - WildcardType wildcard = (WildcardType) componentType; - Type[] lowerBounds = wildcard.getLowerBounds(); - if (lowerBounds.length == 1) { - return new WildcardTypeImpl(new Type[] {lowerBounds[0]}, new Type[] {Object.class}); - } else { - Type[] upperBounds = wildcard.getUpperBounds(); - return new WildcardTypeImpl(new Type[0], new Type[] {upperBounds[0]}); - } - } - return componentType instanceof Class - ? Array.newInstance((Class) componentType, 0).getClass() - : new GenericArrayTypeImpl(componentType); - } - - public static boolean typeVariablesEquals(TypeVariable a, Object bobj) { - if (bobj instanceof TypeVariable) { - TypeVariable b = (TypeVariable) bobj; - return a.getGenericDeclaration().equals(b.getGenericDeclaration()) - && a.getName().equals(b.getName()); - } else { - return false; - } - } - - public static TypeVariableKey asTypeVariableKeyOrNull(Type t) { - if (t instanceof TypeVariable) { - return new TypeVariableKey((TypeVariable) t); - } - return null; - } - - private static Type getOwnerTypeFromRawType(Class rawType) { - return ClassOwnership.JVM_BEHAVIOR.getOwnerType(rawType); - } - - private static String resolveTypeName(Type type) { - return type instanceof Class ? ((Class) type).getName() : type.toString(); - } - - public static class ParameterizedTypeImpl implements ParameterizedType { - private final Type[] actualTypeArguments; - private final Type rawType; - private final Type ownerType; - - public ParameterizedTypeImpl(Type ownerType, Type rawType, Type[] actualTypeArguments) { - if (ownerType == null) { - this.ownerType = getOwnerTypeFromRawType((Class) rawType); - } else { - this.ownerType = ownerType; - } - this.rawType = rawType; - this.actualTypeArguments = actualTypeArguments; - } - - @Override - public Type getOwnerType() { - return ownerType; - } - - @Override - public Type getRawType() { - return rawType; - } - - @Override - public Type[] getActualTypeArguments() { - return actualTypeArguments; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof ParameterizedType)) { - return false; - } - - ParameterizedType that = (ParameterizedType) o; - - if (!Arrays.equals(actualTypeArguments, that.getActualTypeArguments())) { - return false; - } - if (!Objects.equals(rawType, that.getRawType())) { - return false; - } - return Objects.equals(ownerType, that.getOwnerType()); - } - - @Override - public int hashCode() { - int result = Arrays.hashCode(actualTypeArguments); - result = 31 * result + (rawType != null ? rawType.hashCode() : 0); - result = 31 * result + (ownerType != null ? ownerType.hashCode() : 0); - return result; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(typeName(rawType)).append('<'); - int i = 0; - for (Type typeArgument : actualTypeArguments) { - if (i++ != 0) { - builder.append(", "); - } - builder.append(typeName(typeArgument)); - } - return builder.append('>').toString(); - } - } - - static String typeName(Type type) { - return (type instanceof Class) ? ((Class) type).getName() : type.toString(); - } - - public static class GenericArrayTypeImpl implements GenericArrayType { - private final Type genericComponentType; - - public GenericArrayTypeImpl(Type genericComponentType) { - this.genericComponentType = genericComponentType; - } - - @Override - public Type getGenericComponentType() { - return genericComponentType; - } - - @Override - public String toString() { - return resolveTypeName(genericComponentType) + "[]"; - } - - @Override - public int hashCode() { - return genericComponentType.hashCode(); - } - - @Override - public boolean equals(@CheckForNull Object obj) { - if (obj instanceof GenericArrayType) { - GenericArrayType that = (GenericArrayType) obj; - return Objects.equals(getGenericComponentType(), that.getGenericComponentType()); - } - return false; - } - } - - public static class WildcardTypeImpl implements WildcardType { - private final Type[] upperBounds; - private final Type[] lowerBounds; - - public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { - this.upperBounds = upperBounds; - this.lowerBounds = lowerBounds; - } - - @Override - public Type[] getUpperBounds() { - return upperBounds; - } - - @Override - public Type[] getLowerBounds() { - return lowerBounds; - } - - @Override - public boolean equals(@CheckForNull Object obj) { - if (obj instanceof WildcardType) { - WildcardType that = (WildcardType) obj; - return Arrays.equals(lowerBounds, that.getLowerBounds()) - && Arrays.equals(upperBounds, that.getUpperBounds()); - } - return false; - } - - @Override - public int hashCode() { - return Arrays.hashCode(lowerBounds) ^ Arrays.hashCode(upperBounds); - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder("?"); - for (Type lowerBound : lowerBounds) { - builder.append(" super ").append(resolveTypeName(lowerBound)); - } - for (Type upperBound : upperBounds) { - if (!upperBound.equals(Object.class)) { - builder.append(" extends ").append(resolveTypeName(upperBound)); - } - } - return builder.toString(); - } - } - - public static class TypeVariableImpl implements TypeVariable { - private final D genericDeclaration; - private final String name; - private final Type[] upperBounds; - - TypeVariableImpl(D genericDeclaration, String name, Type[] upperBounds) { - this.genericDeclaration = genericDeclaration; - this.name = name; - this.upperBounds = upperBounds; - } - - @Override - public Type[] getBounds() { - return upperBounds; - } - - @Override - public D getGenericDeclaration() { - return genericDeclaration; - } - - @Override - public String getName() { - return name; - } - - @Override - public AnnotatedType[] getAnnotatedBounds() { - return new AnnotatedType[0]; - } - - @Override - public T getAnnotation(Class annotationClass) { - return null; - } - - @Override - public Annotation[] getAnnotations() { - return new Annotation[0]; - } - - @Override - public Annotation[] getDeclaredAnnotations() { - return new Annotation[0]; - } - - @Override - public String toString() { - return name; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof TypeVariable)) { - return false; - } - - TypeVariable that = (TypeVariable) o; - return Objects.equals(genericDeclaration, that.getGenericDeclaration()) - && Objects.equals(name, that.getName()) - && Arrays.equals(upperBounds, that.getBounds()); - } - - @Override - public int hashCode() { - int result = genericDeclaration != null ? genericDeclaration.hashCode() : 0; - result = 31 * result + (name != null ? name.hashCode() : 0); - result = 31 * result + Arrays.hashCode(upperBounds); - return result; - } - } - - static class TypeVariableKey { - private final TypeVariable typeVariable; - - public TypeVariableKey(TypeVariable typeVariable) { - this.typeVariable = typeVariable; - } - - @Override - public boolean equals(Object o) { - return o instanceof TypeVariableKey - && Types.typeVariablesEquals(typeVariable, ((TypeVariableKey) o).typeVariable); - } - - @Override - public int hashCode() { - Annotation[] declaredAnnotations = typeVariable.getDeclaredAnnotations(); - String name = typeVariable.getName(); - - int result = 1; - result = - 31 * result + (declaredAnnotations != null ? Arrays.hashCode(declaredAnnotations) : 0); - result = 31 * result + (name != null ? name.hashCode() : 0); - return result; - } - } - - private enum ClassOwnership { - OWNED_BY_ENCLOSING_CLASS { - @Override - Class getOwnerType(Class rawType) { - return rawType.getEnclosingClass(); - } - }, - LOCAL_CLASS_HAS_NO_OWNER { - @Override - Class getOwnerType(Class rawType) { - return rawType.isLocalClass() ? null : rawType.getEnclosingClass(); - } - }; - - abstract Class getOwnerType(Class rawType); - - static final ClassOwnership JVM_BEHAVIOR = detectJvmBehaviour(); - - private static ClassOwnership detectJvmBehaviour() { - class LocalClass {} - - LocalClass localClassInstance = new LocalClass() {}; - Class subclass = localClassInstance.getClass(); - ParameterizedType parameterizedType = (ParameterizedType) subclass.getGenericSuperclass(); - for (ClassOwnership behavior : ClassOwnership.values()) { - if (behavior.getOwnerType(LocalClass.class) == parameterizedType.getOwnerType()) { - return behavior; - } - } - throw new AssertionError(); - } - } -} diff --git a/java/fury-core/src/main/resources/META-INF/LICENSE b/java/fury-core/src/main/resources/META-INF/LICENSE index 187a82391b..5ba1f74db4 100644 --- a/java/fury-core/src/main/resources/META-INF/LICENSE +++ b/java/fury-core/src/main/resources/META-INF/LICENSE @@ -218,7 +218,6 @@ The text of each license is the standard Apache 2.0 license. Files: java/fury-core/src/main/java/org/apache/fury/util/Preconditions.java java/fury-core/src/main/java/org/apache/fury/reflect/TypeParameter.java - java/fury-core/src/main/java/org/apache/fury/reflect/Types.java java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java java/fury-core/src/main/java/org/apache/fury/util/concurrency/DirectExecutorService.java diff --git a/java/fury-core/src/main/resources/META-INF/native-image/org.apache.fury/fury-core/native-image.properties b/java/fury-core/src/main/resources/META-INF/native-image/org.apache.fury/fury-core/native-image.properties index 3b6e47deb1..2af741d83d 100644 --- a/java/fury-core/src/main/resources/META-INF/native-image/org.apache.fury/fury-core/native-image.properties +++ b/java/fury-core/src/main/resources/META-INF/native-image/org.apache.fury/fury-core/native-image.properties @@ -169,8 +169,8 @@ Args=--initialize-at-build-time=org.apache.fury.memory.MemoryBuffer,\ org.apache.fury.meta.MetaStringEncoder$1,\ org.apache.fury.meta.MetaString$Encoding,\ org.apache.fury.meta.Encoders,\ - org.apache.fury.reflect.Types$ClassOwnership,\ - org.apache.fury.reflect.Types$ClassOwnership$1,\ - org.apache.fury.reflect.Types$ClassOwnership$2,\ + org.apache.fury.reflect.TypeRef$ClassOwnership,\ + org.apache.fury.reflect.TypeRef$ClassOwnership$1,\ + org.apache.fury.reflect.TypeRef$ClassOwnership$2,\ org.apache.fury.resolver.DisallowedList,\ org.apache.fury.util.StringUtils