1、 byte.class) Byte.class) String.class) /C语言实现有序集合intset结构/C语言制作动态链接库.so文件 public static void x(Object o) public static void x(int o) public static void x(Integer o)文件 Methods.java2KB(10) 9 10package canghailan.lang;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method; *
2、 author canghailan 2012-01-13 23:06public class Methods private static final MethodLookup publicLookup = new MethodLookup(1024); public static MethodLookup publicLookup() return publicLookup; public static Object invoke(Object object, String name, Object. params) throws NoSuchMethodException, Invoca
3、tionTargetException, IllegalAccessException Method method = publicLookup.findMethod( object.getClass(), name, MethodLookup.getParameterTypes(params) if (method != null) return method.invoke(object, params); throw new NoSuchMethodException(invokeToString(object.getClass(), name, params); public stati
4、c Object invokeStatic(Class clazz, String name, Object. params) clazz, return method.invoke(null, params); throw new NoSuchMethodException(invokeToString(clazz, name, params); private static String invokeToString(Class clazz, String method, Object. params) StringBuilder buffer = new StringBuilder();
5、 buffer.append(clazz.getName().append($); buffer.append(method).append( for (Object param : params) buffer.append(param).append(, buffer.setLength(buffer.length() - .length(); buffer.append() return buffer.toString();文件 Fields.java4KBimport java.lang.reflect.Field;import java.lang.reflect.Modifier;p
6、ublic class Fields private static final FieldLookup lookup = new FieldLookup(1024); public static FieldLookup lookup() return lookup; public static Object get(Object object, String field) Method getter = lookup.findGetter(object.getClass(), field); if (getter ! return getter.invoke(object); throw ne
7、w NoSuchMethodException(Could not find getter for + object.getClass().getName() + . + field); public static void set(Object object, String field, Object value) StringBuilder buffer = new StringBuilder(set.length() + field.length(); buffer.append().append(field) .setCharAt(.length(), Character.toUppe
8、rCase(field.charAt(0); Methods.invoke(object, buffer.toString(), value); public static Object directGet(Object object, String name) throws NoSuchFieldException, IllegalAccessException Field field = lookup.findField(object.getClass(), name); if (field ! return directGet(object, field); throw new NoSu
9、chFieldException(object.getClass().getName() + + name); public static Object directGet(Object object, Field field) throws IllegalAccessException if (field.isAccessible() return field.get(object); else field.setAccessible(true); Object value = field.get(object); field.setAccessible(false); return val
10、ue; public static void directSet(Object object, String name, Object value) directSet(object, field, value); public static void directSet(Object object, Field field, Object value) field.set(object, value); public static void copy(Object src, Object dst, String. fieldNames) throws NoSuchFieldException
11、, IllegalAccessException, InvocationTargetException, NoSuchMethodException if (src.getClass().equals(dst.getClass() for (String fieldName : fieldNames) Field field = lookup.findField(src.getClass(), fieldName); if (field = null) throw new NoSuchFieldException(src.getClass().getName() + + fieldName);
12、 directSet(dst, field, directGet(src, field); for (String name : set(dst, name, get(src, name); public static void copy(T src, T dst) throws IllegalAccessException for (Class clz = src.getClass(); clz != null; clz = clz.getSuperclass() for (Field field : clz.getDeclaredFields() / skip static and fin
13、al field if (Modifier.isStatic(field.getModifiers() | Modifier.isFinal(field.getModifiers() continue;文件 MethodLookup.java12KBimport java.util.*; * author canghailan 2012-02-23 20:31public class MethodLookup extends CachedLookup implements MemberLookup public static final Class EMPTY_CLASSES = new Cl
14、ass0; public MethodLookup(Map cache) super(cache); public MethodLookup(final int cacheSize) super(cacheSize); Override protected MethodsInfo doFind(Identifier key) return new MethodsInfo(key.getClazz(), key.getName(); public Method findMethod(Class 0) if (accept = null | methodInfo.isMorePrecise(acc
15、ept) accept = methodInfo; return accept = null ? null : accept.getMethod(); /* * linull = nullobject = object.getClass() public static Class getParameterTypes(Object. params) if (params = null) return null; if (params.length = 0) return EMPTY_CLASSES; Class types = new Classparams.length; for (int i
16、 = 0; i params.length; +i) typesi = paramsi = null ? paramsi.getClass(); return types; static class MethodsInfo static final MethodInfo EMPTY_METHODS = new MethodInfo0; final MethodInfo methods; MethodsInfo(Class clazz, String name) methods = generateMethodsInfo(clazz, name); MethodInfo generateMeth
17、odsInfo(Class int maxParameterLength = 0; MapInteger, List methodsInfoMap = new HashMap(); for (Method method : clazz.getMethods() if (method.getName().equals(name) int parameterLength = method.getParameterTypes().length; List methodInfoList = methodsInfoMap.get(parameterLength); if (methodInfoList
18、= null) methodInfoList = new LinkedList methodsInfoMap.put(parameterLength, methodInfoList); methodInfoList.add(new MethodInfo(method); maxParameterLength = Math.max(maxParameterLength, parameterLength); MethodInfo methods = new MethodInfomaxParameterLength + 1; methods.length; methodInfoList = meth
19、odsInfoMap.get(i); if (methodInfoList = null | methodInfoList.size() = 0) methodsi = EMPTY_METHODS; methodsi = methodInfoList.toArray(new MethodInfomethodInfoList.size(); return methods; MethodInfo getMethods(int parameterLength) if (parameterLength methods.length) return methodsparameterLength; ret
20、urn EMPTY_METHODS; public String toString() buffer.append(i).append(t) .append(Arrays.toString(methodsi).append(n static class MethodInfo final Method method; ParameterTypes parameterTypes; / cache MethodInfo(Method method) this.method = method; Method getMethod() return method; ParameterTypes getParamete
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2