1、开源零听小清新范儿十足的音乐播放器代码案例angularbase64.jsmodule支持中文,AngularJS学习-服务Service AngularJS学习-服务Serviceangular实现的form自动布局,算法的简单实现arduino距离传感器,arduino小车前进左右转代码,array_change_key_case:返回字符串键名全,array_chunk:将一个数组分解成包含多个一代码 Java代码 package test;import canghailan.lang.Methods;/* * author canghailan 2012-03-12 21:53 */p
2、ublic class MethodLookupTest public static void main(String args) throws Exception System.out.println(Methods.publicLookup().findMethod( MethodLookupTest.class, x, Object.class) ); / public static void test.MethodLookupTest.x(java.lang.Object) System.out.println(Methods.publicLookup().findMethod( Me
3、thodLookupTest.class, x, int.class) ); / public static void test.MethodLookupTest.x(int) System.out.println(Methods.publicLookup().findMethod( MethodLookupTest.class, x, Integer.class) ); / public static void test.MethodLookupTest.x(java.lang.Integer) System.out.println(Methods.publicLookup().findMe
4、thod( MethodLookupTest.class, x, byte.class) ); / public static void test.MethodLookupTest.x(int) System.out.println(Methods.publicLookup().findMethod( MethodLookupTest.class, x, Byte.class) ); / public static void test.MethodLookupTest.x(java.lang.Object) System.out.println(Methods.publicLookup().f
5、indMethod( MethodLookupTest.class, x, String.class) ); / public static void test.MethodLookupTest.x(java.lang.Object) /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
6、 java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;/* * author canghailan 2012-01-13 23:06 */public class Methods private static final MethodLookup publicLookup = new MethodLookup(1024); public static MethodLookup publicLookup() return publicLookup; public static Object inv
7、oke(Object object, String name, Object. params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException Method method = publicLookup.findMethod( object.getClass(), name, MethodLookup.getParameterTypes(params) ); if (method != null) return method.invoke(object, params); throw
8、new NoSuchMethodException(invokeToString(object.getClass(), name, params); public static Object invokeStatic(Class clazz, String name, Object. params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException Method method = publicLookup.findMethod( clazz, name, MethodLookup.ge
9、tParameterTypes(params) ); if (method != null) 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(); buffer.append(clazz.get
10、Name().append($); buffer.append(method).append(); for (Object param : params) buffer.append(param).append(, ); buffer.setLength(buffer.length() - , .length(); buffer.append(); return buffer.toString(); 文件 Fields.java4KB(10) 9 10package canghailan.lang;import java.lang.reflect.Field;import java.lang.
11、reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;/* * author canghailan 2012-01-13 23:06 */public class Fields private static final FieldLookup lookup = new FieldLookup(1024); public static FieldLookup lookup() return lookup; public static Object ge
12、t(Object object, String field) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException Method getter = lookup.findGetter(object.getClass(), field); if (getter != null) return getter.invoke(object); throw new NoSuchMethodException(Could not find getter for + object.getClass().
13、getName() + . + field); public static void set(Object object, String field, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException StringBuilder buffer = new StringBuilder(set.length() + field.length(); buffer.append(set).append(field) .setCharAt(set.length(),
14、Character.toUpperCase(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 != null) return directGet(object,
15、 field); throw new NoSuchFieldException(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.setAcce
16、ssible(false); return value; public static void directSet(Object object, String name, Object value) throws NoSuchFieldException, IllegalAccessException Field field = lookup.findField(object.getClass(), name); if (field != null) directSet(object, field, value); throw new NoSuchFieldException(object.g
17、etClass().getName() + . + name); public static void directSet(Object object, Field field, Object value) throws IllegalAccessException if (field.isAccessible() field.set(object, value); else field.setAccessible(true); field.set(object, value); field.setAccessible(false); public static void copy(Objec
18、t src, Object dst, String. fieldNames) throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException if (src.getClass().equals(dst.getClass() for (String fieldName : fieldNames) Field field = lookup.findField(src.getClass(), fieldName); if (field = null) throw
19、 new NoSuchFieldException(src.getClass().getName() + . + fieldName); directSet(dst, field, directGet(src, field); else for (String name : fieldNames) set(dst, name, get(src, name); public static void copy(T src, T dst) throws IllegalAccessException for (Class clz = src.getClass(); clz != null; clz =
20、 clz.getSuperclass() for (Field field : clz.getDeclaredFields() / skip static and final field if (Modifier.isStatic(field.getModifiers() | Modifier.isFinal(field.getModifiers() continue; directSet(dst, field, directGet(src, field); 文件 MethodLookup.java12KB(10) 9 10package canghailan.lang;import java
21、.lang.reflect.Method;import java.util.*;/* * author canghailan 2012-02-23 20:31 */public class MethodLookup extends CachedLookup implements MemberLookup public static final Class EMPTY_CLASSES = new Class0; public MethodLookup(Map cache) super(cache); public MethodLookup(final int cacheSize) super(c
22、acheSize); Override protected MethodsInfo doFind(Identifier key) return new MethodsInfo(key.getClazz(), key.getName(); public Method findMethod(Class clazz, String name, Class. actual) MethodInfo accept = null; for (MethodInfo methodInfo : find(new Identifier(clazz, name).getMethods(actual.length) i
23、nt diff = methodInfo.accept(actual); if (diff = 0) return methodInfo.getMethod(); if (diff 0) if (accept = null | methodInfo.isMorePrecise(accept) accept = methodInfo; return accept = null ? null : accept.getMethod(); /* * * null = null * object = object.getClass() * */ public static Class getParame
24、terTypes(Object. params) if (params = null) return null; if (params.length = 0) return EMPTY_CLASSES; Class types = new Classparams.length; for (int i = 0; i params.length; +i) typesi = paramsi = null ? null : paramsi.getClass(); return types; static class MethodsInfo static final MethodInfo EMPTY_M
25、ETHODS = new MethodInfo0; final MethodInfo methods; MethodsInfo(Class clazz, String name) methods = generateMethodsInfo(clazz, name); MethodInfo generateMethodsInfo(Class clazz, String name) int maxParameterLength = 0; MapInteger, List methodsInfoMap = new HashMap(); for (Method method : clazz.getMe
26、thods() if (method.getName().equals(name) int parameterLength = method.getParameterTypes().length; List methodInfoList = methodsInfoMap.get(parameterLength); if (methodInfoList = null) methodInfoList = new LinkedList(); methodsInfoMap.put(parameterLength, methodInfoList); methodInfoList.add(new Meth
27、odInfo(method); maxParameterLength = Math.max(maxParameterLength, parameterLength); MethodInfo methods = new MethodInfomaxParameterLength + 1; for (int i = 0; i methods.length; +i) List methodInfoList = methodsInfoMap.get(i); if (methodInfoList = null | methodInfoList.size() = 0) methodsi = EMPTY_ME
28、THODS; else methodsi = methodInfoList.toArray(new MethodInfomethodInfoList.size(); return methods; MethodInfo getMethods(int parameterLength) if (parameterLength methods.length) return methodsparameterLength; return EMPTY_METHODS; public String toString() StringBuilder buffer = new StringBuilder();
29、for (int i = 0; i methods.length; +i) buffer.append(i).append(t) .append(Arrays.toString(methodsi).append(n); return buffer.toString(); 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