Coverage Report - org.crosswire.common.util.CallContext
 
Classes in this File Line Coverage Branch Coverage Complexity
CallContext
0%
0/8
N/A
1.5
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2005 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.common.util;
 21  
 
 22  
 
 23  
 /**
 24  
  * This singleton class provides a way for a method to determine which class
 25  
  * called it.
 26  
  * <p>
 27  
  * It has been tested to work in command line and WebStart environments.
 28  
  * 
 29  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 30  
  * @author DM Smith
 31  
  */
 32  
 public final class CallContext {
 33  
     /**
 34  
      * Prevent instantiation
 35  
      */
 36  0
     private CallContext() {
 37  0
     }
 38  
 
 39  
     /**
 40  
      * Singleton accessor
 41  
      * 
 42  
      * @return this singleton
 43  
      */
 44  
     public static CallContext instance() {
 45  0
         return resolver;
 46  
     }
 47  
 
 48  
     /**
 49  
      * When called from a method it will return the class calling that method.
 50  
      * 
 51  
      * @return the immediate calling class
 52  
      */
 53  
     public static Class<?> getCallingClass() {
 54  0
         return getCallingClass(1); // add 1 for this method
 55  
     }
 56  
 
 57  
     /**
 58  
      * When called from a method it will return the i-th class calling that
 59  
      * method, up the call chain. If used with a -1 it will return the class
 60  
      * making the call -2 and -3 will return this class
 61  
      * 
 62  
      * @param i the i-th coller
 63  
      * @return the desired calling class
 64  
      * @throws ArrayIndexOutOfBoundsException
 65  
      *             if the index is not valid
 66  
      */
 67  
     public static Class<?> getCallingClass(int i) {
 68  
         try {
 69  0
             return Class.forName(Thread.currentThread().getStackTrace()[CALL_CONTEXT_OFFSET + i].getClassName());
 70  0
         } catch (ClassNotFoundException e) {
 71  0
             return CallContext.class;
 72  
         }
 73  
     }
 74  
 
 75  
     // may need to change if this class is redesigned
 76  
     /**
 77  
      * Offset needed to represent the caller of the method that called this
 78  
      * method.
 79  
      * 
 80  
      */
 81  
     private static final int CALL_CONTEXT_OFFSET = 3;
 82  
 
 83  0
     private static CallContext resolver = new CallContext();
 84  
 }