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.annotation;
 
 import java.util.List;
 import java.util.Map;

Author(s):
Henrik Kaipe
 
 
     private final static ThreadLocal<Map<Object,Object>> nestedArrays =
             new ThreadLocal<Map<ObjectObject>>() {
         @Override
         protected Map<ObjectObjectinitialValue() {
             return new IdentityHashMap<ObjectObject>();
         }
     };
 
             ClassLoader bridgedLoader,
             CallbackContext bridgedContext) {
         super(bridgedLoaderbridgedContext);
     }

    
This method can only handle bridge record-values that are Enum constants. Other values will be left as-is.
 
     public List getCallbackRecord() {
         List list = .getCallbackRecord();
         for (ListIterator li = list.listIterator() ; li.hasNext() ;) {
             final Object nextValue = li.next();
             if (nextValue instanceof Enum) {
                 li.set(findEquivalentEnum(nextValue));
             }
         }
         return list;
     }

    
This method can only handle records that consists exclusivly of Enum-constants.
 
     @Override
     public void setCallbackRecord(Collection callbackRecord) {
         List records = new ArrayList(callbackRecord.size());
         for (Object r : callbackRecord) {
             records.add(findEquivalent(r));
         }
         .setCallbackRecord(records);
     }
 
     static Object findEquivalent(Object oClassLoader searchLoader) {
         if (null == o) {
             return null;
         }
 
         Class<?> c = o.getClass();
         if (c.isArray()) {
             return createEquivalentArray(osearchLoader);
         } else {
             return findEquivalentEnum(osearchLoader);
         }
     }
 
     private static Object createEquivalentArray(
             Object oClassLoader searchLoader) {
         Class<?> componentType = o.getClass().getComponentType();
         if (componentType.isPrimitive()) {
             return o;
 
         } else if (.get().containsKey(o)) {
             return .get().get(o);
 
         } else {
             Object[] array = (Object[])o;
            Object[] eqArray = (Object[]) Array.newInstance(
                    findEquivalentClass(componentTypesearchLoader),
                    array.length);
            .get().put(arrayeqArray);
            for (int i = 0 ; i < eqArray.length ; ++i) {
                eqArray[i] = findEquivalent(array[i], searchLoader);
            }
            return .get().remove(array);
        }
    }
    private static Class<?> findEquivalentClass(
            Class<?> cClassLoader searchLoader) {
        if (false == c.isArray()) {
            try {
                return searchLoader.loadClass(c.getName());
            } catch (ClassNotFoundException ex) {
                throw new Error("Cannot find equivalent of " + c
                    + " on class-loader " + searchLoaderex);
            }
        } else {
            Class<?> componentType = c.getComponentType();
            if (componentType.isPrimitive()) {
                return c;
            } else {
                return Array.newInstance(findEquivalentClass(
                        componentTypesearchLoader), 0).getClass();
            }
        }
    }

    

Parameters:
knownEnumConstant a callback value, expected to be an Enum constant
searchLoader a classloader
Returns:
the equivalent of knownEnumConstant from the specified class-loader searchLoader
    private static Object findEquivalentEnum(
            Object knownEnumConstantClassLoader searchLoader) {
        if (null == knownEnumConstant) {
            return null;
        }
        Class<?> eqClass = findEquivalentClass(
                knownEnumConstant.getClass(), searchLoader);
        if (knownEnumConstant.getClass() == eqClass) {
            return knownEnumConstant;
        } else {
            Enum<?> enm = (Enum<?>) knownEnumConstant;
            eqClass = findEquivalentClass(
                    enm.getDeclaringClass(), searchLoader);
            return Enum.valueOf(eqClass.asSubclass(Enum.class), enm.name());
        }
    }
New to GrepCode? Check out our FAQ X