Encapsulates a result set into an object and object collection with Java reflection

Source: Internet
Author: User

What is the Java reflection mechanism
反射机制是在运行状态中,可以知道任何一个类的属性和方法,并且调用类的属性和方法;
What the reflection mechanism can do

1, Judge the class of the running object
2. Construct an object of any class
3. Get the properties and methods of any class
4. Call any property and method
5. Generate Dynamic Agent

Use reflection to encapsulate a result set as an object or collection (actual measurements available)
 PackageCoral.base.util;ImportJava.beans.IntrospectionException;
ImportJava.beans.PropertyDescriptor;
ImportJava.lang.reflect.Field;
ImportJava.lang.reflect.InvocationTargetException;
ImportJava.lang.reflect.Method;
ImportJava.sql.ResultSet;
ImportJava.sql.SQLException;
ImportJava.util.ArrayList;
ImportJava.util.Date;
ImportJava.util.HashMap;
ImportJava.util.List;
ImportJava.util.Map;

ImportWfc.service.database.RecordSet;

Public class reflectutils {
/**
* Encapsulates a map collection as a Bean object

* * @param param
* @param clazz
* @return
*/
Public Static<T> TMaptobean(map<string, object> param, class<?> clazz) {
Object value =NULL;

class[] Paramtypes =Newclass[1];

Object obj =NULL;

Try{
obj = Clazz.newinstance ();

//Gets the properties of the class
field[] Declaredfields = Clazz.getdeclaredfields ();
//Gets the public property of the parent class or interface
field[] Superfields = Clazz.getsuperclass (). GetFields ();

list<field[]> list =NewArraylist<field[]> ();
if(Declaredfields! =NULL) {
List.add (Declaredfields);
}
if(Superfields! =NULL) {
List.add (Superfields);
}
for(field[] fields:list) {
for(Field field:fields) {
String fieldName = Field.getname ();
//Get the value corresponding to the attribute?
Value = Param.get (FieldName);
//Set the value into the object property there may be attributes but there is no corresponding set method, so do exception handling
Try{
PropertyDescriptor PD =NewPropertyDescriptor (
FieldName, Clazz);
Method method = Pd.getwritemethod ();
Method.invoke (obj,NewObject[] {value});
}Catch(Exception E1) {
}
}
}
}Catch(Exception E1) {
}return(T) obj;
}
/**
* Gets all the properties of the class, including the parent class and interface
* @param clazz
* @return
*/
Public StaticList<field[]>Getbeanfields(Class<?> clazz) {
list<field[]> list =NewArraylist<field[]> ();
field[] Declaredfields = Clazz.getdeclaredfields ();

field[] Superfields = Clazz.getsuperclass (). GetFields ();
if(Declaredfields! =NULL) {
List.add (Declaredfields);

}
if(Superfields! =NULL) {
List.add (Superfields);
}
returnList
}/**
* Get values from the result set
* @param fieldName
* @param RS
* @return
*/
Public StaticObjectGetFieldValue(String FieldName, ResultSet rs) {
Object value =NULL;
Try{
//The catch value does not exist exception
Value = Rs.getobject (FieldName);
returnValue
}Catch(SQLException e) {
//oracle The columns of the database are all uppercase, so it is only found once
FieldName = Fieldname.tolowercase ();
Try{

Value = Rs.getobject (FieldName);
returnValue
}Catch(SQLException E1) {
//The result set has no corresponding value and returns to null
return NULL;
}

}
}
/**
* Method Overloading,
* @param fieldName
* @param RS This is a packaged result set
* @return
*/
Public StaticObjectGetFieldValue(String FieldName, RecordSet rs) {
Object value =NULL;
Value = Rs.getobject (FieldName);
returnValue
}

/**
* Method Overloading
* @param RS Encapsulated result set
* @param clazz
* @return
*/
Public Static<T> TRstobean(RecordSet RS, class<?> clazz) {
Object obj =NULL;
list<field[]> list = Getbeanfields (Clazz);
Try{
obj = Clazz.newinstance ();

for(field[] fields:list) {
for(Field field:fields) {
String fieldName = Field.getname ();
//String FieldName = Field.getname ();?
Object value = GetFieldValue (FieldName, RS);
Try{
PropertyDescriptor PD =NewPropertyDescriptor (
FieldName, Clazz);
Method method = Pd.getwritemethod ();
Method.invoke (obj,NewObject[] {value});
}Catch(Exception E1) {

}
}
}
}Catch(Exception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
return(T) obj;
}
/**
* Encapsulate the result set as a Bean object
* @param RS
* @param clazz
* @return
*/
Public Static<T> TRstobean(ResultSet RS, class<?> clazz) {
Object obj =NULL;
list<field[]> list = Getbeanfields (Clazz);
Try{
while(Rs.next ()) {
obj = Clazz.newinstance ();


for(field[] fields:list) {
for(Field field:fields) {
String fieldName = Field.getname ();
//String FieldName = Field.getname ();?
Object value = GetFieldValue (FieldName, RS);
PropertyDescriptor PD =NewPropertyDescriptor (
FieldName, Clazz);
Method method = Pd.getwritemethod ();
Method.invoke (obj,NewObject[] {value});
}
}
}
}Catch(Exception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}return(T) obj;
}
/**
* Encapsulate the result set as a list

* * @param RS
* @param clazz
* @return
*/
Public Static<T> list<t>rstolist(ResultSet RS, class<?> clazz) {
Arraylist<t> objlist =NewArraylist<t> ();
//Get all the properties
list<field[]> list = Getbeanfields (Clazz);
Try{
while(Rs.next ()) {
//define temporary variables
Object tempobeject = Clazz.newinstance ();

//Add to attribute
for(field[] fields:list) {
for(Field field:fields) {
String fieldName = Field.getname ();
//Get property values?
Object value = GetFieldValue (FieldName, RS);
PropertyDescriptor PD =NewPropertyDescriptor (
FieldName, Clazz);
Method method = Pd.getwritemethod ();
Method.invoke (Tempobeject,NewObject[] {value});
}
}
Objlist.add ((T) tempobeject);
}
}Catch(Exception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
returnobjlist;
}}

Encapsulates a result set into an object and object collection with Java reflection

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.