Java Reflection Basics

Source: Internet
Author: User

1. Description of the ReflectionJava's reflection mechanism is in the running state, for any class, can know all the methods and properties of this class, for any object can call their methods and properties. The mechanism of this dynamic invocation is called reflection. To parse a class, you must get the byte-code file object of the class, and the class to parse. All objects that have a class type that get the bytecode of each class. The process of loading Java objects. New object, the JVM finds the object's class file from the bytecode file into memory, forms the object space and creates the class object, then the new same object, no longer loaded, and the essence of Java reflection is to get all the information of the class object to get the object backwards.  Explains the loading process for class objects. Instances of class classes represent classes and interfaces in a running Java application. That is, there are more than n instances of the JVM in each class that has the class object. (including basic data types) Class has no public constructor method. Class objects are constructed automatically by the Java virtual machine and by invoking the DefineClass method in the class loader when the class is loaded. That is, we do not need to deal with the creation ourselves, the JVM has helped us to create a good. Know that we get all the information about the class object in reverse, primarily by getting the class object.2.3 Ways to get class:New person (). GetClass (); Person.class; Class.forName ("com. Person ");3. Description of the objectThe composition of the class: Construction method, property, method. The  class object describes these three descriptions with the constructor Field,method class.  constructor look at the following example: Package com. Javatest3;import Java.lang.reflect.constructor;import Java.lang.reflect.invocationtargetexception; public Class Demo {public static void main (string[] args) throws ClassNotFoundException, Nosuchmethodexception, Illegalaccessexception, InvocationTargetException, instantiationexception {class<student> studentClass = Student.class; Student Student = new Student (); class<? Extends student> AClass = Student.getclass (); class<?> AClass1 = class.forname ("com. Javatest3.student ");  system.out.println (StudentClass = = AClass1); TrueSystem.out.println (StudentClass = = AClass); TrueSystem.out.println (AClass1 = = AClass); Trueconstructor<?>[] Constructors = aclass1.getconstructors (); Get the public method constructor<?>[] declaredconstructors = Aclass.getdeclaredconstructors ();//Get so the method, private, Protected for (Constructor c:constructors) {System.out.println (c);} for (Constructor c1:declaredconstructors) {System.out.println (C1);} Gets the parameterless construction method execution of the class. constructor<?> Constructor = Aclass1.getconstructor (null); Object o = Constructor.newinstance ();  // Gets the parameter of the class execution constructor<?> constructor1 = Aclass1.getconstructor (Char.class); Constructor.setaccessible (True) Object O1 = constructor1.newinstance (' Male ');}} The  field class looks at the following example:  package com. Javatest3;import Java.lang.reflect.field;public class Demo1 {public static void main (string[] args) {try {class<?> AClass = Class.forName ("com. Javatest3.student1 "); field[] fields = Aclass.getfields (), for (Field f:fields) {System.out.println (f);} For (Field F:aclass.getdeclaredfields ()) {System.out.println (f);} Field f = Aclass.getdeclaredfield ("name"), Object o = Aclass.getdeclaredconstructor (null). Newinstance (); F.setaccessible (TRUE); Violent visit to F.set (O, "Stephen Chow Chi"); Student1 student = (Student1) o; System.out.println (Student.getname ());} catch (Exception ex) {ex.printstacktrace ();}}} The method class looks at the following example: Package com. Javatest3; iMport Java.lang.reflect.method; public class Demo2 {public static void main (string[] args) {try {class<?> ACla SS = Class.forName ("com. Javatest3.student2 "); for (Method m:aclass.getmethods ()) {System.out.println (m);} For (Method m:aclass.getdeclaredmethods ()) {System.out.println (m);} Object o = Aclass.getconstructor (). newinstance (); Method Show1 = Aclass.getdeclaredmethod ("Show1", String.class); Show1.invoke (O, "Zhouxiaoqing"); Method Show2 = Aclass.getdeclaredmethod ("Show2", null); Show2.invoke (o); Method show3 = Aclass.getdeclaredmethod ("show3", null); Show3.invoke (o); Method show4 = Aclass.getdeclaredmethod ("Show4", Int.class); Private show4.setaccessible (true); Show4.invoke (o,123);} catch (Exception ex) {ex.printstacktrace ();}}}   Summary: Construction methods Get general use of getdeclaredconstructors, properties are generally used with the Getdeclaredfields method aclass.getdeclaredmethods Access private method settings accessible to true. 

Java Reflection Basics

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.