Java Reflection-GetXXX and Getdeclaredxxx

Source: Internet
Author: User

1, GetXXX and getdeclaredxxx

In Java, Class<?> has the following methods:

A similar approach is:

2, GetMethod (s) and Getdeclaredmethod (s)

Getdeclaredmethods only gets the method of the current object declaration and does not contain inherited methods.

* Returns an array containing {@code Method} objects reflecting all the
* Declared methods of the class or interface represented by this {@code
* Class} object, including public, protected, default (package)
* Access, and private methods, but excluding inherited methods.

GetMethods Get Public method

All the
Public methods of the class or interface represented by this {@code
* Class} object, including those declared by the class or interface and
* Those inherited from Superclasses and superinterfaces.

Take the usual GetMethod and Getdeclaredmethod as an example:

 Public class Demoservice {    public  string showdemopublic (String methodName) {        return ] Show Demo "+ MethodName;    }     protected string showdemoprotected (String methodName) {        return "show demo" + MethodName;    }     Private string Showdemoprivate (String methodName) {        return "show demo" + MethodName;    }} 

========================getmethods=========================
Public java.lang.String com.logback.demo.service.DemoService.showDemoPublic (java.lang.String)
Public final void Java.lang.Object.wait () throws Java.lang.InterruptedException
Public final void Java.lang.Object.wait (Long,int) throws Java.lang.InterruptedException
Public final native void Java.lang.Object.wait (long) throws Java.lang.InterruptedException
public boolean java.lang.Object.equals (Java.lang.Object)
Public java.lang.String java.lang.Object.toString ()
public native int Java.lang.Object.hashCode ()
Public final native Java.lang.Class Java.lang.Object.getClass ()
Public final native void Java.lang.Object.notify ()
Public final native void Java.lang.Object.notifyAll ()
========================getdeclaredmethods=========================
Public java.lang.String com.logback.demo.service.DemoService.showDemoPublic (java.lang.String)
Private java.lang.String com.logback.demo.service.DemoService.showDemoPrivate (java.lang.String)
Protected java.lang.String com.logback.demo.service.DemoService.showDemoProtected (java.lang.String)

GetMethods and Getdeclaredmethods:

System.out.println ("========================getmethods========================="=  Demoservice.getclass (). GetMethods ();  for (Method m:methods_1) {    System.out.println (m);} System.out.println ("========================getdeclaredmethods========================="=  Demoservice.getclass (). Getdeclaredmethods ();  for (Method m:methods_2) {    System.out.println (m);}

GetMethod and Getdeclaredmethod

 Public voidGetmethodbyname (@RequestParam ("method") String method)throwsnosuchmethodexception {Try{System.out.println ("========================getmethod========================="); Method method1= Demoservice.getclass (). GetMethod ("Showdemo" + method, String.class);    System.out.println (METHOD1); } Catch(Exception ex) {System.out.println (ex); }    Try{System.out.println ("========================getdeclaredmethod========================="); Method method2= Demoservice.getclass (). Getdeclaredmethod ("Showdemo" + method, String.class);    System.out.println (METHOD2); } Catch(Exception ex) {System.out.println (ex); }}

Http://localhost:8088/getMethod?method=Public

========================getmethod=========================
Public java.lang.String com.logback.demo.service.DemoService.showDemoPublic (java.lang.String)
========================getdeclaredmethod=========================
Public java.lang.String com.logback.demo.service.DemoService.showDemoPublic (java.lang.String)

http://localhost:8088/getMethod?method=Protected

========================getmethod=========================
java.lang.NoSuchMethodException: com.logback.demo.service.DemoService.showDemoProtected ( java.lang.String)
========================getdeclaredmethod=========================
Protected java.lang.String com.logback.demo.service.DemoService.showDemoProtected (java.lang.String)

Http://localhost:8088/getMethod?method=Private

========================getmethod=========================
java.lang.NoSuchMethodException: com.logback.demo.service.DemoService.showDemoPrivate (java.lang.String )
========================getdeclaredmethod=========================
Private java.lang.String com.logback.demo.service.DemoService.showDemoPrivate (java.lang.String)

2, GetField (s) and Getdeclaredfield (s)
 Public class Demo {private  Integer ID; Private String name;      Public String desc;}  Public class extends Demo {private  Integer age; Private Integer sex;}

Verifying GetField and Getdeclarefield

         for(Field Field:demo.getClass (). Getdeclaredfields ()) {System.out.println (field); } System.out.println (">>>>>>>>");  for(Field Field:demo.getClass (). GetFields ()) {System.out.println (field); } System.out.println ("===========================================");  for(Field Field:student.getClass (). Getdeclaredfields ()) {System.out.println (field); } System.out.println (">>>>>>>>");  for(Field Field:student.getClass (). GetFields ()) {System.out.println (field); }

Getdeclaredfield gets all the declared properties, not the inherited attributes.

GetFields gets the properties of all public, including inherited attributes.

Private Java.lang.Integer Com.logback.demo.common.Demo.id

Private Java.lang.String Com.logback.demo.common.Demo.name

Public java.lang.String Com.logback.demo.common.Demo.desc

>>>>>>>>

Public java.lang.String Com.logback.demo.common.Demo.desc

===========================================

Private Java.lang.Integer Com.logback.demo.common.StudentDemo.age

Private Java.lang.Integer Com.logback.demo.common.StudentDemo.sex

>>>>>>>>

Public java.lang.String Com.logback.demo.common.Demo.desc

GetFields

Java Reflection-GetXXX and Getdeclaredxxx

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.