Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java get private constructions, private objects, private fields, private methods through reflection?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how Java obtains private structures, private objects, private fields and private methods through reflection. It has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article. Let's take a look at it.

Java reflection gets private constructions, private objects, private fields, private methods 1. Create a private test object / * * @ author lirong * @ desc test object * @ date 20:07 on 2019-06-20 * / public class Person {private int age = 5; private String name; private Person () {} private String test (String name) {System.out.println ("name:" + name); return "test";}} 2. Get properties and methods in private objects / * * @ author lirong * @ desc reflection get private properties and methods * @ date 20:10 on 2019-06-20 * / public class Test {public static void main (String [] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {/ / 1. Get the class object Class clazz = Person.class; / / 2. Get the private no-parameter construction Constructor c = clazz.getDeclaredConstructor (); / / 3. Set the access to visible c.setAccessible (true); / / 4. Create the instance object Person person = (Person) c.newInstance () through the constructor; / / get the field Field age = clazz.getDeclaredField ("age") in class based on the field name; age.setAccessible (true); System.out.println (age.getName () + "=" + age.get (person) / / modify the default value of private variables age.set (person, 18); System.out.println (age.getName () + "=" + age.get (person)); / / 5. Get all fields Field [] fields = clazz.getDeclaredFields (); for (Field f: fields) {/ / set field visibility f.setAccessible (true); String name = f.getName (); Object o = f.get (person); System.out.println (name + "-" + o);} / / 6. Get all the methods Method [] methods = clazz.getDeclaredMethods (); for (Method m: methods) {m.setAccessible (true); String name = m.getName (); Object invoke = m.invoke (person, "Zhang San"); System.out.println (name + "=" + invoke);}} obtain private inner class objects through reflection

The first is our target:

Class Out {/ / the target gets the private member attribute of the Inner object private class Inner {/ / inner class private String inner = "ccc";}} directly lists the code public class Main {@ SuppressWarnings ({"rawtypes", "unchecked"}) public static void main (String [] args) throws Exception {/ / gets the external class Class clzz = Out.class / / get the default no-parameter constructor of the external class Constructor con = clzz.getDeclaredConstructor (); / / instance an external class object Out outObj = (Out) con.newInstance (); / / get all the inner classes within the external class Class innerClazz [] = clzz.getDeclaredClasses () / / traversing for (Class c: innerClazz) {/ / gets the integer code of the modifier int mod = c.getModifiers (); / / returns the string object String modifier = Modifier.toString (mod) of the modifier corresponding to the integer code / / find the inner class if (modifier.contains ("private")) modified by private) {/ / according to the characteristics of the inner class, the outer class needs to reflect and obtain the constructor of the inner class (here you get the default constructor of the inner class) Constructor cc = c.getDeclaredConstructor (clzz) / / because the inner class is private, you need to force access to the constructor cc.setAccessible (true); / / the external class object reflects the object Object obj=cc.newInstance (outObj) that gets the inner class. / / get the private member attribute inner Field f=c.getDeclaredField ("inner") of the inner class; / / get the access permission f.setAccessible (true); / / get the value System.out.println (f.get (obj)) of the private member attribute inner in the inner class object obj }

Output result:

Ccc

Thank you for reading this article carefully. I hope the article "how to get private structures, private objects, private fields and private methods by Java through reflection" shared by the editor is helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report