Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.apache.xmlrpc.metadata;
00020
00021 import java.io.Serializable;
00022 import java.lang.reflect.Method;
00023 import java.util.ArrayList;
00024 import java.util.Calendar;
00025 import java.util.Date;
00026 import java.util.List;
00027 import java.util.Map;
00028
00029 import org.apache.xmlrpc.XmlRpcException;
00030 import org.w3c.dom.Node;
00031
00032
00036 public class Util {
00041 private static final Class jaxbElementClass;
00042 static {
00043 Class c;
00044 try {
00045 c = Class.forName("javax.xml.bind.Element");
00046 } catch (ClassNotFoundException e) {
00047 c = null;
00048 }
00049 jaxbElementClass = c;
00050 }
00051
00058 public static String getSignatureType(Class pType) {
00059 if (pType == Integer.TYPE || pType == Integer.class)
00060 return "int";
00061 if (pType == Double.TYPE || pType == Double.class)
00062 return "double";
00063 if (pType == Boolean.TYPE || pType == Boolean.class)
00064 return "boolean";
00065 if (pType == String.class)
00066 return "string";
00067 if (Object[].class.isAssignableFrom(pType)
00068 || List.class.isAssignableFrom(pType))
00069 return "array";
00070 if (Map.class.isAssignableFrom(pType))
00071 return "struct";
00072 if (Date.class.isAssignableFrom(pType)
00073 || Calendar.class.isAssignableFrom(pType))
00074 return "dateTime.iso8601";
00075 if (pType == byte[].class)
00076 return "base64";
00077
00078
00079 if (pType == void.class)
00080 return "ex:nil";
00081 if (pType == Byte.TYPE || pType == Byte.class)
00082 return "ex:i1";
00083 if (pType == Short.TYPE || pType == Short.class)
00084 return "ex:i2";
00085 if (pType == Long.TYPE || pType == Long.class)
00086 return "ex:i8";
00087 if (pType == Float.TYPE || pType == Float.class)
00088 return "ex:float";
00089 if (Node.class.isAssignableFrom(pType))
00090 return "ex:node";
00091 if (jaxbElementClass != null
00092 && jaxbElementClass.isAssignableFrom(pType)) {
00093 return "ex:jaxbElement";
00094 }
00095 if (Serializable.class.isAssignableFrom(pType))
00096 return "base64";
00097
00098
00099 return null;
00100 }
00101
00108 public static String[][] getSignature(Method[] pMethods) {
00109 final List result = new ArrayList();
00110 for (int i = 0; i < pMethods.length; i++) {
00111 String[] sig = getSignature(pMethods[i]);
00112 if (sig != null) {
00113 result.add(sig);
00114 }
00115 }
00116 return (String[][]) result.toArray(new String[result.size()][]);
00117 }
00118
00125 public static String[] getSignature(Method pMethod) {
00126 Class[] paramClasses = pMethod.getParameterTypes();
00127 String[] sig = new String[paramClasses.length + 1];
00128 String s = getSignatureType(pMethod.getReturnType());
00129 if (s == null) {
00130 return null;
00131 }
00132 sig[0] = s;
00133 for (int i = 0; i < paramClasses.length; i++) {
00134 s = getSignatureType(paramClasses[i]);
00135 if (s == null) {
00136 return null;
00137 }
00138 sig[i+1] = s;
00139 }
00140 return sig;
00141 }
00142
00146 public static String getMethodHelp(Class pClass, Method[] pMethods) {
00147 final List result = new ArrayList();
00148 for (int i = 0; i < pMethods.length; i++) {
00149 String help = getMethodHelp(pClass, pMethods[i]);
00150 if (help != null) {
00151 result.add(help);
00152 }
00153 }
00154 switch (result.size()) {
00155 case 0:
00156 return null;
00157 case 1:
00158 return (String) result.get(0);
00159 default:
00160 StringBuffer sb = new StringBuffer();
00161 for (int i = 0; i < result.size(); i++) {
00162 sb.append(i+1);
00163 sb.append(": ");
00164 sb.append(result.get(i));
00165 sb.append("\n");
00166 }
00167 return sb.toString();
00168 }
00169 }
00170
00174 public static String getMethodHelp(Class pClass, Method pMethod) {
00175 StringBuffer sb = new StringBuffer();
00176 sb.append("Invokes the method ");
00177 sb.append(pClass.getName());
00178 sb.append(".");
00179 sb.append(pMethod.getName());
00180 sb.append("(");
00181 Class[] paramClasses = pMethod.getParameterTypes();
00182 for (int i = 0; i < paramClasses.length; i++) {
00183 if (i > 0) {
00184 sb.append(", ");
00185 }
00186 sb.append(paramClasses[i].getName());
00187 }
00188 sb.append(").");
00189 return sb.toString();
00190 }
00191
00195 public static String getSignature(Object[] args) {
00196 StringBuffer sb = new StringBuffer();
00197 for (int i = 0; i < args.length; i++) {
00198 if (i > 0) {
00199 sb.append(", ");
00200 }
00201 if (args[i] == null) {
00202 sb.append("null");
00203 } else {
00204 sb.append(args[i].getClass().getName());
00205 }
00206 }
00207 return sb.toString();
00208 }
00209
00213 public static Object newInstance(Class pClass) throws XmlRpcException {
00214 try {
00215 return pClass.newInstance();
00216 } catch (InstantiationException e) {
00217 throw new XmlRpcException("Failed to instantiate class " + pClass.getName(), e);
00218 } catch (IllegalAccessException e) {
00219 throw new XmlRpcException("Illegal access when instantiating class " + pClass.getName(), e);
00220 }
00221 }
00222 }