Common reflection methods:
Packagereflection;Importjava.io.IOException;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;ImportJava.net.URL; Public classtestreflection { Public Static voidMain (string[] args)throwsclassnotfoundexception, Instantiationexception, Illegalaccessexception, IllegalArgumentException, invocationtargetexception {String str= "Reflection." T; //Get classClass c=class.forname (str); //Get class PathURL Z=c.getresource (""); URL s=c.getclassloader (). GetResource (""); //Get class address in classClassLoader d=C.getclassloader (); //get the interface implemented by Class,c class[] f=c.getinterfaces () [0];Class[] F=c.getinterfaces (); System.out.println (z); System.out.println (s); System.out.println (d); System.out.println (f); //getClassLoader (). GetResource (FileName)//a new object with the acquired classObject o=c.newinstance (); //receive all the methods in the objectMethod[] Method =C.getmethods (); System.out.println ("Comment on this element:" +c.getdeclaredannotations ()); for(Method m:method) {if(M.getname (). Equals ("M")) { //method selects the first parameter to pass the object, followed by the parameter passing method parameterM.invoke (o); System.out.println ("Hash code:" +M.hashcode ()); } if(M.getname (). Equals ("B") {m.invoke (O,The); //Get parameter Types for(Class paramtype:m.getparametertypes ()) {System.out.println ("Parameter type:" +paramtype.getname ()); } } } } }/*
The instanceof method tests whether args returns a class instance
if (Args[0] instanceof String)
* @param args **/classT { PublicT () {System.out.println ("The constructor was called."); } Public voidm () {System.out.println ("M function is called"); } Public voidbintIintj) {System.out.println (Result of "B function:" +i+j); }}
One, using reflection to automatically assign a value to Javabeen
Example been:
Packageproxy; Public classnotice{String type; String name; String data; String address; String vill; String Upfile; String num; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetData () {returndata; } Public voidsetData (String data) { This. data =data; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; } PublicString Getvill () {returnvill; } Public voidSetvill (String vill) { This. Vill =vill; } PublicString Getupfile () {returnUpfile; } Public voidSetupFile (String upfile) { This. Upfile =Upfile; } PublicString Getnum () {returnnum; } Public voidsetnum (String num) { This. num =num; } PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; }}
Insert Data class:
Packagecn.test;ImportJava.lang.reflect.Field;ImportJava.util.HashMap; Public classReflectdemo {StaticHashmap<string,string>data; Static{Data=NewHashmap<string,string>(); Data.put ("Name", "Proposed land requisition"); Data.put ("Type", "Test announcement"); Data.put ("Data", "2017-01-12"); Data.put ("Address", "Chengdu City Qingyang District"); Data.put ("Vill", "er DAO Street"); Data.put ("Upfile", "File-specific content"); Data.put ("Num", "Number: Country: 001"); Data.put ("Type", "Test announcement"); } Public Static voidMain (string[] args)throwsException {Notice obj=NewNotice (); Test2 (Obj,data); System.out.println (Obj.getaddress ()); } Public Static voidTest2 (Notice obj,hashmap<string,string> data)throwsException {Class cls=Obj.getclass (); //getdeclaredfields (): Gets all the declared fields of a classfield[] Fields =Cls.getdeclaredfields (); for(Field field:fields) {String Item=Data.get (Field.getname ()); Field.set (Obj,item); } }}
Second, dynamic agent
Implement Invocationhandler interface rewrite Invoke method through class
You can call the method before calling the other method first, do permission control
Implementation class:
Packageproxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy; Public classMyproxydemoImplementsinvocationhandler{Object AA; Publicobject Newproxy (Object aa) { This. aa=AA;
Returns a proxy class instance for the specified interface (Gets the class load address, gets the interface implemented by the class)returnProxy.newproxyinstance (Aa.getclass (). getClassLoader (), Aa.getclass (). Getinterfaces (), This); } intcount; Public intcount () {returncount; } @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {befor (Method,args);
Call this instance Object ad= Method.invoke ( This. Aa,args); return NULL; }
Different functions can be accomplished by overriding this method Public voidbefor (method, object[] args) {System.out.println ("----------"); }}
Test class:
Public class Testproxy {//usermanager a normal javabeenpublicstaticvoid main (string[] args) { Myproxydemo handler=new Myproxydemo (); Usermanager Usermanager= (usermanager) handler.newproxy (new Usermanagerimpl ()); Usermanager.adduser ("ad", "Da");} }
Java: A common use of reflection,