Java Reflection Basics

Source: Internet
Author: User
Tags modifiers

The reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the method of the object is called the reflection mechanism of the Java language. Link

The main method of reflection

Method/Property

Explain

Getdeclaredmethods ()

Get all the methods

Getreturntype ()

Get the return type of the method

Getparametertypes ()

Get the incoming parameter type for a method

Getdeclaredmethod (" method name", Parameter type. Class,......)

Get a specific approach

Getdeclaredconstructors ()

Get all the construction methods

getdeclaredconstructor ( parameter type. Class,......)

Get a specific construction method

Getsuperclass ()

Gets the parent class of a class

Getinterfaces ()

An interface that takes a class implementation

Getdeclaredfield ()

Gets the specified property of the declaration

Getdeclaredfields ()

Get all properties of a claim

GetField ()

Gets the specified property

GetFields ()

Get all Properties

Field.get (Clazz)

Get class Properties

Field.set (Clazz,property);

Set class properties

Reflection-related class Java.lang.Class;                Java.lang.reflect.Constructor; Java.lang.reflect.Field;        Java.lang.reflect.method;java.lang.reflect.modifier;
reflection accesses the class's Properties class<?> Clazz =NULL; Try{clazz= Class.forName ("Com.simple.dec.advance.basic.basic1.SonClazz"); Field field= Clazz.getdeclaredfield ("Sonpwd"); Field.setaccessible (true); Object result= Field.get ( This);            Zlog.zze (result); Field.set ( This, "Java Reflection mechanism"); Result= Field.get ( This);        Zlog.zze (result); } Catch(Exception e) {e.printstacktrace (); }
method class for reflection Access class<?> Clazz =NULL; Try{clazz= Class.forName ("Com.simple.dec.advance.basic.basic1.SonClazz"); Method Method= Clazz.getmethod ("Getsonpwd");//Method.invoke (Clazz.newinstance ());Object result = Method.invoke ( This);   Zlog.zze (result); //with no parametersmethod = Clazz.getmethod ("Getsonemail"); Result= Method.invoke ( This);   Zlog.zze (result); //with parameters, access to private methodsmethod = Clazz.getdeclaredmethod ("Getsum",int.class, String.class); Method.setaccessible (true); Result= Method.invoke ( This, "Isson ...");            Zlog.zze (result); Method= Clazz.getmethod ("Getsum",int.class, String.class); Result= Method.invoke ( This, "Isson ...");        Zlog.zze (result); } Catch(Exception e) {e.printstacktrace (); }

Reference:

A detailed explanation of the Java reflection mechanism

Java Reflection

Java Reflection and annotations

The reflection mechanism in Java

Alternate code

    /*** A method that calls a subclass * Cannot access private methods*/    Private voidGetsonmethod () {Class<?> Clazz =NULL; Try{clazz= Class.forName ("Com.simple.dec.advance.basic.basic1.SonClazz"); Method Method= Clazz.getmethod ("Getsonpwd");//Method.invoke (Clazz.newinstance ());Object result = Method.invoke ( This);   Zlog.zze (result); //with no parametersmethod = Clazz.getmethod ("Getsonemail"); Result= Method.invoke ( This);   Zlog.zze (result); //with parameters, access to private methodsmethod = Clazz.getdeclaredmethod ("Getsum",int.class, String.class); Method.setaccessible (true); Result= Method.invoke ( This, "Isson ...");            Zlog.zze (result); Method= Clazz.getmethod ("Getsum",int.class, String.class); Result= Method.invoke ( This, "Isson ...");        Zlog.zze (result); } Catch(Exception e) {e.printstacktrace (); }    }    /*** Get all methods of subclasses*/    Private voidGetsonmethodall () {Class<?> Clazz =NULL; Try{clazz= Class.forName ("Com.simple.dec.advance.basic.basic1.SonClazz"); Method method[]=Clazz.getmethods ();  for(inti = 0; i < method.length; ++i) {Class<?> ReturnType =Method[i].getreturntype (); Class<?> para[] =method[i].getparametertypes (); inttemp =method[i].getmodifiers (); System.out.print (modifier.tostring (temp)+ " "); System.out.print (Returntype.getname ()+ "  "); System.out.print (Method[i].getname ()+ " "); System.out.print ("(");  for(intj = 0; J < Para.length; ++j) {System.out.print (Para[j].getname ()+ "+" + "arg" +j); if(J < Para.length-1) {System.out.print (","); }} Class<?> exce[] =method[i].getexceptiontypes (); if(Exce.length > 0) {System.out.print (") throws");  for(intk = 0; K < Exce.length; ++k) {System.out.print (Exce[k].getname ()+ " "); if(K < Exce.length-1) {System.out.print (","); }                    }                } Else{System.out.print (")");            } System.out.println (); }        } Catch(ClassNotFoundException e) {e.printstacktrace (); } zlog.zze (""); }    /*** Call a property of subclass * Direct access to private property*/    Private voidGetsonproperty () {Class<?> Clazz =NULL; Try{clazz= Class.forName ("Com.simple.dec.advance.basic.basic1.SonClazz"); Field field= Clazz.getdeclaredfield ("Sonpwd"); Field.setaccessible (true); Object result= Field.get ( This);            Zlog.zze (result); Field.set ( This, "Java Reflection mechanism"); Result= Field.get ( This);        Zlog.zze (result); } Catch(Exception e) {e.printstacktrace (); }    }    /*** Get all attributes of subclass*/    Private voidGetsonpropertyall () {Class<?> Clazz =NULL; Try{clazz= Class.forName ("Com.simple.dec.advance.basic.basic1.SonClazz"); System.out.println ("=============== This class property ==============="); //get all the properties of this classfield[] field =Clazz.getdeclaredfields ();  for(inti = 0; i < field.length; i++) {                //Permission Modifiers                intMo =field[i].getmodifiers (); String Priv=modifier.tostring (MO); //Property Typeclass<?> type =Field[i].gettype (); System.out.println (Priv+ "" + type.getname () + "" + field[i].getname () + ";"); } System.out.println ("========== implemented interface or parent class Property =========="); //gets the implemented interface or properties of the parent classfield[] Filed1 =Clazz.getfields ();  for(intj = 0; J < Filed1.length; J + +) {                //Permission Modifiers                intMo =filed1[j].getmodifiers (); String Priv=modifier.tostring (MO); //Property Typeclass<?> type =Filed1[j].gettype (); System.out.println (Priv+ "" + type.getname () + "" + filed1[j].getname () + ";"); }        } Catch(ClassNotFoundException e) {e.printstacktrace (); }    }

Java Reflection Basics

Related Article

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.