View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class MethodUtil 3 * 4 * Copyright (c), 2002, Masahiro Takatsuka. 5 * All Rights Researved. 6 * 7 * Original Author: Masahiro Takatsuka (masa@jbeans.net) 8 * $Author: takatsukam $ 9 * 10 * $Date: 2003/07/25 04:51:46 $ 11 * 12 * $Id: MethodUtil.java,v 1.1.1.1 2003/07/25 04:51:46 takatsukam Exp $ 13 * 14 * Reference: Document no: 15 * ___ ___ 16 * 17 * To Do: 18 * ___ 19 * 20 ------------------------------------------------------------------- */ 21 22 /* --------------------------- Package ---------------------------- */ 23 package net.jbeans.lang.reflect; 24 25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */ 26 import java.io.*; 27 import java.lang.reflect.*; 28 29 import net.jbeans.lang.*; 30 31 /*==================================================================== 32 Implementation of class MethodUtil 33 ====================================================================*/ 34 /*** 35 * MethodUtil is a utility class, which serialize 36 * the java.lang.reflect.Method object. 37 * 38 * @version $Revision: 1.1.1.1 $ 39 * @author Masahiro Takatsuka (masa@jbeans.net) 40 */ 41 42 public class MethodUtil { 43 /*** 44 * Constructor. No one needs to instantiate a MethodUtil. 45 */ 46 private MethodUtil() { 47 super(); 48 } 49 50 /*** 51 * Writes all pieces of information required to re-construct Method object. 52 */ 53 public final static void writeMethod(Method method, ObjectOutputStream s) throws IOException { 54 // get all information need to be serialized. 55 Class declaringClass = null; 56 String methodName = null; 57 Class[] parameterTypes = null; 58 if (method != null) { 59 declaringClass = method.getDeclaringClass(); 60 methodName = method.getName(); 61 parameterTypes = method.getParameterTypes(); 62 } else { 63 declaringClass = Object.class; 64 methodName = "dummy"; 65 parameterTypes = new Class[] {Object.class}; 66 } 67 // return type is not needed to be serialized. 68 // Class returnType = method.getReturnType(); 69 // exception types are not needed to be serialized. 70 // Class[] exceptionTypes = method.getExceptionTypes(); 71 // modifiers is not needed to be serialized. 72 // int modifiers = method.getModifiers(); 73 74 // string representation of returntype 75 // String returnTypeName = (returnType != null) ? 76 // returnType.getName() : "null"; 77 78 // string representation of parameterTypes. 79 String[] parameterTypeNames = ClassUtil.getClassNames(parameterTypes); 80 81 // first write declaring class 82 s.writeObject(declaringClass); 83 // then method name 84 s.writeObject(methodName); 85 // then parameterTypes 86 s.writeObject(parameterTypeNames); 87 } 88 89 /*** 90 * Read serialized information and re-construct a method object. 91 */ 92 public final static Method readMethod(ObjectInputStream s) throws ClassNotFoundException, IOException { 93 94 // first read declaring class. 95 Class declaringClass = (Class) s.readObject(); 96 // then read method name 97 String methodName = (String) s.readObject(); 98 // then read parameterTypes 99 String[] parameterTypeNames = (String[]) s.readObject(); 100 101 // get Class object for parameterTypes 102 Class[] parameterTypes = ClassUtil.getClasses(parameterTypeNames); 103 104 // construc a Method. 105 Method method = null; 106 try { 107 method = declaringClass.getMethod(methodName, parameterTypes); 108 } catch (Exception e) { 109 /* 110 The requested method was not found. 111 We assume that "null" method must have been serialized. 112 So, just return "null"; 113 */ 114 } 115 return method; 116 } 117 } 118

This page was automatically generated by Maven