Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright 2003-2009 OFFIS, Henri Tremblay
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.easymock.internal;
     17 
     18 import java.io.Serializable;
     19 import java.lang.reflect.Method;
     20 import java.util.HashMap;
     21 import java.util.Map;
     22 
     23 public class MethodSerializationWrapper implements Serializable {
     24 
     25     private static final long serialVersionUID = 1775475200823842126L;
     26 
     27     private static final Map<String, Class<?>> primitiveTypes = new HashMap<String, Class<?>>(
     28             10);
     29 
     30     static {
     31         primitiveTypes.put(Boolean.TYPE.getName(), Boolean.TYPE);
     32         primitiveTypes.put(Byte.TYPE.getName(), Byte.TYPE);
     33         primitiveTypes.put(Short.TYPE.getName(), Short.TYPE);
     34         primitiveTypes.put(Character.TYPE.getName(), Character.TYPE);
     35         primitiveTypes.put(Integer.TYPE.getName(), Integer.TYPE);
     36         primitiveTypes.put(Long.TYPE.getName(), Long.TYPE);
     37         primitiveTypes.put(Float.TYPE.getName(), Float.TYPE);
     38         primitiveTypes.put(Double.TYPE.getName(), Double.TYPE);
     39     }
     40 
     41     private String className;
     42 
     43     private String methodName;
     44 
     45     private String[] parameterTypeNames;
     46 
     47     public MethodSerializationWrapper(Method m) {
     48         className = m.getDeclaringClass().getName();
     49         methodName = m.getName();
     50 
     51         Class<?>[] parameterTypes = m.getParameterTypes();
     52 
     53         parameterTypeNames = new String[parameterTypes.length];
     54 
     55         for (int i = 0; i < parameterTypes.length; i++) {
     56             parameterTypeNames[i] = parameterTypes[i].getName();
     57         }
     58     }
     59 
     60     public Method getMethod() throws ClassNotFoundException,
     61             NoSuchMethodException {
     62         Class<?> clazz = Class.forName(className, true, Thread.currentThread()
     63                 .getContextClassLoader());
     64 
     65         Class<?>[] parameterTypes = new Class[parameterTypeNames.length];
     66 
     67         for (int i = 0; i < parameterTypeNames.length; i++) {
     68             Class<?> primitiveType = primitiveTypes.get(parameterTypeNames[i]);
     69             if (primitiveType != null) {
     70                 parameterTypes[i] = primitiveType;
     71             } else {
     72                 parameterTypes[i] = Class.forName(parameterTypeNames[i], true,
     73                         Thread.currentThread().getContextClassLoader());
     74             }
     75         }
     76 
     77         Method m = clazz.getMethod(methodName, parameterTypes);
     78 
     79         return m;
     80     }
     81 }
     82