Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2006 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;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.inject.internal.MoreTypes.canonicalize;
import com.google.common.collect.ImmutableList;
import com.google.inject.internal.MoreTypes;
import com.google.inject.util.Types;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.List;
/**
* Represents a generic type {@code T}. Java doesn't yet provide a way to represent generic types,
* so this class does. Forces clients to create a subclass of this class which enables retrieval of
* the type information even at runtime.
*
*
For example, to create a type literal for {@code List}, you can create an empty
* anonymous inner class:
*
*
{@code TypeLiteral> list = new TypeLiteral>() {};}
*
*
Along with modeling generic types, this class can resolve type parameters. For example, to
* figure out what type {@code keySet()} returns on a {@code Map}, use this code:
*
*
{@code
* TypeLiteral
*
* @author [email protected] (Bob Lee)
* @author [email protected] (Jesse Wilson)
*/
public class TypeLiteral {
final Class rawType;
final Type type;
final int hashCode;
/**
* Constructs a new type literal. Derives represented class from type parameter.
*
*
Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
* anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
*/
@SuppressWarnings("unchecked")
protected TypeLiteral() {
this.type = getSuperclassTypeParameter(getClass());
this.rawType = (Class) MoreTypes.getRawType(type);
this.hashCode = type.hashCode();
}
/** Unsafe. Constructs a type literal manually. */
@SuppressWarnings("unchecked")
TypeLiteral(Type type) {
this.type = canonicalize(checkNotNull(type, "type"));
this.rawType = (Class) MoreTypes.getRawType(this.type);
this.hashCode = this.type.hashCode();
}
/**
* Returns the type from super class's type parameter in {@link MoreTypes#canonicalize(Type)
* canonical form}.
*/
static Type getSuperclassTypeParameter(Class subclass) {
Type superclass = subclass.getGenericSuperclass();
if (superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return canonicalize(parameterized.getActualTypeArguments()[0]);
}
/** Gets type literal from super class's type parameter. */
static TypeLiteral fromSuperclassTypeParameter(Class subclass) {
return new TypeLiteral