Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2012 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.template;
 
 import java.util.List;
 import java.util.Map;

Author(s):
Henrik Kaipe
 
 public class TestrunCallbacks {
 
     private static final List/*CallbackRef*/ callbackMethodParams =
             new ArrayList();
     private static final Map/*Field,CallbackRef*/ callbackInjectionFields =
             new IdentityHashMap();
     private static final Map/*Method,Object[]*/ valuesCache =
             new IdentityHashMap();

    
This piece of information has been saved for exceptional circumstances when the test-classes has been rebyted/reloaded once more by the wrapped runner from third-party.
 
     private static final List/*Method*/ callbackMethods = new ArrayList();
     private static Collection/*Combined*/ currentCombinedRecord;

    
To have the class-loader as a final field makes debugging easier.
 
 //    private final ClassLoader cl = getClass().getClassLoader();
 
     boolean currentlyWrappedWithinAdaptiveRules = false;
 
     private Collection/*Combined*/ refBackupOfCurrentCombinedRecord;
     private CallbackRecord callbackRecord;
     protected Object[] callbackMethodArguments;
 
     public static int addMethodParams(Method m) {
         .add(m);
         int startIndex = .size();
         for (int i = 0, size = m.getParameterTypes().length ; i < size ; ++i) {
             .add(CallbackRef.newInstance(mi));
         }
         return startIndex;
     }
 
     public static void addFieldToInject(Field f) {
         try {
             f.setAccessible(true);
         } catch (SecurityException ignore) {
             /* We will try to access the field anyway ... */
         }
         .put(f, CallbackRef.newInstance(f));
     }
 
     public static void setCallbackRecord(Collection testRunCombinedRecord) {
          = testRunCombinedRecord;
     }
 
     public static Iterator iterateFields() {
         return .keySet().iterator();
     }
 
     public static Iterator iterateMethods() {
         return .iterator();
     }
 
     public static Collection getCurrentCallbackRecord() {
         return ;
     }
 
    private void parseCurrentCombinedRecord() {
        List callbacks = new ArrayList(.size());
        for (Iterator i = .iterator(); i.hasNext();) {
            final Combined combined = Combined
                    .resurrectFromOtherClassLoader(i.next());
            Object callback = combined.retrieve(this);
            if (null != callback) {
                callbacks.add(callback);
            }
        }
        this. = new CallbackRecord(callbacks);
    }
    private CallbackRecord getCallbackRecord() {
            parseCurrentCombinedRecord();
        }
        return this.;
    }
    private void setupCallbackMethodArguments() {
                new Object[.size()]);
        ListIterator li = Arrays.asList().listIterator();
        while (li.hasNext()) {
            final CallbackRef methodParam = (CallbackRefli.next();
            li.set(createCallbackProxy(methodParam));
        }
    }
    private void injectCallbackFields() {
        for (Iterator i = .entrySet().iterator()
                ; i.hasNext() ;) {
            Map.Entry/*Field,CallbackRef*/ fieldEntr = (Map.Entryi.next();
            Field f = (FieldfieldEntr.getKey();
            if (f.getDeclaringClass().isInstance(this)) {
                try {
                    f.set(thiscreateCallbackProxy(
                            (CallbackReffieldEntr.getValue()));
                } catch (Exception ex) {
                    throw new Error("Callback injection failed for " + fex);
                }
            }
        }
    }
    private Object createCallbackProxy(final CallbackRef ref) {
        final Class paramClass = ref.getParamClass();
        final Class[] interfaces = new Class[] {paramClass};
        if (CallbackControlPanel.class == paramClass
                || org.callbackparams.CallbackControlPanel.class == paramClass) {
            return Proxy.newProxyInstance(
                    getClass().getClassLoader(), interfaces,
                    new InvocationHandler() {
                public Object invoke(Object proxyMethod methodObject[] args)
                throws Throwable {
                    try {
                        return method.invoke(
                                getCallbackRecord().getCallbackControlPanel(),
                                args);
                    } catch (InvocationTargetException x) {
                        throw x.getTargetException();
                    }
                }
            });
            
        } else {
            return Proxy.newProxyInstance(
                    paramClass.getClassLoader(), interfaces,
                    new InvocationHandler() {
                CallbackRecord record;
                InvocationHandler wrappedInvokeHandler;
                public Object invoke(Object proxyMethod methodObject[] args)
                throws Throwable {
                    if (getCallbackRecord() != ) {
                         = getCallbackRecord();
                         = .newCallbackInvoker(ref);
                    }
                    return .invoke(proxymethodargs);
                }
            });
        }
    }
    private void init() throws Exception {
        if (null == ) {
            ThirdPartyReloadingWorkaround.getInstance()
                    .resurrectSetup(getClass());
        }
        injectCallbackFields();
    }
    public TestrunCallbacks() throws Exception {
        init();
    }
New to GrepCode? Check out our FAQ X