Utilities for working with {@link Type}. @author Ben Yu
| 52 | * @author Ben Yu |
| 53 | */ |
| 54 | final class Types { |
| 55 | |
| 56 | /** Class#toString without the "class " and "interface " prefixes */ |
| 57 | private static final Joiner COMMA_JOINER = Joiner.on(", ").useForNull("null"); |
| 58 | |
| 59 | /** Returns the array type of {@code componentType}. */ |
| 60 | static Type newArrayType(Type componentType) { |
| 61 | if (componentType instanceof WildcardType) { |
| 62 | WildcardType wildcard = (WildcardType) componentType; |
| 63 | Type[] lowerBounds = wildcard.getLowerBounds(); |
| 64 | checkArgument(lowerBounds.length <= 1, "Wildcard cannot have more than one lower bounds."); |
| 65 | if (lowerBounds.length == 1) { |
| 66 | return supertypeOf(newArrayType(lowerBounds[0])); |
| 67 | } else { |
| 68 | Type[] upperBounds = wildcard.getUpperBounds(); |
| 69 | checkArgument(upperBounds.length == 1, "Wildcard should have only one upper bound."); |
| 70 | return subtypeOf(newArrayType(upperBounds[0])); |
| 71 | } |
| 72 | } |
| 73 | return JavaVersion.CURRENT.newArrayType(componentType); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns a type where {@code rawType} is parameterized by {@code arguments} and is owned by |
| 78 | * {@code ownerType}. |
| 79 | */ |
| 80 | static ParameterizedType newParameterizedTypeWithOwner( |
| 81 | @Nullable Type ownerType, Class<?> rawType, Type... arguments) { |
| 82 | if (ownerType == null) { |
| 83 | return newParameterizedType(rawType, arguments); |
| 84 | } |
| 85 | // ParameterizedTypeImpl constructor already checks, but we want to throw NPE before IAE |
| 86 | checkNotNull(arguments); |
| 87 | checkArgument(rawType.getEnclosingClass() != null, "Owner type for unenclosed %s", rawType); |
| 88 | return new ParameterizedTypeImpl(ownerType, rawType, arguments); |
| 89 | } |
| 90 | |
| 91 | /** Returns a type where {@code rawType} is parameterized by {@code arguments}. */ |
| 92 | static ParameterizedType newParameterizedType(Class<?> rawType, Type... arguments) { |
| 93 | return new ParameterizedTypeImpl( |
| 94 | ClassOwnership.JVM_BEHAVIOR.getOwnerType(rawType), rawType, arguments); |
| 95 | } |
| 96 | |
| 97 | /** Decides what owner type to use for constructing {@link ParameterizedType} from a raw class. */ |
| 98 | private enum ClassOwnership { |
| 99 | OWNED_BY_ENCLOSING_CLASS { |
| 100 | @Override |
| 101 | @Nullable Class<?> getOwnerType(Class<?> rawType) { |
| 102 | return rawType.getEnclosingClass(); |
| 103 | } |
| 104 | }, |
| 105 | LOCAL_CLASS_HAS_NO_OWNER { |
| 106 | @Override |
| 107 | @Nullable Class<?> getOwnerType(Class<?> rawType) { |
| 108 | if (rawType.isLocalClass()) { |
| 109 | return null; |
| 110 | } else { |
| 111 | return rawType.getEnclosingClass(); |
nothing calls this directly
no test coverage detected