Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 Project Funcito Contributors
   *
   *  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.
  */
 
 /* Copied in large part from project Mockito, with the following license:
  * Copyright (c) 2007 Mockito contributors
  * This program is made available under the terms of the MIT License.
  */
 
 package org.funcito.internal.stub.cglib;
 
 import  org.objenesis.ObjenesisStd;
 
 import java.util.List;

Thanks to Mockito guys (and indirectly, jMock) for this handy class that wraps all the cglib magic.
 
 public class CglibImposterizer extends AbstractClassImposterizer {
 
     public static final CglibImposterizer INSTANCE = new CglibImposterizer();
 
     private ObjenesisStd objenesis = new ObjenesisStd();
 
     private CglibImposterizer() {}
 
         @Override
         public String getClassName(String prefixString sourceObject keyPredicate names) {
             return "codegen." + super.getClassName(prefixsourcekeynames);
         }
     };
 
     public <T> T imposterise(final MethodInterceptor interceptorClass<T> mockedTypeClass<?>... additionalInterfaces) {
         try {
             setConstructorsAccessible(mockedTypetrue);
             Class<?> proxyClass = createProxyClass(mockedTypeadditionalInterfaces);
             return mockedType.cast(createProxy(proxyClassinterceptor));
         } finally {
             setConstructorsAccessible(mockedTypefalse);
         }
     }
 
     private <T> Class<?> createProxyClass(Class<?> mockedTypeClass<?>... additionalInterfaces) {
         // NOTE: this was part of the original ClassImposterizer, but it doesn't seem to be needed
 //        if (mockedType == Object.class) {
 //            mockedType = ClassWithSuperclassToWorkAroundCglibBug.class;
 //        }
 
         Enhancer enhancer = new Enhancer() {
             @Override
             @SuppressWarnings("unchecked")
             protected void filterConstructors(Class scList constructors) {
                 // Don't filter
             }
         };
         enhancer.setClassLoader(SearchingClassLoader.combineLoadersOf(mockedType));
         enhancer.setUseFactory(true);
         if (mockedType.isInterface()) {
             enhancer.setSuperclass(Object.class);
             enhancer.setInterfaces(prepend(mockedTypeadditionalInterfaces));
         } else {
             Class<?>[] addInterfaces = additionalInterfaces.length==0 ? null : additionalInterfaces;
             enhancer.setInterfaces(addInterfaces);
             enhancer.setSuperclass(mockedType);
         }
 //        enhancer.setCallbackTypes(new Class[]{MethodInterceptor.class, NoOp.class});
         enhancer.setCallbackTypes(new Class[]{MethodInterceptor.class});
         // we want to handle bridge methods so don't filter them
 //        enhancer.setCallbackFilter(IGNORE_BRIDGE_METHODS);
         if (mockedType.getSigners() != null) {
         } else {
             enhancer.setNamingPolicy(.);
         }
 
         try {
             return enhancer.createClass();
         } catch (CodeGenerationException e) {
             if (Modifier.isPrivate(mockedType.getModifiers())) {
                throw new FuncitoException("\n"
                        + "Funcito cannot mock this class: " + mockedType
                        + ".\n"
                        + "Most likely it is a private class that is not visible by Funcito");
            }
            throw new FuncitoException("\n"
                    + "Funcito cannot mock this class: " + mockedType
                    + ".\n"
                    + "Funcito can only mock visible & non-final classes");
        }
    }
    private Object createProxy(Class<?> proxyClassfinal MethodInterceptor interceptor) {
        Factory proxy = (Factory.newInstance(proxyClass);
        proxy.setCallbacks(new Callback[]{interceptor});
//        proxy.setCallbacks(new Callback[] {interceptor, NoOp.INSTANCE});
        return proxy;
    }
    private static class ClassWithSuperclassToWorkAroundCglibBug {
    }
New to GrepCode? Check out our FAQ X