Coverage Report - org.simpleframework.xml.strategy.Loader
 
Classes in this File Line Coverage Branch Coverage Complexity
Loader
71%
5/7
50%
1/2
1.333
 
 1  
 /*
 2  
  * Loader.java January 2010
 3  
  *
 4  
  * Copyright (C) 2010, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.strategy;
 20  
 
 21  
 /**
 22  
  * The <code>Loader</code> object is used to provide class loading
 23  
  * for the strategies. This will attempt to load the class using
 24  
  * the thread context class loader, if this loader is set it will
 25  
  * be used to load the class. If not then the class will be loaded
 26  
  * using the caller class loader. Loading in this way ensures 
 27  
  * that a custom loader can be provided using the current thread.
 28  
  * 
 29  
  * @author Niall Gallagher
 30  
  */
 31  680
 class Loader {
 32  
    
 33  
    /**
 34  
     * This method is used to acquire the class of the specified name.
 35  
     * Loading is performed by the thread context class loader as this
 36  
     * will ensure that the class loading strategy can be changed as
 37  
     * requirements dictate. Typically the thread context class loader
 38  
     * can handle all serialization requirements.
 39  
     * 
 40  
     * @param type this is the name of the class that is to be loaded
 41  
     * 
 42  
     * @return this returns the class that has been loaded by this
 43  
     */
 44  
    public Class load(String type) throws Exception {
 45  71613
       ClassLoader loader = getClassLoader();
 46  
       
 47  71613
       if(loader == null) {
 48  0
          loader = getCallerClassLoader();
 49  
       }
 50  71613
       return loader.loadClass(type);      
 51  
    }
 52  
    
 53  
    /**
 54  
     * This is used to acquire the caller class loader for this object.
 55  
     * Typically this is only used if the thread context class loader
 56  
     * is set to null. This ensures that there is at least some class
 57  
     * loader available to the strategy to load the class.
 58  
     * 
 59  
     * @return this returns the loader that loaded this class     
 60  
     */
 61  
    private static ClassLoader getCallerClassLoader() throws Exception {
 62  0
       return Loader.class.getClassLoader();
 63  
    }
 64  
 
 65  
    /**
 66  
     * This is used to acquire the thread context class loader. This
 67  
     * is the default class loader used by the cycle strategy. When
 68  
     * using the thread context class loader the caller can switch the
 69  
     * class loader in use, which allows class loading customization.
 70  
     * 
 71  
     * @return this returns the loader used by the calling thread
 72  
     */
 73  
    private static ClassLoader getClassLoader() throws Exception {
 74  71613
       return Thread.currentThread().getContextClassLoader();
 75  
    }
 76  
    
 77  
 }