crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 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 | package com.google.inject; |
| 18 | |
| 19 | import static com.google.inject.util.Objects.nonNull; |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 20 | import java.lang.reflect.ParameterizedType; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 21 | import java.lang.reflect.Type; |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 24 | * Represents a generic type {@code T}. Java doesn't yet provide a way to |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 25 | * represent generic types, so this class does. Forces clients to create a |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 26 | * subclass of this class which enables retrieval the type information even at |
| 27 | * runtime. |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 28 | * |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 29 | * <p>For example, to create a type literal for {@code List<String>}, you can |
| 30 | * create an empty anonymous inner class: |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 31 | * |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 32 | * <p> |
| 33 | * {@code TypeLiteral<List<String>> list = new TypeLiteral<List<String>>() {};} |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 34 | * |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 35 | * <p>Assumes that type {@code T} implements {@link Object#equals} and |
| 36 | * {@link Object#hashCode()} as value (as opposed to identity) comparison. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 37 | * |
| 38 | * @author crazybob@google.com (Bob Lee) |
| 39 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 40 | public abstract class TypeLiteral<T> { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 41 | |
| 42 | final Class<? super T> rawType; |
| 43 | final Type type; |
| 44 | |
| 45 | /** |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 46 | * Constructs a new type literal. Derives represented class from type |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 47 | * parameter. |
| 48 | * |
| 49 | * <p>Clients create an empty anonymous subclass. Doing so embeds the type |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 50 | * parameter in the anonymous class's type hierarchy so we can reconstitute it |
| 51 | * at runtime despite erasure. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 52 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 53 | @SuppressWarnings("unchecked") |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 54 | protected TypeLiteral() { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 55 | this.type = getSuperclassTypeParameter(getClass()); |
| 56 | this.rawType = (Class<? super T>) getRawType(type); |
| 57 | } |
| 58 | |
| 59 | /** |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 60 | * Unsafe. Constructs a type literal manually. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 61 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 62 | @SuppressWarnings("unchecked") |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 63 | private TypeLiteral(Type type) { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 64 | this.rawType = (Class<? super T>) getRawType(nonNull(type, "type")); |
| 65 | this.type = type; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Gets type from super class's type parameter. |
| 70 | */ |
| 71 | static Type getSuperclassTypeParameter(Class<?> subclass) { |
| 72 | Type superclass = subclass.getGenericSuperclass(); |
| 73 | if (superclass instanceof Class) { |
| 74 | throw new RuntimeException("Missing type parameter."); |
| 75 | } |
| 76 | return ((ParameterizedType) superclass).getActualTypeArguments()[0]; |
| 77 | } |
| 78 | |
| 79 | /** |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 80 | * Gets type literal from super class's type parameter. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 81 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 82 | static TypeLiteral<?> fromSuperclassTypeParameter(Class<?> subclass) { |
| 83 | return new SimpleTypeLiteral<Object>(getSuperclassTypeParameter(subclass)); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 86 | @SuppressWarnings({ "unchecked" }) |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 87 | private static Class<?> getRawType(Type type) { |
| 88 | if (type instanceof Class<?>) { |
| 89 | // type is a normal class. |
| 90 | return (Class<?>) type; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 91 | } |
| 92 | else { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 93 | // type is a parameterized type. |
| 94 | if (!(type instanceof ParameterizedType)) { |
| 95 | unexpectedType(type, ParameterizedType.class); |
| 96 | } |
| 97 | ParameterizedType parameterizedType = (ParameterizedType) type; |
| 98 | |
| 99 | // I'm not exactly sure why getRawType() returns Type instead of Class. |
| 100 | // Neal isn't either but suspects some pathological case related |
| 101 | // to nested classes exists. |
| 102 | Type rawType = parameterizedType.getRawType(); |
| 103 | if (!(rawType instanceof Class<?>)) { |
| 104 | unexpectedType(rawType, Class.class); |
| 105 | } |
| 106 | return (Class<?>) rawType; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Gets the raw type. |
| 112 | */ |
| 113 | Class<? super T> getRawType() { |
| 114 | return rawType; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Gets underlying {@code Type} instance. |
| 119 | */ |
| 120 | public Type getType() { |
| 121 | return type; |
| 122 | } |
| 123 | |
| 124 | public int hashCode() { |
| 125 | return type.hashCode(); |
| 126 | } |
| 127 | |
| 128 | public boolean equals(Object o) { |
| 129 | if (o == this) { |
| 130 | return true; |
| 131 | } |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 132 | if (!(o instanceof TypeLiteral<?>)) { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 133 | return false; |
| 134 | } |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 135 | TypeLiteral<?> t = (TypeLiteral<?>) o; |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 136 | return type.equals(t.type); |
| 137 | } |
| 138 | |
| 139 | public String toString() { |
| 140 | return type instanceof Class<?> |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 141 | ? ((Class<?>) type).getName() |
| 142 | : type.toString(); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 143 | } |
| 144 | |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 145 | static void unexpectedType(Type type, Class<?> expected) { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 146 | throw new AssertionError( |
| 147 | "Unexpected type. Expected: " + expected.getName() |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 148 | + ", got: " + type.getClass().getName() |
| 149 | + ", for type literal: " + type.toString() + "."); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 153 | * Gets type literal for the given {@code Type} instance. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 154 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 155 | public static TypeLiteral<?> get(Type type) { |
| 156 | return new SimpleTypeLiteral<Object>(type); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 160 | * Gets type literal for the given {@code Class} instance. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 161 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 162 | public static <T> TypeLiteral<T> get(Class<T> type) { |
| 163 | return new SimpleTypeLiteral<T>(type); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 164 | } |
| 165 | |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 166 | private static class SimpleTypeLiteral<T> extends TypeLiteral<T> { |
| 167 | public SimpleTypeLiteral(Type type) { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 168 | super(type); |
| 169 | } |
| 170 | } |
| 171 | } |