| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Beanutils example source code file (

This example Commons Beanutils source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Beanutils tags/keywords

bytearrayoutputstream, bytearrayoutputstream, class, class, classloader, classreloader, classreloader, filenotfoundexception, inputstream, io, ioexception, string, string

The Commons Beanutils source code

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.apache.commons.beanutils.converters;


 * A special classloader useful for testing j2ee-like scenarios.
 * <p>In some tests we want to be able to emulate "container" frameworks,
 * where code runs in a hierarchy of classloaders, and certain classes may
 * be loaded by various classloaders in the hierarchy.</p>
 * <p>Normally this is done by having certain jars or class-file-directories
 * in the classpath of some classloaders but not others. This is quite 
 * difficult difficult to integrate with the build process for the unit
 * tests though; compiling certain classes and having the output go into
 * places that is not in the default classpath for the unit tests would be
 * a major pain.</p>
 * <p>So this class takes a sneaky alternative approach: it can grab any class
 * already loaded by a parent classloader and <i>reload that class via this
 * classloader. The effect is exactly as if a class (or jar file) had been
 * present in the classpath for a container's "shared" classloader <i>and
 * been present in the component-specific classpath too, without any messing
 * about with the way unit test code is compiled or executed.

public class ClassReloader extends ClassLoader {
    public ClassReloader(ClassLoader parent) {
     * Given a class already in the classpath of a parent classloader,
     * reload that class via this classloader.
    public Class reload(Class clazz) throws FileNotFoundException, IOException {
        String className = clazz.getName();
        String classFile = className.replace('.', '/') + ".class";
        InputStream classStream = getParent().getResourceAsStream(classFile);
        if (classStream == null) {
            throw new FileNotFoundException(classFile);
        byte[] buf = new byte[1024];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for(;;) {
            int bytesRead =;
            if (bytesRead == -1)
            baos.write(buf, 0, bytesRead);
        byte[] classData = baos.toByteArray();
        // now we have the raw class data, let's turn it into a class
        Class newClass = defineClass(className, classData, 0, classData.length);
        return newClass;

Other Commons Beanutils examples (source code examples)

Here is a short list of links related to this Commons Beanutils source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.