Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

Author(s):
Henrik Kaipe
 
 class CallbackContextBridge implements CallbackContext {
 
     static CallbackContext newInstance(
             ClassLoader bridgedClassLoaderCallbackContext bridgedContext) {
         return new CallbackContextBridge(bridgedClassLoaderbridgedContext);
     }
 
     protected final CallbackContext bridgedContext;
     protected final ClassLoader bridgedLoader;
     protected final ClassLoader localLoader =
             TestrunCallbacks.class.getClassLoader();
     private final Map methodMaps = new HashMap();
 
     private CallbackContextBridge(
             ClassLoader bridgedLoader,
             CallbackContext bridgedContext) {
         this. = bridgedLoader;
         this. = bridgedContext;
     }
 
     public void addFieldToInject(Field f) {
         try {
             .addFieldToInject(
                     getBridgedDeclaringClass(f).getDeclaredField(f.getName()));
         } catch (NoSuchFieldException ex) {
             throw new Error(ex);
         }
     }
 
     public Iterator iterateFields() {
         return new BridgedMemberIterator(.iterateFields());
     }
 
     public int addMethodParams(Method m) {
         final Class bridgedClass = getBridgedDeclaringClass(m);
         return .addMethodParams( (Method)
                 getMethodMap(bridgedClass).get(MethodHashKey.getHashKey(m)));
     }
 
     public Iterator iterateMethods() {
         return new BridgedMemberIterator(.iterateMethods());
     }
 
     public List getCallbackRecord() {
         return .getCallbackRecord();
     }
 
     public void setCallbackRecord(Collection callbackRecord) {
         List records = new ArrayList(callbackRecord.size());
         records.addAll(callbackRecord);
         .setCallbackRecord(records);
     }
 
     private Map getMethodMap(Class clazz) {
         if (false == .containsKey(clazz)) {
             Map map = new HashMap();
             for (Iterator i = Arrays.asList(clazz.getDeclaredMethods())
                     .iterator() ; i.hasNext() ;) {
                 final Method m = (Methodi.next();
                 map.put(MethodHashKey.getHashKey(m), m);
             }
             .put(clazzmap);
         }
         return (Map.get(clazz);
     }
    private Class getBridgedDeclaringClass(Member localMember) {
        try {
            return 
                    .loadClass(localMember.getDeclaringClass().getName());
        } catch (ClassNotFoundException ex) {
            throw new Error(ex);
        }
    }
    private class BridgedMemberIterator implements Iterator {
        private Iterator sourceMembers;
        public BridgedMemberIterator(Iterator sourceMembers) {
            this. = sourceMembers;
        }
        public boolean hasNext() {
            return .hasNext();
        }
        public Object next() {
            Member sourceMember = (Member.next();
            Class localClass;
            try {
                localClass = .loadClass(
                        sourceMember.getDeclaringClass().getName());
            } catch (ClassNotFoundException ex) {
                throw new Error(ex);
            }
            if (sourceMember instanceof Field) {
                try {
                    return localClass.getDeclaredField(sourceMember.getName());
                } catch (NoSuchFieldException ex) {
                    throw new Error(ex);
                }
            } else if (sourceMember instanceof Method) {
                return getMethodMap(localClass)
                        .get(MethodHashKey.getHashKey((Method)sourceMember));
            } else {
                throw new UnsupportedOperationException(
                        "Unable to bridge members of "
                        + sourceMember.getClass());
            }
        }
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
New to GrepCode? Check out our FAQ X