一、前言
关于googleapi源码包中的org.apache.soap.util.MethodUtils、org.apache.soap.util.StringUtils方法及字符串工具类,获取类容器方法getConstructor、获取类的处理方法getMethod等。
二、源码说明
1.MethodUtils类
package org.apache.soap.util;@b@@b@import java.lang.reflect.Constructor;@b@import java.lang.reflect.Method;@b@import java.lang.reflect.Modifier;@b@import java.util.Enumeration;@b@import java.util.Vector;@b@@b@public class MethodUtils@b@{@b@ static boolean access$0(Class[] paramArrayOfClass1, Class[] paramArrayOfClass2)@b@ {@b@ return areMethodConvertable(paramArrayOfClass1, paramArrayOfClass2);@b@ }@b@@b@ static String access$1(Class paramClass, String paramString, Class[] paramArrayOfClass, boolean paramBoolean)@b@ {@b@ return callToString(paramClass, paramString, paramArrayOfClass, paramBoolean);@b@ }@b@@b@ private static boolean areMethodConvertable(Class[] paramArrayOfClass1, Class[] paramArrayOfClass2)@b@ {@b@ if (paramArrayOfClass1.length != paramArrayOfClass2.length)@b@ return false;@b@@b@ for (int i = 0; i < paramArrayOfClass1.length; ++i)@b@ if (!(isMethodConvertable(paramArrayOfClass1[i], paramArrayOfClass2[i])))@b@ return false;@b@@b@ return true;@b@ }@b@@b@ private static String callToString(Class paramClass, String paramString, Class[] paramArrayOfClass, boolean paramBoolean)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer();@b@ if (paramBoolean)@b@ localStringBuffer.append("static ");@b@ localStringBuffer.append(StringUtils.getClassName(paramClass));@b@ if (paramString != null)@b@ localStringBuffer.append(".").append(paramString);@b@ localStringBuffer.append("(");@b@ if ((paramArrayOfClass != null) && (paramArrayOfClass.length > 0))@b@ {@b@ localStringBuffer.append(StringUtils.getClassName(paramArrayOfClass[0]));@b@ for (int i = 1; i < paramArrayOfClass.length; ++i)@b@ localStringBuffer.append(",").append(StringUtils.getClassName(paramArrayOfClass[i]));@b@@b@ }@b@ else@b@ {@b@ localStringBuffer.append("[none]"); }@b@ localStringBuffer.append(")");@b@ return localStringBuffer.toString();@b@ }@b@@b@ static int entryGetModifiers(Object paramObject)@b@ {@b@ return ((paramObject instanceof Method) ? @b@ ((Method)paramObject).getModifiers() : @b@ ((Constructor)paramObject).getModifiers());@b@ }@b@@b@ static String entryGetName(Object paramObject)@b@ {@b@ return ((paramObject instanceof Method) ? @b@ ((Method)paramObject).getName() : @b@ ((Constructor)paramObject).getName());@b@ }@b@@b@ static Class[] entryGetParameterTypes(Object paramObject)@b@ {@b@ return ((paramObject instanceof Method) ? @b@ ((Method)paramObject).getParameterTypes() : @b@ ((Constructor)paramObject).getParameterTypes());@b@ }@b@@b@ static String entryToString(Object paramObject)@b@ {@b@ return ((paramObject instanceof Method) ? @b@ ((Method)paramObject).toString() : @b@ ((Constructor)paramObject).toString());@b@ }@b@@b@ public static Constructor getConstructor(Class paramClass, Class[] paramArrayOfClass)@b@ throws SecurityException, NoSuchMethodException@b@ {@b@ return ((Constructor)getEntryPoint(paramClass, null, paramArrayOfClass, true));@b@ }@b@@b@ private static Object getEntryPoint(Class paramClass, String paramString, Class[] paramArrayOfClass, boolean paramBoolean)@b@ throws SecurityException, NoSuchMethodException@b@ {@b@ Object localObject2;@b@ Object localObject1 = null;@b@ try@b@ {@b@ if (paramString != null)@b@ {@b@ localObject1 = paramClass.getMethod(paramString, paramArrayOfClass);@b@ if ((paramBoolean) && @b@ (!(Modifier.isStatic(entryGetModifiers(localObject1)))))@b@ {@b@ throw @b@ new NoSuchMethodException(callToString(paramClass, @b@ paramString, @b@ paramArrayOfClass, @b@ paramBoolean) + @b@ " resolved to instance " + localObject1);@b@ }@b@ return localObject1;@b@ }@b@@b@ return paramClass.getConstructor(paramArrayOfClass);@b@ }@b@ catch (NoSuchMethodException localNoSuchMethodException)@b@ {@b@ if ((paramArrayOfClass == null) || (paramArrayOfClass.length == 0))@b@ {@b@ throw @b@ new NoSuchMethodException(callToString(paramClass, @b@ paramString, @b@ paramArrayOfClass, @b@ paramBoolean) + @b@ " not found.");@b@ }@b@@b@ }@b@@b@ if (paramString != null)@b@ {@b@ localObject2 = paramClass.getMethods();@b@ }@b@ else@b@ {@b@ localObject2 = paramClass.getConstructors();@b@ }@b@ if (localObject2.length == 0)@b@ {@b@ throw new NoSuchMethodException("No methods!");@b@ }@b@@b@ MoreSpecific localMoreSpecific = new MoreSpecific();@b@ for (int i = 0; i < localObject2.length; ++i)@b@ {@b@ Object localObject3 = localObject2[i];@b@@b@ if ((Modifier.isPublic(entryGetModifiers(localObject3))) && @b@ (((paramString == null) || (entryGetName(localObject3).equals(paramString)))) && @b@ (areMethodConvertable(entryGetParameterTypes(localObject3), paramArrayOfClass)))@b@ {@b@ localMoreSpecific.addItem(localObject3);@b@ }@b@@b@ }@b@@b@ localObject1 = localMoreSpecific.getMostSpecific(paramClass, paramString, paramArrayOfClass, paramBoolean);@b@@b@ if (localObject1 == null)@b@ {@b@ throw new NoSuchMethodException(callToString(paramClass, @b@ paramString, @b@ paramArrayOfClass, @b@ paramBoolean) + @b@ " -- no signature match");@b@ }@b@@b@ if ((paramString != null) && @b@ (paramBoolean) && @b@ (!(Modifier.isStatic(entryGetModifiers(localObject1)))))@b@ {@b@ throw new NoSuchMethodException(callToString(paramClass, @b@ paramString, @b@ paramArrayOfClass, @b@ paramBoolean) + @b@ " resolved to instance: " + localObject1);@b@ }@b@@b@ return localObject1;@b@ }@b@@b@ public static Method getMethod(Class paramClass, String paramString, Class[] paramArrayOfClass, boolean paramBoolean)@b@ throws SecurityException, NoSuchMethodException@b@ {@b@ return ((Method)getEntryPoint(paramClass, paramString, paramArrayOfClass, paramBoolean));@b@ }@b@@b@ public static Method getMethod(Object paramObject, String paramString, Class[] paramArrayOfClass)@b@ throws SecurityException, NoSuchMethodException@b@ {@b@ boolean bool = paramObject instanceof Class;@b@ return getMethod((bool) ? (Class)paramObject : paramObject.getClass(), @b@ paramString, paramArrayOfClass, bool);@b@ }@b@@b@ private static boolean isAssignmentConvertable(Class paramClass1, Class paramClass2)@b@ {@b@ return @b@ (((paramClass2.equals(Integer.TYPE)) && ((@b@ (paramClass1.equals(Byte.TYPE)) || @b@ (paramClass1.equals(Short.TYPE)) || @b@ (paramClass1.equals(Character.TYPE))))) || @b@ (isMethodConvertable(paramClass1, paramClass2)));@b@ }@b@@b@ private static boolean isMethodConvertable(Class paramClass1, Class paramClass2)@b@ {@b@ if (paramClass1.equals(paramClass2)) {@b@ return true;@b@ }@b@@b@ if (paramClass2 == null)@b@ {@b@ return (paramClass1.isPrimitive() ^ true);@b@ }@b@@b@ do@b@ {@b@ if (!(paramClass2.isArray())) {@b@ return false;@b@ }@b@@b@ paramClass1 = paramClass1.getComponentType();@b@ paramClass2 = paramClass2.getComponentType();@b@ }@b@ while (paramClass1.isArray());@b@@b@ if (paramClass2.isArray()) {@b@ return false;@b@ }@b@@b@ if (paramClass1.isAssignableFrom(paramClass2)) {@b@ return true;@b@ }@b@@b@ if ((paramClass1.equals(Void.TYPE)) || (paramClass1.equals(Boolean.TYPE)) || @b@ (paramClass2.equals(Void.TYPE)) || (paramClass2.equals(Boolean.TYPE)))@b@ return false;@b@@b@ Class[] arrayOfClass = { Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, @b@ Long.TYPE, Float.TYPE, Double.TYPE };@b@@b@ for (int i = 0; i < arrayOfClass.length; ++i)@b@ if (paramClass1.equals(arrayOfClass[i]))@b@ break;@b@ if (i >= arrayOfClass.length)@b@ return false;@b@@b@ for (int j = 0; j < arrayOfClass.length; ++j)@b@ if (paramClass2.equals(arrayOfClass[j]))@b@ break;@b@ if (j >= arrayOfClass.length) {@b@ return false;@b@ }@b@@b@ return ((j < i) && (((j != 0) || (i > 2))));@b@ }@b@@b@ private static class MoreSpecific extends Vector@b@ {@b@ void addItem(Object paramObject)@b@ {@b@ if (super.size() == 0) {@b@ super.addElement(paramObject);@b@ }@b@ else {@b@ Class[] arrayOfClass1 = MethodUtils.entryGetParameterTypes(paramObject);@b@ int i = 1;@b@ Enumeration localEnumeration = super.elements();@b@ while ((i & localEnumeration.hasMoreElements()) != 0)@b@ {@b@ Object localObject = localEnumeration.nextElement();@b@@b@ Class[] arrayOfClass2 = MethodUtils.entryGetParameterTypes(localObject);@b@ if (MethodUtils.access$0(arrayOfClass2, arrayOfClass1))@b@ super.removeElement(localObject);@b@ else if (MethodUtils.access$0(arrayOfClass1, arrayOfClass2))@b@ i = 0;@b@ }@b@@b@ if (i != 0)@b@ super.addElement(paramObject);@b@ }@b@ }@b@@b@ Object getMostSpecific(Class paramClass, String paramString, Class[] paramArrayOfClass, boolean paramBoolean)@b@ throws NoSuchMethodException@b@ {@b@ if (super.size() == 1)@b@ return super.firstElement();@b@ if (super.size() > 1)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer();@b@ Enumeration localEnumeration = super.elements();@b@ localStringBuffer.append(localEnumeration.nextElement());@b@ while (localEnumeration.hasMoreElements())@b@ localStringBuffer.append(" and ").append(localEnumeration.nextElement());@b@ throw new NoSuchMethodException(MethodUtils.access$1(paramClass, @b@ paramString, @b@ paramArrayOfClass, @b@ paramBoolean) + @b@ " is ambiguous. It matches " + @b@ localStringBuffer.toString());@b@ }@b@ return null;@b@ }@b@ }@b@}
2.StringUtils类
package org.apache.soap.util;@b@@b@import java.io.File;@b@import java.io.FileNotFoundException;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.Reader;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@@b@public class StringUtils@b@{@b@ public static final String lineSeparator = System.getProperty("line.separator", "\n");@b@ public static String URI_SEPARATION_CHAR = "@";@b@@b@ public static String getClassName(Class paramClass)@b@ {@b@ String str = paramClass.getName();@b@@b@ return ((paramClass.isArray()) ? parseDescriptor(str) : str);@b@ }@b@@b@ public static Reader getContentAsReader(URL paramURL)@b@ throws SecurityException, IllegalArgumentException, IOException@b@ {@b@ if (paramURL == null)@b@ {@b@ throw new IllegalArgumentException("URL cannot be null.");@b@ }@b@@b@ try@b@ {@b@ Object localObject = paramURL.getContent();@b@@b@ if (localObject == null)@b@ {@b@ throw new IllegalArgumentException("No content.");@b@ }@b@@b@ if (localObject instanceof InputStream)@b@ {@b@ InputStreamReader localInputStreamReader = new InputStreamReader((InputStream)localObject);@b@@b@ if (localInputStreamReader.ready())@b@ {@b@ return localInputStreamReader;@b@ }@b@@b@ throw new FileNotFoundException();@b@ }@b@@b@ throw new IllegalArgumentException(@b@ "This URL points to a: " + @b@ getClassName(localObject.getClass()));@b@ }@b@ catch (SecurityException localSecurityException)@b@ {@b@ throw new SecurityException("Your JVM's SecurityManager has disallowed this.");@b@ }@b@ catch (FileNotFoundException localFileNotFoundException)@b@ {@b@ throw new FileNotFoundException("This file was not found: " + paramURL);@b@ }@b@ }@b@@b@ public static String getContentAsString(URL paramURL)@b@ throws SecurityException, IllegalArgumentException, IOException@b@ {@b@ return IOUtils.getStringFromReader(getContentAsReader(paramURL));@b@ }@b@@b@ public static URL getURL(URL paramURL, String paramString)@b@ throws MalformedURLException@b@ {@b@ return getURL(paramURL, paramString, 1);@b@ }@b@@b@ private static URL getURL(URL paramURL, String paramString, int paramInt)@b@ throws MalformedURLException@b@ {@b@ URL localURL = null;@b@ try@b@ {@b@ localURL = new URL(paramURL, paramString);@b@ try@b@ {@b@ localURL.openStream();@b@ }@b@ catch (IOException localIOException1)@b@ {@b@ throw new MalformedURLException("This file was not found: " + localURL);@b@ }@b@ }@b@ catch (MalformedURLException localMalformedURLException)@b@ {@b@ localURL = new URL("file", "", paramString);@b@ try@b@ {@b@ localURL.openStream();@b@ }@b@ catch (IOException localIOException2)@b@ {@b@ if (paramURL != null)@b@ {@b@ String str1 = paramURL.getFile();@b@ String str2 = new File(str1).getParent();@b@@b@ if ((str2 != null) && (paramInt < 3))@b@ {@b@ return getURL(new URL("file", "", str2 + '/'), @b@ paramString, @b@ paramInt + 1);@b@ }@b@ }@b@@b@ throw new MalformedURLException("This file was not found: " + localURL);@b@ }@b@ }@b@@b@ return localURL;@b@ }@b@@b@ private static String parseDescriptor(String paramString)@b@ {@b@ char[] arrayOfChar = paramString.toCharArray();@b@ int i = 0;@b@ int j = 0;@b@@b@ while (arrayOfChar[j] == '[')@b@ {@b@ ++i;@b@ ++j;@b@ }@b@@b@ StringBuffer localStringBuffer = new StringBuffer();@b@@b@ switch (arrayOfChar[(j++)]) { case 'B':@b@ localStringBuffer.append("byte");@b@ break;@b@ case 'C':@b@ localStringBuffer.append("char");@b@ break;@b@ case 'D':@b@ localStringBuffer.append("double");@b@ break;@b@ case 'F':@b@ localStringBuffer.append("float");@b@ break;@b@ case 'I':@b@ localStringBuffer.append("int");@b@ break;@b@ case 'J':@b@ localStringBuffer.append("long");@b@ break;@b@ case 'S':@b@ localStringBuffer.append("short");@b@ break;@b@ case 'Z':@b@ localStringBuffer.append("boolean");@b@ break;@b@ case 'L':@b@ localStringBuffer.append(arrayOfChar, @b@ j, arrayOfChar.length - j - 1);@b@ case 'E':@b@ case 'G':@b@ case 'H':@b@ case 'K':@b@ case 'M':@b@ case 'N':@b@ case 'O':@b@ case 'P':@b@ case 'Q':@b@ case 'R':@b@ case 'T':@b@ case 'U':@b@ case 'V':@b@ case 'W':@b@ case 'X':@b@ case 'Y': } for (j = 0; j < i; ++j)@b@ localStringBuffer.append("[]");@b@@b@ return localStringBuffer.toString();@b@ }@b@@b@ public static String parseFullTargetObjectURI(String paramString)@b@ {@b@ if (paramString == null) return null;@b@ int i = paramString.indexOf(URI_SEPARATION_CHAR);@b@ if ((paramString != null) && (i != -1))@b@ return paramString.substring(0, i);@b@@b@ return paramString;@b@ }@b@}