1. What is reflection?
Reflection, Reflection, through which we can get a variety of information at run time, such as assemblies, modules, types, fields, properties, methods, and events.
Frame Composition:
:
2. What are the usual methods of reflection?
Package com.wzw.text;
public class Person {
private String name;
private int age;
Private job Job;
Public Job Getjob () {
return job;
}
public void Setjob (Job job) {
This.job = job;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public person () {
}
}
A simple UI test:
Package Com.wzw.text;import Java.lang.reflect.field;import java.lang.reflect.invocationtargetexception;import Com.wzw.modal.titleinfo;import Com.wzw.modal.userinfo;import Com.wzw.util.Reflect; Public classTextui { Public Static voidMain (string[] args) throws Nosuchmethodexception, SecurityException, Illegalaccessexception, IllegalArgumentException, InvocationTargetException {//TODO auto-generated Method StubClass Classzz = person.class; Field[] Fields=Classzz.getdeclaredfields (); for(Field f:fields) {Class C=F.gettype (); if(C.getpackage ()! =NULL){ if(C.getpackage (). GetName (). Equals ("Com.wzw.text") ) {System. out. println (C.getpackage (). GetName ()); System. out. println ("Type:"+c.getname ()); System. out. println ("Name:"+f.getname ()); }}} String userinfosql= Reflect.getselectsql (NewUserInfo ()); System. out. println (Userinfosql); }}
Lookup related code for reflection:
Package Com.wzw.util;import java.lang.reflect.*;p Ublic class reflect {public static String getselectsql (Object obj) thr oWS nosuchmethodexception, SecurityException, Illegalaccessexception, IllegalArgumentException, invocationtargetexception{class< extends object> classzz = Obj.getclass (); String tableName = Classzz.getsimplename (); StringBuffer sbsql = new StringBuffer (); Sbsql.append ("SELECT * from" +tablename+ ""); Sbsql.append ("Where 1=1"); field[] fields = Classzz.getdeclaredfields ();//Gets the field defined for (field F:fields) {String methodName = "Get" + f.getname (). Sub String (0,1). toUpperCase () +f.getname (). substring (1); Method M = Classzz.getdeclaredmethod (methodName);//Gets a method Object o = M.invoke (obj); if (o!=null) {if (o instanceof String) {sbsql.append ("and" +f.getname () + "= '" +o+ "'"); }else{sbsql.append ("and" +f.getname () + "=" +o); }}} return Sbsql.tostring (); }/*public static String getUpdate (Object obj) throws IllegalargumentexceptiOn, illegalaccessexception{class<? extends object> classzz = Obj.getclass (); String sql= "Update" + classzz.getsimplename () + "Set"; String sql2= ""; String sql3= "Where"; field[] fields = Classzz.getdeclaredfields (); for (Field f:fields) {f.setaccessible (true);//The private value can be accessed by Object value = F.get (obj); If the field has PrimaryKey annotation if (f.isannotationpresent (Primarykey.class)) {if (value instanceof String) {sql3 + = F.getname () + "=" "+value+" "; }else{Sql3 + = F.getname () + "=" +value+ ""; }}else if (!f.isannotationpresent (Nonfield.class)) {if (value instanceof String) {sql2 + = F.getname () + "= '" +value+ "', "; }else{sql2+=f.getname () + "=" +value+ ","; }}} sql2 = Sql2.substring (0,sql2.length ()-1); sql = sql + SQL2 +sql3; return SQL; }*/}
Application Scenarios for reflection:
Reflection in the application is mostly associated with configuration files, features and other elements, next we look at some configuration node fragment code. Configure node One "test" type="mymodule.myhttpmodule, Myhttpmodule"/> Configuration Node Two"ajaxpro" verb="post,get " Path="ajaxpro/*.ashx " type="Ajaxpro.ajaxhandlerfactory,ajaxpro "/>These two configuration nodes believe everyone is not unfamiliar, node one is to register a custom processing module for the ASP. NET processing pipeline process.
The second configuration node registers a custom handler for a specific file in an ASP. NET specific directory.
The example here is the Ajaxpro configuration section. For httpmodules and HttpHandler understand the deviation of the place to welcome everyone to correct, we continue down.
Most of the time for such a configuration file we only know to this configuration, the program will not be able to think of a reason after running.
Each of the two add configuration nodes has a type="xxx.xxx,xxxx"property, the property value is configured with the"namespace. Class name, assembly name".
It just fits our reflection. The requirement to create an object, the value of the configuration node tells us the name of the Assembly and the information of the class.
Typically, reflection can also be combined with Factory mode to form a reflection factory, and ASP. NET will read the Web. config profile information when the application starts.
and to load the required assemblies, and then to create the objects of our registered httpmodules and HttpHandler types by reflection.
This makes it easy to implement our custom business logic in httpmodules and HttpHandler. There are a lot of similar configuration nodes in our configuration files, but they are basically consistent, that is, by using reflection to dynamically create objects,
Implement pluggable policies for framework components to make programs more flexible and scalable.
Annotations? What is a common specification for annotations and annotations?
Java Reflection Knowledge