Coverage Report - org.truth0.util.ReflectionUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectionUtil
0%
0/13
0%
0/4
4
 
 1  
 /*
 2  
  * Copyright (c) 2011 David Saff
 3  
  * Copyright (c) 2011 Christian Gruber
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.truth0.util;
 18  
 
 19  
 import com.google.common.annotations.GwtIncompatible;
 20  
 import java.lang.reflect.Field;
 21  
 import java.lang.reflect.ParameterizedType;
 22  
 import java.lang.reflect.Type;
 23  
 
 24  
 /**
 25  
  * Reflection utility methods.
 26  
  *
 27  
  * @author Christian Gruber (cgruber@israfil.net)
 28  
  */
 29  
 @GwtIncompatible("java.lang.reflect.*")
 30  0
 public class ReflectionUtil {
 31  
 
 32  
   /** Returns the captured type. */
 33  
   public static Class<?> typeParameter(Class<?> clazz, int paramIndex) {
 34  0
     Type superclass = clazz.getGenericSuperclass();
 35  0
     if (!(superclass instanceof ParameterizedType)) {
 36  0
       throw new IllegalArgumentException ("" + superclass + " isn't parameterized");
 37  
     }
 38  0
     Type[] typeParams = ((ParameterizedType) superclass).getActualTypeArguments();
 39  0
     return (Class<?>)typeParams[paramIndex];
 40  
   }
 41  
 
 42  
   public static Field getField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
 43  0
     Class<?> currentClass = clazz;
 44  0
     while (currentClass != null) {
 45  
       try {
 46  0
         return clazz.getDeclaredField(fieldName);
 47  0
       } catch (NoSuchFieldException e) {
 48  0
         currentClass = currentClass.getSuperclass();
 49  0
       }
 50  
     }
 51  0
     throw new NoSuchFieldException("No such field " + fieldName + " declared on " +
 52  
         clazz.getSimpleName() + " or its parent classes.");
 53  
   }
 54  
 }