/*
 * Copyright (C) 2008 Google Inc.
 *
 * 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 com.google.inject.util;

import com.google.inject.Provider;
import com.google.inject.internal.MoreTypes;
import com.google.inject.internal.MoreTypes.GenericArrayTypeImpl;
import com.google.inject.internal.MoreTypes.ParameterizedTypeImpl;
import com.google.inject.internal.MoreTypes.WildcardTypeImpl;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

Static methods for working with types.
Author:crazybob@google.com (Bob Lee)
Since:2.0
/** * Static methods for working with types. * * @author crazybob@google.com (Bob Lee) * @since 2.0 */
public final class Types { private Types() {}
Returns a new parameterized type, applying typeArguments to rawType. The returned type does not have an owner type.
Returns:a serializable parameterized type.
/** * Returns a new parameterized type, applying {@code typeArguments} to {@code rawType}. The * returned type does not have an owner type. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) { return newParameterizedTypeWithOwner(null, rawType, typeArguments); }
Returns a new parameterized type, applying typeArguments to rawType and enclosed by ownerType.
Returns:a serializable parameterized type.
/** * Returns a new parameterized type, applying {@code typeArguments} to {@code rawType} and * enclosed by {@code ownerType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static ParameterizedType newParameterizedTypeWithOwner( Type ownerType, Type rawType, Type... typeArguments) { return new ParameterizedTypeImpl(ownerType, rawType, typeArguments); }
Returns an array type whose elements are all instances of componentType.
Returns:a serializable generic array type.
/** * Returns an array type whose elements are all instances of {@code componentType}. * * @return a {@link java.io.Serializable serializable} generic array type. */
public static GenericArrayType arrayOf(Type componentType) { return new GenericArrayTypeImpl(componentType); }
Returns a type that represents an unknown type that extends bound. For example, if bound is CharSequence.class, this returns ? extends CharSequence. If bound is Object.class, this returns ?, which is shorthand for ? extends Object.
/** * Returns a type that represents an unknown type that extends {@code bound}. For example, if * {@code bound} is {@code CharSequence.class}, this returns {@code ? extends CharSequence}. If * {@code bound} is {@code Object.class}, this returns {@code ?}, which is shorthand for {@code ? * extends Object}. */
public static WildcardType subtypeOf(Type bound) { return new WildcardTypeImpl(new Type[] {bound}, MoreTypes.EMPTY_TYPE_ARRAY); }
Returns a type that represents an unknown supertype of bound. For example, if bound is String.class, this returns ? super String.
/** * Returns a type that represents an unknown supertype of {@code bound}. For example, if {@code * bound} is {@code String.class}, this returns {@code ? super String}. */
public static WildcardType supertypeOf(Type bound) { return new WildcardTypeImpl(new Type[] {Object.class}, new Type[] {bound}); }
Returns a type modelling a List whose elements are of type elementType.
Returns:a serializable parameterized type.
/** * Returns a type modelling a {@link List} whose elements are of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static ParameterizedType listOf(Type elementType) { return newParameterizedType(List.class, elementType); }
Returns a type modelling a Collection whose elements are of type elementType.
Returns:a serializable parameterized type.
/** * Returns a type modelling a {@link Collection} whose elements are of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static ParameterizedType collectionOf(Type elementType) { return newParameterizedType(Collection.class, elementType); }
Returns a type modelling a Set whose elements are of type elementType.
Returns:a serializable parameterized type.
/** * Returns a type modelling a {@link Set} whose elements are of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static ParameterizedType setOf(Type elementType) { return newParameterizedType(Set.class, elementType); }
Returns a type modelling a Map whose keys are of type keyType and whose values are of type valueType.
Returns:a serializable parameterized type.
/** * Returns a type modelling a {@link Map} whose keys are of type {@code keyType} and whose values * are of type {@code valueType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static ParameterizedType mapOf(Type keyType, Type valueType) { return newParameterizedType(Map.class, keyType, valueType); } // for other custom collections types, use newParameterizedType()
Returns a type modelling a Provider that provides elements of type elementType.
Returns:a serializable parameterized type.
/** * Returns a type modelling a {@link Provider} that provides elements of type {@code elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static ParameterizedType providerOf(Type providedType) { return newParameterizedType(Provider.class, providedType); }
Returns a type modelling a Provider that provides elements of type elementType.
Returns:a serializable parameterized type.
/** * Returns a type modelling a {@link javax.inject.Provider} that provides elements of type {@code * elementType}. * * @return a {@link java.io.Serializable serializable} parameterized type. */
public static Type javaxProviderOf(Type type) { return Types.newParameterizedType(javax.inject.Provider.class, type); } }