📄 method.java
字号:
Constructor.addTypeParameters(sb, getTypeParameters()); sb.append(getGenericReturnType()).append(' '); sb.append(getDeclaringClass().getName()).append('.'); sb.append(getName()).append('('); Type[] types = getGenericParameterTypes(); if (types.length > 0) { sb.append(types[0]); for (int i = 1; i < types.length; i++) sb.append(',').append(types[i]); } sb.append(')'); types = getGenericExceptionTypes(); if (types.length > 0) { sb.append(" throws ").append(types[0]); for (int i = 1; i < types.length; i++) sb.append(',').append(types[i]); } return sb.toString(); } /** * Invoke the method. Arguments are automatically unwrapped and widened, * and the result is automatically wrapped, if needed.<p> * * If the method is static, <code>o</code> will be ignored. Otherwise, * the method uses dynamic lookup as described in JLS 15.12.4.4. You cannot * mimic the behavior of nonvirtual lookup (as in super.foo()). This means * you will get a <code>NullPointerException</code> if <code>o</code> is * null, and an <code>IllegalArgumentException</code> if it is incompatible * with the declaring class of the method. If the method takes 0 arguments, * you may use null or a 0-length array for <code>args</code>.<p> * * Next, if this Method enforces access control, your runtime context is * evaluated, and you may have an <code>IllegalAccessException</code> if * you could not acces this method in similar compiled code. If the method * is static, and its class is uninitialized, you trigger class * initialization, which may end in a * <code>ExceptionInInitializerError</code>.<p> * * Finally, the method is invoked. If it completes normally, the return value * will be null for a void method, a wrapped object for a primitive return * method, or the actual return of an Object method. If it completes * abruptly, the exception is wrapped in an * <code>InvocationTargetException</code>. * * @param o the object to invoke the method on * @param args the arguments to the method * @return the return value of the method, wrapped in the appropriate * wrapper if it is primitive * @throws IllegalAccessException if the method could not normally be called * by the Java code (i.e. it is not public) * @throws IllegalArgumentException if the number of arguments is incorrect; * if the arguments types are wrong even with a widening conversion; * or if <code>o</code> is not an instance of the class or interface * declaring this method * @throws InvocationTargetException if the method throws an exception * @throws NullPointerException if <code>o</code> is null and this field * requires an instance * @throws ExceptionInInitializerError if accessing a static method triggered * class initialization, which then failed */ public Object invoke(Object o, Object[] args) throws IllegalAccessException, InvocationTargetException, IllegalArgumentException { if (args == null) { args = nullArgs; } if (!Modifier.isStatic (getModifiers())) { if (o == null && !"<init>".equals(name)) { throw new NullPointerException("Null object pointer"); } if (o != null && !getDeclaringClass().isInstance(o)) { throw new IllegalArgumentException("Object type doesn't match method's class"); } } if (args.length != getParameterTypes().length) { throw new IllegalArgumentException("wrong number of arguments"); } /* Process arguments to get them 'correct' */ for (int i = 0; i < args.length; i++) { Class pt = parameterTypes[i]; Object arg = args[i]; if (pt.isPrimitive()) { /* Might need fixup */ if (pt == Boolean.TYPE) { if (arg instanceof Boolean) { } else { throw new IllegalArgumentException(); } } else if (pt == Byte.TYPE) { if (arg instanceof Byte) { } else { throw new IllegalArgumentException(); } } else if (pt == Short.TYPE) { if (arg instanceof Short) { } else if (arg instanceof Byte) { args[i] = new Short((short)((Byte)arg).byteValue()); } else { throw new IllegalArgumentException(); } } else if (pt == Character.TYPE) { if (arg instanceof Character) { } else { throw new IllegalArgumentException(); } } else if (pt == Integer.TYPE) { if (arg instanceof Integer) { } else if (arg instanceof Short) { args[i] = new Integer((int)((Short)arg).shortValue()); } else if (arg instanceof Character) { args[i] = new Integer((int)((Character)arg).charValue()); } else if (arg instanceof Byte) { args[i] = new Integer((int)((Byte)arg).byteValue()); } else { throw new IllegalArgumentException(); } } else if (pt == Long.TYPE) { if (arg instanceof Long) { } else if (arg instanceof Integer) { args[i] = new Long((long)((Integer)arg).intValue()); } else if (arg instanceof Short) { args[i] = new Long((long)((Short)arg).shortValue()); } else if (arg instanceof Character) { args[i] = new Long((long)((Character)arg).charValue()); } else if (arg instanceof Byte) { args[i] = new Long((long)((Byte)arg).byteValue()); } else { throw new IllegalArgumentException(); } } else if (pt == Float.TYPE) { if (arg instanceof Float) { } else if (arg instanceof Long) { args[i] = new Float((float)((Long)arg).longValue()); } else if (arg instanceof Integer) { args[i] = new Float((float)((Integer)arg).intValue()); } else if (arg instanceof Short) { args[i] = new Float((float)((Short)arg).shortValue()); } else if (arg instanceof Character) { args[i] = new Float((float)((Character)arg).charValue()); } else if (arg instanceof Byte) { args[i] = new Float((float)((Byte)arg).byteValue()); } else { throw new IllegalArgumentException(); } } else if (pt == Double.TYPE) { if (arg instanceof Double) { } else if (arg instanceof Float) { args[i] = new Double((double)((Float)arg).floatValue()); } else if (arg instanceof Long) { args[i] = new Double((double)((Long)arg).longValue()); } else if (arg instanceof Integer) { args[i] = new Double((double)((Integer)arg).intValue()); } else if (arg instanceof Short) { args[i] = new Double((double)((Short)arg).shortValue()); } else if (arg instanceof Character) { args[i] = new Double((double)((Character)arg).charValue()); } else if (arg instanceof Byte) { args[i] = new Double((double)((Byte)arg).byteValue()); } else { throw new IllegalArgumentException(); } } else { throw new Error("cannot happen"); } } else if (arg!=null && !pt.isAssignableFrom(arg.getClass())) { throw new IllegalArgumentException("incompatible argument"); } } return invoke0(o, args); } /* * NATIVE HELPERS */ private native Object invoke0(Object o, Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException; /** * Returns an array of <code>TypeVariable</code> objects that represents * the type variables declared by this constructor, in declaration order. * An array of size zero is returned if this class has no type * variables. * * @return the type variables associated with this class. * @throws GenericSignatureFormatError if the generic signature does * not conform to the format specified in the Virtual Machine * specification, version 3. * @since 1.5 */ /* FIXME[GENERICS]: Should be TypeVariable<Method>[] */ public TypeVariable[] getTypeParameters() { String sig = getSignature(); if (sig == null) return new TypeVariable[0]; MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getTypeParameters(); } /** * Return the String in the Signature attribute for this method. If there * is no Signature attribute, return null. */ private native String getSignature(); /** * Returns an array of <code>Type</code> objects that represents * the exception types declared by this method, in declaration order. * An array of size zero is returned if this method declares no * exceptions. * * @return the exception types declared by this method. * @throws GenericSignatureFormatError if the generic signature does * not conform to the format specified in the Virtual Machine * specification, version 3. * @since 1.5 */ public Type[] getGenericExceptionTypes() { String sig = getSignature(); if (sig == null) return getExceptionTypes(); MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getGenericExceptionTypes(); } /** * Returns an array of <code>Type</code> objects that represents * the parameter list for this method, in declaration order. * An array of size zero is returned if this method takes no * parameters. * * @return a list of the types of the method's parameters * @throws GenericSignatureFormatError if the generic signature does * not conform to the format specified in the Virtual Machine * specification, version 3. * @since 1.5 */ public Type[] getGenericParameterTypes() { String sig = getSignature(); if (sig == null) return getParameterTypes(); MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getGenericParameterTypes(); } /** * Returns the return type of this method. * * @return the return type of this method * @throws GenericSignatureFormatError if the generic signature does * not conform to the format specified in the Virtual Machine * specification, version 3. * @since 1.5 */ public Type getGenericReturnType() { String sig = getSignature(); if (sig == null) return getReturnType(); MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getGenericReturnType(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -