Coverage Report - org.apache.commons.io.input.ClassLoaderObjectInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassLoaderObjectInputStream
66%
8/12
100%
2/2
2,667
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  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.apache.commons.io.input;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.io.ObjectInputStream;
 22  
 import java.io.ObjectStreamClass;
 23  
 import java.io.StreamCorruptedException;
 24  
 import java.lang.reflect.Proxy;
 25  
 
 26  
 /**
 27  
  * A special ObjectInputStream that loads a class based on a specified
 28  
  * <code>ClassLoader</code> rather than the system default.
 29  
  * <p>
 30  
  * This is useful in dynamic container environments.
 31  
  *
 32  
  * @since 1.1
 33  
  */
 34  
 public class ClassLoaderObjectInputStream extends ObjectInputStream {
 35  
 
 36  
     /** The class loader to use. */
 37  
     private final ClassLoader classLoader;
 38  
 
 39  
     /**
 40  
      * Constructs a new ClassLoaderObjectInputStream.
 41  
      *
 42  
      * @param classLoader  the ClassLoader from which classes should be loaded
 43  
      * @param inputStream  the InputStream to work on
 44  
      * @throws IOException in case of an I/O error
 45  
      * @throws StreamCorruptedException if the stream is corrupted
 46  
      */
 47  
     public ClassLoaderObjectInputStream(
 48  
             final ClassLoader classLoader, final InputStream inputStream)
 49  
             throws IOException, StreamCorruptedException {
 50  12
         super(inputStream);
 51  12
         this.classLoader = classLoader;
 52  12
     }
 53  
 
 54  
     /**
 55  
      * Resolve a class specified by the descriptor using the
 56  
      * specified ClassLoader or the super ClassLoader.
 57  
      *
 58  
      * @param objectStreamClass  descriptor of the class
 59  
      * @return the Class object described by the ObjectStreamClass
 60  
      * @throws IOException in case of an I/O error
 61  
      * @throws ClassNotFoundException if the Class cannot be found
 62  
      */
 63  
     @Override
 64  
     protected Class<?> resolveClass(final ObjectStreamClass objectStreamClass)
 65  
             throws IOException, ClassNotFoundException {
 66  
 
 67  
         try {
 68  22
             return Class.forName(objectStreamClass.getName(), false, classLoader);
 69  0
         } catch (ClassNotFoundException cnfe) {
 70  
             // delegate to super class loader which can resolve primitives
 71  0
             return super.resolveClass(objectStreamClass);
 72  
         }
 73  
     }
 74  
 
 75  
     /**
 76  
      * Create a proxy class that implements the specified interfaces using
 77  
      * the specified ClassLoader or the super ClassLoader.
 78  
      *
 79  
      * @param interfaces the interfaces to implement
 80  
      * @return a proxy class implementing the interfaces
 81  
      * @throws IOException in case of an I/O error
 82  
      * @throws ClassNotFoundException if the Class cannot be found
 83  
      * @see java.io.ObjectInputStream#resolveProxyClass(java.lang.String[])
 84  
      * @since 2.1
 85  
      */
 86  
     @Override
 87  
     protected Class<?> resolveProxyClass(final String[] interfaces) throws IOException,
 88  
             ClassNotFoundException {
 89  2
         final Class<?>[] interfaceClasses = new Class[interfaces.length];
 90  4
         for (int i = 0; i < interfaces.length; i++) {
 91  2
             interfaceClasses[i] = Class.forName(interfaces[i], false, classLoader);
 92  
         }
 93  
         try {
 94  2
             return Proxy.getProxyClass(classLoader, interfaceClasses);
 95  0
         } catch (final IllegalArgumentException e) {
 96  0
             return super.resolveProxyClass(interfaces);
 97  
         }
 98  
     }
 99  
 
 100  
 }