javaweb-codereview learning record -4

Reflection java.lang.Runtime

Getruntime need to return an instance of class Runtime before when p cattle knowledge planet also learned to call Runtime reflection mechanism to execute commands as Runtime is a singleton class design and construction methods of the class is private and can not be newinstance to directly instantiate the class, so in addition to the class returned by getRuntime class instance to invoke methods, but may also be obtained by a reflection method of the class structure and provided direct access to instantiate property class.

import sun.misc.IOUtils;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class reflect_runtime {
    private static Object cmd="calc";

    public static void main (String args[]) throws Exception {
        // 获取Runtime类对象
        Class runtimeClass1 = Class.forName("java.lang.Runtime");

// Get constructor 
        the Constructor constructor = runtimeClass1.getDeclaredConstructor ();
        constructor.setAccessible(true);

// Create the Runtime class example, RT = equivalent to new new Runtime Runtime (); 
        Object RuntimeInstance = Constructor.newInstance ();

// Get the Runtime exec (String cmd) method 
        Method, runtimeMethod = runtimeClass1.getMethod ( "Exec", String. Class );

// call exec method equivalent to rt.exec (cmd); 
        Process = Process (Process) runtimeMethod.invoke (RuntimeInstance, cmd);



    }

}

The above code comments already explained very clear, a time () Returns the class by class-type object getDeclaredConstructor no-argument constructor, and then to modify the configuration property accessed by the method setAccessible (true), to facilitate the next by this constructor to newInstance instance of the class.

 And here is the return getRuntime instance of the class

 

 

The method of construction and getDeclareConstructor getConstructor class are available, the latter can get the difference between private constructor class, but not the former, the method of construction generally are used to get the class of the former, of course, if a class has multiple the method of construction, it can be coupled with the inlet parameters of the type of method, and the reflection function call when whichever is more similar construction method, the constructor getDeclareConstructors on all available class returns an array, it can be more.

Reflection to invoke class methods

This is in p cattle knowledge planets also have to call the methods of the class through an object class type, of course, there are also two ways, getMethod and getDeclareMethod are able to get into the class member method, except that getMethod only get to 当前类和父类all there is methods rights (such as: public modified method), and getDeclareMethod can get to all members of the methods of the current class (does not include the parent class).

Examples of reflection by Method.invoke call a method, the first parameter is generally reflected invoke function call class, the second beginning is passed parameter (), but if the call is a static class method, the first a parameter can be null, because the static method call need not instantiate the class class.

Call to a member variable reflection

GetDeclaredField be able to get through all the class member variables.

getFieldAnd getDeclaredFieldthe difference between the same getMethodandgetDeclaredMethod

 Gets the value of a member variable:

Object obj = field.get (class instance object);
Modify the value of the member variables:
field.set (class instance object, the modified value);

The same can also be used here setAccessible when modifying variables similar private modified to modify the access rights

Guess you like

Origin www.cnblogs.com/tr1ple/p/12229915.html