📄 reflect.java
字号:
package aegis;public class Reflect { public static Class getFieldType(Class declaringClass, String descriptor) { switch (descriptor.charAt(0)) { case 'Z': return Boolean.TYPE; case 'B': return Byte.TYPE; case 'C': return Character.TYPE; case 'S': return Short.TYPE; case 'I': return Integer.TYPE; case 'J': return Long.TYPE; case 'F': return Float.TYPE; case 'D': return Double.TYPE; case 'L': try { String classname = descriptor.substring(1, descriptor.length() - 1). replace('/', '.'); return Class.forName(classname, false, declaringClass.getClassLoader()); } catch (ClassNotFoundException ex) { throw new NoClassDefFoundError(); } case '[': try { String classname = descriptor.replace('/', '.'); return Class.forName(classname, false, declaringClass.getClassLoader()); } catch (ClassNotFoundException ex) { throw new NoClassDefFoundError(); } default: throw new InternalError(); } } public static Class getReturnType(Class declaringClass, String descriptor){ descriptor = descriptor.substring(descriptor.indexOf(')') + 1); if (descriptor.charAt(0) == 'V') return Void.TYPE; return getFieldType(declaringClass, descriptor); } public static Class[] getParameterTypes(Class declaringClass, String descriptor) { int capacity = 8; int size = 0; Class[] params = new Class[capacity]; for (int i = 1; descriptor.charAt(i) != ')'; i++, size++) { if (size == capacity) { capacity *= 2; Class[] newParams = new Class[capacity]; for (int j = 0; j < size; j++) newParams[j] = params[j]; params = newParams; } switch (descriptor.charAt(i)) { case 'Z': params[size] = Boolean.TYPE; break; case 'B': params[size] = Byte.TYPE; break; case 'C': params[size] = Character.TYPE; break; case 'S': params[size] = Short.TYPE; break; case 'I': params[size] = Integer.TYPE; break; case 'J': params[size] = Long.TYPE; break; case 'F': params[size] = Float.TYPE; break; case 'D': params[size] = Double.TYPE; break; case 'L': try { int j = descriptor.indexOf(';', i); String classname = descriptor.substring(i+1, j).replace('/', '.'); params[size] = Class.forName(classname, false, declaringClass.getClassLoader()); i = j; } catch (ClassNotFoundException ex) { throw new NoClassDefFoundError(); } break; case '[': try { int j; for (j = i; descriptor.charAt(j) == '['; j++) ; if (descriptor.charAt(j) == 'L') j = descriptor.indexOf(';', j); String classname = descriptor.substring(i, j+1).replace('/', '.'); params[size] = Class.forName(classname, false, declaringClass.getClassLoader()); i = j; } catch (ClassNotFoundException ex) { throw new NoClassDefFoundError(); } break; default: throw new InternalError(); } } Class[] result = new Class[size]; for (int i = 0; i < size; i++) result[i] = params[i]; return result; } public static boolean widenToBoolean(Class type, Object value) throws IllegalArgumentException { if (type == Boolean.TYPE) return ((Boolean) value).booleanValue(); else throw new IllegalArgumentException(); } public static byte widenToByte(Class type, Object value) throws IllegalArgumentException { if (type == Byte.TYPE) return ((Byte) value).byteValue(); else throw new IllegalArgumentException(); } public static char widenToCharacter(Class type, Object value) throws IllegalArgumentException { if (type == Character.TYPE) return ((Character) value).charValue(); else throw new IllegalArgumentException(); } public static short widenToShort(Class type, Object value) throws IllegalArgumentException { if (type == Short.TYPE) return ((Short) value).shortValue(); else if (type == Byte.TYPE) return (short) ((Byte) value).byteValue(); else throw new IllegalArgumentException(); } public static int widenToInteger(Class type, Object value) throws IllegalArgumentException { if (type == Integer.TYPE) return ((Integer) value).intValue(); else if (type == Short.TYPE) return (int) ((Short) value).shortValue(); else if (type == Character.TYPE) return (int) ((Character) value).charValue(); else if (type == Byte.TYPE) return (int) ((Byte) value).byteValue(); else throw new IllegalArgumentException(); } public static long widenToLong(Class type, Object value) throws IllegalArgumentException { if (type == Long.TYPE) return ((Long) value).longValue(); else if (type == Integer.TYPE) return (long) ((Integer) value).intValue(); else if (type == Short.TYPE) return (long) ((Short) value).shortValue(); else if (type == Character.TYPE) return (long) ((Character) value).charValue(); else if (type == Byte.TYPE) return (long) ((Byte) value).byteValue(); else throw new IllegalArgumentException(); } public static float widenToFloat(Class type, Object value) throws IllegalArgumentException { if (type == Float.TYPE) return ((Float) value).floatValue(); else if (type == Long.TYPE) return (float) ((Long) value).longValue(); else if (type == Integer.TYPE) return (float) ((Integer) value).intValue(); else if (type == Short.TYPE) return (float) ((Short) value).shortValue(); else if (type == Character.TYPE) return (float) ((Character) value).charValue(); else if (type == Byte.TYPE) return (float) ((Byte) value).byteValue(); else throw new IllegalArgumentException(); } public static double widenToDouble(Class type, Object value) throws IllegalArgumentException { if (type == Double.TYPE) return ((Double) value).doubleValue(); else if (type == Float.TYPE) return (double) ((Float) value).floatValue(); else if (type == Long.TYPE) return (double) ((Long) value).longValue(); else if (type == Integer.TYPE) return (double) ((Integer) value).intValue(); else if (type == Short.TYPE) return (double) ((Short) value).shortValue(); else if (type == Character.TYPE) return (double) ((Character) value).charValue(); else if (type == Byte.TYPE) return (double) ((Byte) value).byteValue(); else throw new IllegalArgumentException(); } public static Object widen(Object obj, Class type) { if (type.isPrimitive()) { if (obj == null) throw new NullPointerException(); if (type == Boolean.TYPE) { if (! (obj instanceof Boolean)) throw new IllegalArgumentException(); } else if (type == Byte.TYPE) { if (! (obj instanceof Byte)) throw new IllegalArgumentException(); } else if (type == Character.TYPE) { if (! (obj instanceof Character)) throw new IllegalArgumentException(); } else if (type == Short.TYPE) { if (! (obj instanceof Short)) { if (obj instanceof Byte) obj = new Short(((Number) obj).shortValue()); else throw new IllegalArgumentException(); } } else if (type == Integer.TYPE) { if (! (obj instanceof Integer)) { if (obj instanceof Character) obj = new Integer((int) ((Character) obj).charValue()); else if (obj instanceof Byte || obj instanceof Short) obj = new Integer(((Number) obj).intValue()); else throw new IllegalArgumentException(); } } else if (type == Long.TYPE) { if (! (obj instanceof Long)) { if (obj instanceof Character) obj = new Long((long) ((Character) obj).charValue()); else if (obj instanceof Byte || obj instanceof Short || obj instanceof Integer) obj = new Long(((Number) obj).longValue()); else throw new IllegalArgumentException(); } } else if (type == Float.TYPE) { if (! (obj instanceof Float)) { if (obj instanceof Character) obj = new Float((float) ((Character) obj).charValue()); else if (obj instanceof Byte || obj instanceof Short || obj instanceof Integer || obj instanceof Long) obj = new Float(((Number) obj).floatValue()); else throw new IllegalArgumentException(); } } else if (type == Double.TYPE) { if (! (obj instanceof Double)) { if (obj instanceof Character) obj = new Double((double) ((Character) obj).charValue()); else if (obj instanceof Byte || obj instanceof Short || obj instanceof Integer || obj instanceof Long || obj instanceof Float) obj = new Double(((Number) obj).doubleValue()); else throw new IllegalArgumentException(); } } else { throw new InternalError(); } } else { if (obj != null && ! type.isInstance(obj)) throw new IllegalArgumentException(); } return obj; } public static Object[] widen(Object[] args, Class[] params) throws IllegalArgumentException { if (args == null) args = new Object[0]; if (params == null) params = new Class[0]; int length = args.length; if (length != params.length) throw new IllegalArgumentException(); for (int i = 0; i < length; i++) args[i] = widen(args[i], params[i]); return args; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -