📄 method.java
字号:
return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); } /** * Get a String representation of the Method. A Method's String * representation is "<modifiers> <returntype> * <methodname>(<paramtypes>) throws <exceptions>", where * everything after ')' is omitted if there are no exceptions.<br> Example: * <code>public static int run(java.lang.Runnable,int)</code> * * @return the String representation of the Method */ public String toString() { // 128 is a reasonable buffer initial size for constructor StringBuilder sb = new StringBuilder(128); Modifier.toString(getModifiers(), sb).append(' '); sb.append(ClassHelper.getUserName(getReturnType())).append(' '); sb.append(getDeclaringClass().getName()).append('.'); sb.append(getName()).append('('); Class[] c = getParameterTypes(); if (c.length > 0) { sb.append(ClassHelper.getUserName(c[0])); for (int i = 1; i < c.length; i++) sb.append(',').append(ClassHelper.getUserName(c[i])); } sb.append(')'); c = getExceptionTypes(); if (c.length > 0) { sb.append(" throws ").append(c[0].getName()); for (int i = 1; i < c.length; i++) sb.append(',').append(c[i].getName()); } return sb.toString(); } public String toGenericString() { // 128 is a reasonable buffer initial size for constructor StringBuilder sb = new StringBuilder(128); Modifier.toString(getModifiers(), sb).append(' '); 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 { return invokeNative(o, args, declaringClass, parameterTypes, returnType, slot, flag); } /** * 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(declaringClass, slot); if (sig == null) return new TypeVariable[0]; MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getTypeParameters(); } /** * 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(declaringClass, slot); 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(declaringClass, slot); 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(declaringClass, slot); if (sig == null) return getReturnType(); MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getGenericReturnType(); } public Annotation getAnnotation(Class annoClass) { Annotation[] annos = getDeclaredAnnotations(); for (int i = 0; i < annos.length; i++) if (annos[i].annotationType() == annoClass) return annos[i]; return null; } public Annotation[] getDeclaredAnnotations() { return getDeclaredAnnotationsNative(declaringClass, slot); } public Annotation[][] getParameterAnnotations() { return getParameterAnnotationsNative(declaringClass, slot); } /** * If this method is an annotation method, returns the default * value for the method. If there is no default value, or if the * method is not a member of an annotation type, returns null. * Primitive types are wrapped. * * @throws TypeNotPresentException if the method returns a Class, * and the class cannot be found * * @since 1.5 */ public Object getDefaultValue() { return getDefaultValueNative(declaringClass, slot); } /* * NATIVE HELPERS */ /** * Return the raw modifiers for this method. * @return the method's modifiers */ private native int getMethodModifiers(Class declaringClass, int slot); /** * Return the String in the Signature attribute for this method. If there * is no Signature attribute, return null. */ private native String getSignature(Class declaringClass, int slot); private native Object invokeNative(Object o, Object[] args, Class declaringClass, Class[] parameterTypes, Class returnType, int slot, boolean noAccessCheck) throws IllegalAccessException, InvocationTargetException; private native Object getDefaultValueNative(Class declaringClass, int slot); private native Annotation[] getDeclaredAnnotationsNative(Class declaringClass, int slot); private native Annotation[][] getParameterAnnotationsNative(Class declaringClass, int slot);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -