Today, there is a scene, is a javabean, some of the value of the property needs to go to other tables in the database to obtain, so you need to call other DAO methods to get this value, and then set in.
But the problem is, if you need to assign a value in this way a lot of properties, a set in the need to write many set methods, the code is not only redundant, but also cumbersome.
So I want to go through the reflection mechanism to automatically set the value.
Assuming that there are javabean for the Creditratingfile.java class, some property values need to be obtained by calling methods in the Creditratingfileapplyservice class and getting the return value and then set out these properties.
First, get all the properties of JavaBean for Creditratingfile.java class
Second, to get more Creditratingfileapplyservice class method
Third, after matching and then according to Creditratingfile.java setter method set corresponding to the Creditratingfileapplyservice class method return value
Post the Code:
Reflection Tool Class:
Package com.krm.modules.creditFileApplay.util;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.LinkedHashMap;
Import java.util.List;
Import Java.util.Map;
Import Com.krm.modules.creditFileApplay.model.CreditRatingFile;
Import Com.krm.modules.creditFileApplay.service.CreditRatingFileApplyService;
Import com.thinkgem.jeesite.common.utils.Reflections;
public class Reflectutil {
@SuppressWarnings ("unused")
public static <E> E genvaluebygenerics (class<?> clazz,class<? > seviceclz,e entity) {
Map<string, field> resutlmap = new linkedhashmap<string, field> ();
for (; Clazz! = Object.class; clazz = Clazz.getsuperclass ()) {
field[] fields = Clazz.getdeclaredfields ();
for (field Field:fields) {
Resutlmap.put (Field.getname (), Field);
System.out.println (Field.getname ());
//Gets all the properties in JavaBean, matches the service all methods through the reflection mechanism
String Toservicemethodname = "Get" +field.getname (). substring (0, 1) . toUpperCase () +field.getname (). substring (1, field.getname (). Length ());
if ("GetCreditLimit2". Equals (toservicemethodname) | | "GetCreditRatingLevel3". Equals (Toservicemethodname)) {
Doservicemethod (seviceclz,toservicemethodname,entity, Field.getname ());
}
}
}
return entity;
}
public static void Bianli (Object obj) {
field[] fields = Obj.getclass (). Getdeclaredfields ();
for (int i = 0, len = fields.length; i < Len; i++) {
//For each property, get the property name
String varName = Fields[i].getname ();
try {
//Get the original access CONTROL permission
Boolean accessflag = Fields[i].isaccessible ();
Modify access control permissions
Fields[i].setaccessible (true);
Gets the variable
object o;
in object F of property Fields[i]. try {
o = fields[i].get (obj);
System.err.println ("The incoming object contains a variable as follows:" + varName + "=" + O);
} catch (Illegalaccessexception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
//Restore access control permissions
fields[i].setaccessible (accessflag)
} catch (IllegalArgumentException ex) {
Ex.printstacktrace ();
}
}
}
/**
* Get all methods
* @param clz
* @return
*/
public static list<string> Gensetmethodcode (class& lt;? > Clz) {
List<string> setmethods = new arraylist<> ();
method[] Declaredmethods = Clz.getdeclaredmethods ();
String name = Clz.getname ();
int dot = Name.lastindexof (".");
String objname = name.substring (dot+1, dot+2). toLowerCase () + name.substring (dot+2);
for (Method declaredmethod:declaredmethods) {
String methodName = Declaredmethod.getname ();
Setmethods.add (objname + "." + MethodName + "();");
}
for (string string:setmethods) {
System.out.println (string);
}
return setmethods;
}
/**
* Gets all methods of the specified service class and invokes the
* @param CLZ
*/
public static <E> void Doservicemethod (class<?> clz,string beanattribute,e entity,string PropertyName) {
method[] Declaredmethods = Clz.getdeclaredmethods ();
for (Method declaredmethod:declaredmethods) {
String methodName = Declaredmethod.getname ();
if (Beanattribute.equals (MethodName)) {
ReflectMethod (Methodname,clz,entity,propertyname);
}
}
}
public static <E> void ReflectMethod (String methodname,class<?> clz,e entity,string PropertyName) {
try {
You can pass a fixed path to a class, or you can pass the GetName () parameter of class.
Class<?> cls = Class.forName ("Com.krm.modules.creditFileApplay.service.CreditRatingFileApplyService");
Class<?> cls = Class.forName (Clz.getname ());
Object obj = cls.newinstance ();
The first parameter is the name of the called method, followed by the parameter type of the method
Class[] Argtypes=new class[1];
Argtypes[0]=map.class;
Method Setfunc = Cls.getmethod (methodname,argtypes);
When the method is obtained, the object and arguments of the class of the called method are passed through the Invoke method call, and the object can be obtained by cls.newinstance
map<string,object> map = new hashmap<string,object> ();
Gets the result of the specified method run
Object result = Setfunc.invoke (OBJ,MAP);
Call setter Method
Reflections.invokesetter (Entity, PropertyName, result);
} catch (Exception e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) {
Creditratingfile entity = new Creditratingfile ();
Entity.setcreditlimit3 ("444");
Genvaluebygenerics (Creditratingfile.class, creditratingfileapplyservice.class,entity);
SYSTEM.OUT.PRINTLN ("The attribute value obtained by the entity class through the reflection mechanism is:" +entity.getcreditlimit2 ());
SYSTEM.OUT.PRINTLN ("The attribute value obtained by the entity class through the reflection mechanism is:" +entity.getcreditratinglevel3 ());
SYSTEM.OUT.PRINTLN ("The attribute value obtained by the entity class through the reflection mechanism is:" +entity.getcreditlimit3 ());
}
}
Methods in the service class
public class Creditratingfileapplyservice extends Mybatisbaseservice<creditratingfileapply> {
@Resource
Private Creditratingfileapplydao Creditratingfileapplydao;
@Override
Public String Gettablename () {
Return Configutils.getvalue ("Schema.configplat") + ". Credit_rating_file_apply ";
}
@Override
Public String Getidkey () {
return "id";
}
@Override
Public mybatisbasedao<creditratingfileapply> Getdao () {
return Creditratingfileapplydao;
}
Public list<creditratingfileapply> Querybyparam (map<string, object> params) {
return Creditratingfileapplydao.querybyparam (params);
}
Public String Gethomevalue (map<string,object> params) {
list<string> list = Creditratingfileapplydao.gethomevalue (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String Getcarname (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getcarname (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String Getcarnum (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getcarnum (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String Getcarvalue (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getcarvalue (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String getassetsum (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getassetsum (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String GetBusinessAsset1 (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getbusinessasset1 (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String Getyear1familyincome (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getyear1familyincome (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String getyear1debtsum (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getyear1debtsum (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String Getyear2familyincome (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getyear2familyincome (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String getyear2debtsum (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getyear2debtsum (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
Public String Getyear3familyincome (map<string,object> params) {
list<string> list = Creditratingfileapplydao.getyear3familyincome (params);
if (null! = List&&list.size () >0) {
Return List.get (0);
}
Return "";
}
}
Java uses reflection mechanism to complete JavaBean property assignment