Coverage Summary for Class: TypeParameter (com.google.common.reflect)

Class Class, % Method, % Line, %
TypeParameter 0% (0/1) 0% (0/4) 0% (0/11)


1 /* 2  * Copyright (C) 2011 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.reflect; 16  17 import static com.google.common.base.Preconditions.checkArgument; 18  19 import com.google.common.annotations.Beta; 20 import java.lang.reflect.Type; 21 import java.lang.reflect.TypeVariable; 22 import javax.annotation.CheckForNull; 23  24 /** 25  * Captures a free type variable that can be used in {@link TypeToken#where}. For example: 26  * 27  * <pre>{@code 28  * static <T> TypeToken<List<T>> listOf(Class<T> elementType) { 29  * return new TypeToken<List<T>>() {} 30  * .where(new TypeParameter<T>() {}, elementType); 31  * } 32  * }</pre> 33  * 34  * @author Ben Yu 35  * @since 12.0 36  */ 37 @Beta 38 @ElementTypesAreNonnullByDefault 39 /* 40  * A nullable bound would let users create a TypeParameter instance for a parameter with a nullable 41  * bound. However, it would also let them create `new TypeParameter<@Nullable T>() {}`, which 42  * wouldn't behave as users might expect. Additionally, it's not clear how the TypeToken API could 43  * support even a "normal" `TypeParameter<T>` when `<T>` has a nullable bound. (See the discussion 44  * on TypeToken.where.) So, in the interest of failing fast and encouraging the user to switch to a 45  * non-null bound if possible, let's require a non-null bound here. 46  * 47  * TODO(cpovirk): Elaborate on "wouldn't behave as users might expect." 48  */ 49 public abstract class TypeParameter<T> extends TypeCapture<T> { 50  51  final TypeVariable<?> typeVariable; 52  53  protected TypeParameter() { 54  Type type = capture(); 55  checkArgument(type instanceof TypeVariable, "%s should be a type variable.", type); 56  this.typeVariable = (TypeVariable<?>) type; 57  } 58  59  @Override 60  public final int hashCode() { 61  return typeVariable.hashCode(); 62  } 63  64  @Override 65  public final boolean equals(@CheckForNull Object o) { 66  if (o instanceof TypeParameter) { 67  TypeParameter<?> that = (TypeParameter<?>) o; 68  return typeVariable.equals(that.typeVariable); 69  } 70  return false; 71  } 72  73  @Override 74  public String toString() { 75  return typeVariable.toString(); 76  } 77 }