1 /**
2  * Copyright (C) 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 package com.google.inject.util;
19 
20 import com.google.inject.Provider;
21 import com.google.inject.internal.MoreTypes;
22 import com.google.inject.internal.MoreTypes.GenericArrayTypeImpl;
23 import com.google.inject.internal.MoreTypes.ParameterizedTypeImpl;
24 import com.google.inject.internal.MoreTypes.WildcardTypeImpl;
25 
26 import java.lang.reflect.GenericArrayType;
27 import java.lang.reflect.ParameterizedType;
28 import java.lang.reflect.Type;
29 import java.lang.reflect.WildcardType;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 
34 /**
35  * Static methods for working with types.
36  *
37  * @author crazybob@google.com (Bob Lee)
38  * @since 2.0
39  */
40 public final class Types {
Types()41   private Types() {}
42 
43   /**
44    * Returns a new parameterized type, applying {@code typeArguments} to
45    * {@code rawType}. The returned type does not have an owner type.
46    *
47    * @return a {@link java.io.Serializable serializable} parameterized type.
48    */
newParameterizedType(Type rawType, Type... typeArguments)49   public static ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) {
50     return newParameterizedTypeWithOwner(null, rawType, typeArguments);
51   }
52 
53   /**
54    * Returns a new parameterized type, applying {@code typeArguments} to
55    * {@code rawType} and enclosed by {@code ownerType}.
56    *
57    * @return a {@link java.io.Serializable serializable} parameterized type.
58    */
newParameterizedTypeWithOwner( Type ownerType, Type rawType, Type... typeArguments)59   public static ParameterizedType newParameterizedTypeWithOwner(
60       Type ownerType, Type rawType, Type... typeArguments) {
61     return new ParameterizedTypeImpl(ownerType, rawType, typeArguments);
62   }
63 
64   /**
65    * Returns an array type whose elements are all instances of
66    * {@code componentType}.
67    *
68    * @return a {@link java.io.Serializable serializable} generic array type.
69    */
arrayOf(Type componentType)70   public static GenericArrayType arrayOf(Type componentType) {
71     return new GenericArrayTypeImpl(componentType);
72   }
73 
74   /**
75    * Returns a type that represents an unknown type that extends {@code bound}.
76    * For example, if {@code bound} is {@code CharSequence.class}, this returns
77    * {@code ? extends CharSequence}. If {@code bound} is {@code Object.class},
78    * this returns {@code ?}, which is shorthand for {@code ? extends Object}.
79    */
subtypeOf(Type bound)80   public static WildcardType subtypeOf(Type bound) {
81     return new WildcardTypeImpl(new Type[] { bound }, MoreTypes.EMPTY_TYPE_ARRAY);
82   }
83 
84   /**
85    * Returns a type that represents an unknown supertype of {@code bound}. For
86    * example, if {@code bound} is {@code String.class}, this returns {@code ?
87    * super String}.
88    */
supertypeOf(Type bound)89   public static WildcardType supertypeOf(Type bound) {
90     return new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { bound });
91   }
92 
93   /**
94    * Returns a type modelling a {@link List} whose elements are of type
95    * {@code elementType}.
96    *
97    * @return a {@link java.io.Serializable serializable} parameterized type.
98    */
listOf(Type elementType)99   public static ParameterizedType listOf(Type elementType) {
100     return newParameterizedType(List.class, elementType);
101   }
102 
103   /**
104    * Returns a type modelling a {@link Set} whose elements are of type
105    * {@code elementType}.
106    *
107    * @return a {@link java.io.Serializable serializable} parameterized type.
108    */
setOf(Type elementType)109   public static ParameterizedType setOf(Type elementType) {
110     return newParameterizedType(Set.class, elementType);
111   }
112 
113   /**
114    * Returns a type modelling a {@link Map} whose keys are of type
115    * {@code keyType} and whose values are of type {@code valueType}.
116    *
117    * @return a {@link java.io.Serializable serializable} parameterized type.
118    */
mapOf(Type keyType, Type valueType)119   public static ParameterizedType mapOf(Type keyType, Type valueType) {
120     return newParameterizedType(Map.class, keyType, valueType);
121   }
122 
123   // for other custom collections types, use newParameterizedType()
124 
125   /**
126    * Returns a type modelling a {@link Provider} that provides elements of type
127    * {@code elementType}.
128    *
129    * @return a {@link java.io.Serializable serializable} parameterized type.
130    */
providerOf(Type providedType)131   public static ParameterizedType providerOf(Type providedType) {
132     return newParameterizedType(Provider.class, providedType);
133   }
134 }