Java Reflection uses

Source: Internet
Author: User
Tags locale setcookie

 PackageCom.mycompany.helloworld.refect;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;/*** Method call using Reflection * Createy by user on 7/19/2018.17:57*/ Public classDemo { Public Static voidMain (string[] args)throwsException {Class clazz= User.class; Object obj=clazz.newinstance (); User u=NewUser ("Xiaoming"); //getdeclaredfields Get field         for(Field f:clazz.getdeclaredfields ()) {//The field is not allowed to be accessed through reflection when the result of Isaccessible () is falseF.setaccessible (true); System.out.println (F.getname ()+ ":" +f.get (u)); }        //set the dynamic call method to set the parameters of a dynamically called method to be of type stringMethod Urimethod = Clazz.getmethod ("Sayhel", String.class); //Dynamic Call MethodUrimethod.invoke (obj, "This is the print in app"); }     Public Static classUser {PrivateString name; PrivateInteger age;  PublicUser () {} PublicUser (String name) { This. Name =name; }         Public voidSayhel (String desc) {System.out.println ("Come--->" +desc); }         PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }         PublicInteger getage () {returnAge ; }         Public voidsetage (Integer age) { This. Age =Age ; }    }}------------------------------------------------------------ PackageCom.mycompany.helloworld.refect;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportJava.util.HashMap;ImportJava.util.Map; Public classCommonservlet {Private Static Finalmap<string, string> header_field_relatetion =NewHashmap<string, string>(); Static{header_field_relatetion.put ("Content-type", "Setcontent_type"); Header_field_relatetion.put ("Accept-language", "Setaccept_language"); Header_field_relatetion.put ("Cookie", "Setcookie"); Header_field_relatetion.put ("Origin", "Setorigin"); Header_field_relatetion.put ("Locale", "SetLocale"); Header_field_relatetion.put ("Host", "Sethost"); Header_field_relatetion.put ("Authorization", "Setauthorization"); }    PrivateMetaObject MessageHeader =NewMetaObject ();  Public Static voidMain (string[] args)throwsException {System.out.println ("Refect------Start"); Fillimixobjectheader (NewCommonservlet ()); }    /*** HttpServletRequest converted to MetaObject using reflection *@paramRequestmessage*/    Private Static voidFillimixobjectheader (Commonservlet requestmessage)throwsException {Try {            /*** Get the MessageHeader field in the Commonrequest class*/Field Headerfield= Requestmessage.getclass (). Getdeclaredfield ("MessageHeader"); //The field is not allowed to be accessed through reflection when the result of Isaccessible () is falseHeaderfield.setaccessible (true); //Gets the reflection field that is used when object reflection calls the methodObject Header =Headerfield.get (requestmessage); //Gets the type of the reflection field used to get the dynamic call methodClass<?> headercls =Headerfield.gettype (); /*** Using reflection to dynamically invoke the method inside the class MessageHeader*/             for(Map.entry<string, string>Relatetion:HEADER_FIELD_RELATETION.entrySet ()) {String value=Relatetion.getkey (); if(Value! =NULL&&!value.trim (). Equals ("")) {                    //set the dynamic call method to set the parameters of a dynamically called method to be of type stringMethod Headitemmethod = Headercls.getmethod (Relatetion.getvalue (), String.class); Headitemmethod.invoke (header, value); //Dynamic Invocation                }            }            //dynamically call the Seturi method inside the MessageHeader class, set the arguments for the dynamically called method to be of type stringMethod Urimethod = Headercls.getmethod ("Seturi", String.class); Urimethod.invoke (header,"Http://localhost:8080/tbs"); //Dynamic invocation of the Setmethod method inside the MessageHeader classMethod Methodmethod = Headercls.getmethod ("Setmethod", String.class); Methodmethod.invoke (header,"GET");        Headerfield.set (Requestmessage, header); } Catch(Exception e) {e.printstacktrace (); }    }     Public Static classMetaObject {PrivateString Method; PrivateString authorization; PrivateString host; PrivateString locale; PrivateA String cookie; PrivateString Accept_language; PrivateString Content_Type; PrivateString Origin;  Public voidSeturi (String uri) {System.out.println ("Seturi:" +URI); }         Public voidSetmethod (String method) {System.out.println ("Setmethod:" +method); }         Public voidsetauthorization (String var1) {System.out.println ("Setauthorization:" +var1);  This. Authorization =var1; }         Public voidSethost (String host) {System.out.println ("Sethost:" +host);  This. Host =host; }         Public voidSetLocale (String locale) {System.out.println ("SetLocale:" +locale);  This. Locale =locale; }         Public voidSetcookie (String cookie) {System.out.println ("Setcookie:" +cookies);  This. cookie =cookies; }         Public voidsetaccept_language (String accept_language) {System.out.println ("Setaccept_language:" +accept_language);  This. Accept_language =Accept_language; }         Public voidSetcontent_type (String content_type) {System.out.println ("Setcontent_type:" +content_type);  This. Content_Type =Content_Type; }         Public voidSetorigin (String origin) {System.out.println ("Setorigin:" +origin);  This. Origin =origin; }    }}

Java reflection using

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.