Coverage Report - org.apache.onami.autobind.utils.ClassLoadingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassLoadingUtils
0%
0/13
0%
0/4
3
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  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,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 
 20  
 package org.apache.onami.autobind.utils;
 21  
 
 22  
 import com.google.common.base.Optional;
 23  
 
 24  
 public final class ClassLoadingUtils {
 25  
 
 26  0
     private ClassLoadingUtils() {
 27  
         //Utility Class
 28  0
     }
 29  
 
 30  
     /**
 31  
      * Load a class from the current class loader or the thread context class loader.
 32  
      * The default behavior is to load the class using the class loader of ClassLoadingUtils and falls back
 33  
      * to the thread context class loader if the class was not found.
 34  
      * @param className
 35  
      * @return
 36  
      * @throws ClassNotFoundException
 37  
      */
 38  
     public static Class loadClass(String className) throws ClassNotFoundException {
 39  0
         Optional<ClassLoader> currentCloassLoader = Optional.of(ClassLoadingUtils.class.getClassLoader());
 40  0
         Optional<ClassLoader> threadContextClassLoader = Optional.<ClassLoader>fromNullable(Thread.currentThread().getContextClassLoader());
 41  0
         Optional<Class> clazz = tryLoad(className, currentCloassLoader).or(tryLoad(className, threadContextClassLoader));
 42  0
         if (!clazz.isPresent()) {
 43  0
             throw new ClassNotFoundException("Could not load class:" + className);
 44  
         } else {
 45  0
             return clazz.get();
 46  
         }
 47  
     }
 48  
 
 49  
     /**
 50  
      * Try loading a {@link Class} with specified name from the specified {@link ClassLoader}.
 51  
      *
 52  
      * @param className   The name of the class.
 53  
      * @param classLoader The class loader.
 54  
      * @return Returns an Optional class.
 55  
      */
 56  
     private static Optional<Class> tryLoad(String className, Optional<ClassLoader> classLoader) {
 57  0
         if (!classLoader.isPresent()) {
 58  0
             return Optional.absent();
 59  
         } else {
 60  
             try {
 61  0
                 return Optional.<Class>of(classLoader.get().loadClass(className));
 62  0
             } catch (ClassNotFoundException ex) {
 63  0
                 return Optional.absent();
 64  
             }
 65  
         }
 66  
     }
 67  
 }