A hole in the tread of Java reflection

Source: Internet
Author: User

A reflection was used at work today. Its business is simply described as: The method signature that requires a timed call is saved to the database when the system starts, the thread is timed to read the corresponding method signature from the database, and the method is called after the instance is generated by reflection. Complete a certain scheduled task.

The method to write the signature is as follows

Public Class a{

public void statistics (Member Member, Boolean flag)//It is wrong to write this.

}

Call A.class (). GetMethod ("Statistics", Member.class, Boolean.class); So called

Newspaper Nosuchmethodexception.

Cause: The method signature must be completely correct when GetMethod. This procedure does not have an invalid automatic boxing unboxing.

Workaround: Change the statistics in a (Member Member, Boolean flag) to: statistics (Member Member, Boolean flag).

By the way, a look at the JDK documentation:

The way it is handled.

If name the IS " <init> ;"  or " <clinit> " A is NoSuchMethodException raised. <init> and <client> What are the things ... means I don't know.

If the parameter of the method is empty, it is treated as an empty array.

The algorithm for finding the method is as follows:

Description: Given a class C,

1. If the specified method name and parameter name exactly match, return the specified method,

2. If the corresponding method is not found in the given C, it is recursively entered into the parent class of C until object. Also, if there are multiple methods for finding the same signature,

If more than one such method was found in C, and one of the these methods have a return type that's more specific than any of T He others

That is, if more than 1 method matches are exceeded, the return values of these methods are judged, and the most exact matching method is returned. See this, is also confused, hurriedly go back to the Java basic syntax, is it

Can you define multiple methods of the same signature in a class? (The same signature refers to the same method name, the same parameter type, and the order of the same parameter type). If the match is only accurate to the parameter type, then randomly returns a

Match the method on the.

Finally, I explained that I was confused: the Java language does not allow more than 1 methods to have the same signature in the same class, but the Java virtual machine is allowed. Because this feature improves the virtual machine for implementing virtual languages

The flexibility of the function. For example: The return value of Convarant can be implemented by bridge method, this bridge method and method write can have the same method signature but have different access types .... Bridge, Unknown ... Tall on the look.

Reference: Https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29

A hole in the tread of Java reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.