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