Java Reflection Learning Four

Source: Internet
Author: User

calling private methods with reflection, accessing private properties

With reflection, the first is the acquisition of the class object, followed by the method and the Field object.

In the case of method, you can see from the documentation:

The GetMethod () method returns the public method object,

The method object returned by Getdeclaredmethod () can be non-public.

field is the same way.

  access to private properties and methods to suppress Java access checks by using the Setaccessible () method in the AccessibleObject class (the base class of the Constructor, field, and method classes) before use .

Instance 1, calling private method

Suppose you have a class that contains a private method.

public class privateclass{    private String SayHello (string name)    {        return "Hello:" + Name;    }}

Use the reflection mechanism to access the method externally:

Import Java.lang.reflect.method;public class testprivate{public    static void Main (string[] args) throws exception< c1/>{        Privateclass p = new Privateclass ();        class<?> ClassType = P.getclass ();        Get Method Object Method        = Classtype.getdeclaredmethod ("SayHello",                new class[] {string.class});        Method.setaccessible (TRUE); Suppress Java access Control check        //If you do not add the above sentence, you will error:testprivate can not access a member of class Privateclass with modifiers "Priva TE "        String str = (string) Method.invoke (p, new object[] {" Zhangsan "});        System.out.println (str);}    }


Instance 2, accessing private properties

Direct access to private properties, changing the value of the private property in the example.

A class that contains a private property:

public class privateclass2{    private String name = "Zhangsan";    Public String getName ()    {        return name;    }}

Modify the value of its private property with reflection:

Import Java.lang.reflect.field;public class testprivate2{public    static void Main (string[] args) throws exception< c8/>{        PrivateClass2 p = new PrivateClass2 ();        class<?> ClassType = P.getclass ();        Field field = Classtype.getdeclaredfield ("name");        Field.setaccessible (TRUE); Suppresses Java's checking of modifiers        field.set (p, "Lisi");        System.out.println (P.getname ());}    }

Class

The Object class in Java is the source of inheritance for all classes, where the getclass () method returns a class Object. All classes have this method.

   class is very special, and it inherits from object like other classes, in fact the body is used to express the Java program runtime classes and interfaces, but also to express the enum, array, primitive Java Types (Boolean, Byte, char, short, int, long, float, double) and keyword void.

When a class is loaded, or when the defineclass () of the Loader (class loader) is called by the JVM, the JVM automatically generates a Class object.

If you want to change the Java standard Library source code to observe the actual generation time of the class object, such as adding println () in the class constructor, it is not possible. Because class does not have public constructor.

Class is the origin of reflection. For any class that you want to explore, you only have to create a class object for it before you can evoke more than 10 reflection APIs through the latter.

The way to get the "Class" object

  1. Use the GetClass () method to return the class object.

Use Class.getsuperclass () to get the class object of the parent class and return NULL if it is an object class.

  2. Using static method Class.forName ()

  3. Use the. class syntax. Class name.

The class object of the shaping array can also be obtained in the form of Int[].class.

Wrapper class . The type syntax actually returns a class object of the corresponding native data type.

Resources:

Http://www.cnblogs.com/mengdd/archive/2013/01/26/2878136.html

Http://www.cnblogs.com/gulvzhe/archive/2012/01/27/2330001.html

Http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html

Http://www.cnblogs.com/octobershiner/archive/2012/03/18/2404751.html

Java Reflection Learning Four

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.