Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 the original author or authors.
   *
   * Licensed 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
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.callbackparams.internal;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import  org.apache.bcel.classfile.JavaClass;
 import  org.apache.bcel.util.ClassLoaderRepository;

Author(s):
Henrik Kaipe
 
 public class CallbackTestClassReloader extends ClassLoader {
 
     public interface ReloadFilter {
         boolean needsToBeReloaded(String classNameToReload);
     }
 
     interface Rebyter {

        
Performs custom byte-code modifications of the class about to be reloaded. But whatever you do - you must not change the class-name!!

Parameters:
originalClassDef class-definition as retrieved from the original classloader
Returns:
the byte array with the new class-definition's byte-code
 
         byte[] rebyte(
                 CallbackContext callbackContext,
                 JavaClass templateClassDef);
 
         byte[] newSyntheticClass(String className);
     }
 
     private final ClassLoader templateClassLoader;
     private final ClassLoaderRepository templateClassLoaderRepo;
     private final ReloadFilter reloadFilter;
     private final Rebyter rebyter;
 
     private final Set redefinedPackages = new HashSet();
     private final Map reloadedClasses = new HashMap();
     private final ResourceMap reloadedResources = new ResourceMap();
 
             new CallbackContextQue(null); // Will be replace by construcor
 
     private CallbackTestClassReloader(ClassLoader parentClassLoader,
             ClassLoader templateClassLoader,
             Rebyter rebyterReloadFilter reloadFilter) {
         super(parentClassLoader);
         setDefaultAssertionStatus(true);
 
         this. = templateClassLoader;
         this. = reloadFilter;
         this. = rebyter;
         this. =
                 new ClassLoaderRepository(templateClassLoader);
         try {
             this. = new CallbackContextQue(
                     CallbackContextFactory.forClassLoader(this));
         } catch (Exception ex) {
             if (ex instanceof RuntimeException) {
                 throw (RuntimeException)ex;
             } else {
                 throw new Error(ex);
             }
         }
     }
 
     public static CallbackTestClassReloader newInstance(Class testClass,
             ReloadFilter specialReloadFilterCallbackRebyteInfo rebyteInfo) {
         CallbackMethodProxyingRebyter rebyter =
                 new CallbackMethodProxyingRebyter(testClassrebyteInfo);
        return new CallbackTestClassReloader(
                rebyter.getParentClassLoader(),
                testClass.getClassLoader(),
                rebyter,
                new GeneralReloadFilter(specialReloadFilter));
    }
    //    @Override cannot be used under JDK-1.4.x
    public URL getResource(String name) {
        URL rebytedResource = .getURL(name);
        if (null != rebytedResource) {
            return rebytedResource;
        } else {
            return .getResource(name);
        }
    }
//    @Override cannot be used under JDK-1.4.x
    public InputStream getResourceAsStream(String name) {
        InputStream is = .getAsStream(name);
        if (null != is) {
            return is;
        } else {
            return .getResourceAsStream(name);
        }
    }
//    @Override cannot be used under JDK-1.4.x
    protected Class findClass(String namethrows ClassNotFoundException {
        if (.needsToBeReloaded(name)) {
            return findReloadedClass(name);
        } else {
            return .loadClass(name);
        }
    }
    private void ensurePackageIsRedefined(String packageName)
    throws IllegalArgumentException {
        if (.add(packageName)) {
            definePackage(packageName,
                    nullnullnullnullnullnullnull);
        }
    }
    private Class findReloadedClass(String namethrows ClassNotFoundException {
        if (false == .containsKey(name)) {
            ensurePackageIsRedefined(name
                    .substring(0, Math.max(0, name.lastIndexOf('.'))));
            reloadClass(name);
        }
        return (Class.get(name);
    }
    private void reloadClass(String namethrows ClassNotFoundException {
        byte[] byteCode = .newSyntheticClass(name);
        if (null == byteCode) {
            byteCode = .rebyte(,
                    .loadClass(name));
        }
        Class reloadedClass = defineClass(namebyteCode, 0, byteCode.length);
        .put(reloadedClassbyteCode);
        .put(namereloadedClass);
    }
    public void setupTestRunCallbackRecord(List callbackRecord) {
        getCallbackContext().setCallbackRecord(callbackRecord);
    }
        this..releaseQue();
        return this..getTargetContext();
    }
    //    @Override cannot be used under JDK-1.4.x
    
Handles the necessary stuff for making method getResources(String) work properly. Please note that in JDK-1.4 the method getResources(String) is final! - That is why this method was overridden instead.
    protected Enumeration findResources(String namethrows IOException {
        final URL url = .getURL(name);
        if (null != url) {
            return singletonEnumeration(url);
        } else {
            return findTemplateClassLoaderResources(name);
        }
    }
    private Enumeration singletonEnumeration(final URL singleElement) {
        return new Enumeration() {
            boolean elementAvailable = true;
            public boolean hasMoreElements() {
                return ;
            }
            public Object nextElement() {
                if (hasMoreElements()) {
                     = false;
                    return singleElement;
                } else {
                    throw new NoSuchElementException(
                            "No more resource available");
                }
            }
        };
    }
    throws IOException {
        Enumeration templateResources = .getResources(name);
        /* Possible duplicate urls from parent should be removed ... */
        if (null != getParent()) {
            Collection parentResources = Collections
                    .list(getParent().getResources(name));
            while (templateResources.hasMoreElements()
                    && false == parentResources.isEmpty()) {
                URL duplicate = (URLtemplateResources.nextElement();
                if (false == parentResources.remove(duplicate)) {
                    /* Weird situation - seems like the template
                     * class-loader somehow censurs the resources of its
                     * parent. Best way to resolve the situation is
                     * probably to return all resources available through
                     * the template class-loader ... */
                    return .getResources(name);
                }
            }
        }
        return templateResources;
    }
New to GrepCode? Check out our FAQ X