Thoughts on Java reflection mechanism

Source: Internet
Author: User
Today, we see a Java runtime method getsomevalue (string propertyname, object OBJ ), in general, the function is to obtain the actual running value of this field based on the field name of an object and the object. Besides some logical operations, java. Lang. Reflect. MethodInvoke(Object OBJ, object... ARGs) method. After reading the JDK instructions, I decided to write a small example to study it ~

First, let's briefly introduce the runtime mechanism of Java. During the program running, the system keeps identifying all objects for the so-called runtime type (rtti) during the Java runtime ), to determine the class to which each object belongs. JVM usually uses the runtime type information to select the correct method for execution.
* In Java, the class that stores the runtime type information is called the class.

* For each loaded type (whether it is a class or an interface), JVM will create a class object for it accordingly. In your application, you can obtain and reference A Class Object.

* At runtime, once we want to generate a class object, JVM will first check whether the class object of that type has been loaded. If the file is not loaded, the JVM will find the. Class file with the same name and load it. Therefore, Java programs are not fully loaded at startup, which is different from many traditional languages. Once a class object of that type enters the memory, it is used to create all objects of that type.

We usually use getclass (), Class. forname (), obj. Class, and so on to operate the runtime class. Another mechanism is reflection. Let's take a look.

If you do not know the exact type of an object, rtti will help us investigate. But there is a limit: The type must be known during compilation.
Reflection allows us to explore a class during runtime. The only difference between rtti and "Reflection" is that for rtti, the compiler will open and check the. Class file during compilation. But for "reflection ,. class files are not available during compilation, but are opened and checked by the runtime environment. We use the reflection mechanism to generally use Java. lang. the classes and methods provided by the reflect package

Turn to the topic and write a simple example to learn the invoke method.
[Code] [br] 1 public class player [br] 2 {[br] 3 protected object getsomevalue (string propertyname, object OBJ) throws nosuchmethodexception [br] 4 {[br] 5 object rtnval = NULL; [br] 6 try [br] 7 {[br] 8 method = obj. getclass (). getmethod ("get" + character. touppercase (propertyname. charat (0) + propertyname. substring (1); [br] 9 rtnval = method. invoke (OBJ); [br] 10} [br] 11 catch (nosuchmethodexception e) [br] 12 {[br] 13 throw E; [br] 14} [br] 15 catch (exception ex) [br] 16 {[br] 17 ex. printstacktrace (); [br] 18} [br] 19 Return rtnval; [br] 20} [br] 21} [/Code]

The method has two parameters: one is the field name, and the other is the runtime object for which you want to obtain the corresponding field value. First, the rtnval of the return value of the object type is defined, then, get the get method of the corresponding field through the getclass () and getmethod () methods. The string operation in the brackets is for this purpose. At this time, the key is coming, we call the invoke (OBJ) method of the method object we just obtained. obj is the runtime object you need to locate... ARGs is omitted, because the get field method does not contain parameters, so write

Then we derive a class runner from the player class to test the above method.
[Code] [br] 1 class runner extends player [br] 2 {[br] 3 private string playingtype = "400 m "; [br] 4 [br] 5 [br] 6 Public String getplayingtype () {[br] 7 return playingtype; [br] 8} [br] 9 [br] 10 [br] 11 Public void runnertest (Object OBJ) throws nosuchmethodexception [br] 12 {[br] 13 system. out. println (getsomevalue ("playingtype", OBJ); [br] 14} [br] 15} [/Code]
We set a field to indicate the competition project that runners participate in, and then generate the get method accordingly. Then, we have a test method to output the field value. To add one, the above getsomevalue method throws a nosuchmethod exception, so pay attention to capture

Next, you can write a primary method in the player class to test
[Code] [br] 1 public static void main (string [] ARGs) [br] 2 {[br] 3 runner r = new runner (); [br] 4 try [br] 5 {[br] 6 R. runnertest (r); [br] 7} [br] 8 catch (exception e) [br] 9 {[br] 10 E. printstacktrace (); [br] 11} [br] 12} [/Code]
R is the object we created at runtime, and then the runtime object R is introduced in the running test method, and the value of the corresponding field is "400 m". Of course, to verify this, we can also write a constructor with the playingtype field for the runner class to change its value when running. The output will also be the real-time value of the currently created object.

Summary,Invoke(Object OBJ, object... ARGs) This method calls thisMethodUnderlying method of object representation,

If the underlying method is static, you can ignore the specifiedobjParameters. This parameter can be null.

 

If the shape parameter required by the underlying method is 0argsThe array length can be 0 or null.

 

If the underlying method is an instance method, use dynamic method lookup to call it,

If the underlying method is static and the class that declares this method has not been initialized, it will be initialized.

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.