欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java反射技术与类使用示例

程序员文章站 2024-02-25 19:33:45
复制代码 代码如下:package com.java.db;import java.lang.reflect.constructor;import java.lang.re...

复制代码 代码如下:

package com.java.db;
import java.lang.reflect.constructor;
import java.lang.reflect.field;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.arraylist;
import java.util.arrays;
import java.util.iterator;
import java.util.list;
import java.util.map;

import com.java.entity.bookshelf;
import com.java.util.getmetadatacloumname;
public class getnewinstances<t> {
 class[] cl = {};
 object[] ob = {};
 /**
  * 每次用完之后设为空 不然会累加
  */
 public void setnulltoarrays(){
  this.cl = new class[]{};
  this.ob = new object[]{};
 }
 /**
  * copy object数组
  *
  * @param obj
  *            构造方法里需要的实际值
  * @return
  */
 public object[] getobjectarrays(object obj) {
   ob = arrays.copyof(ob,ob.length + 1);
   ob[ob.length - 1] = obj;
   return ob;
 }

 /**
  * copy class 数组
  *
  * @param cla
  *            要添加的class
  *
  * @return
  */
 @suppresswarnings("unchecked")
 public class[] getclassarrays(class<?> cla) {
  if (cla != null) {
   cl = arrays.copyof(cl,cl.length + 1);
   cl[cl.length - 1] = cla;
   return cl;
  }else{
   return cl;
  }
 }

 /**
  * 得到类的实例
  *
  * @param clazz
  *            要实例化的类
  * @return 实例化之后的类
  * @throws instantiationexception
  * @throws illegalaccessexception
  * @throws illegalargumentexception
  * @throws securityexception
  * @throws invocationtargetexception
  * @throws nosuchmethodexception
  */
 @suppresswarnings("unchecked")
 public object getclassnewinstance(class<?> clazz)
   throws instantiationexception, illegalaccessexception,
   illegalargumentexception, securityexception,
   invocationtargetexception, nosuchmethodexception {
  object oj = null;
  constructor[] cons = clazz.getdeclaredconstructors();// 得到构造函数
  class[] cla = cons[1].getparametertypes();
     system.out.println("提示用户是否需要添加字段   构造函数参数的大小:"+cla.length);
  for (int i = 0; i < cla.length; i++) {
   string classstr = cla[i].tostring();
   // system.out.println("参数的类型:"+classstr);
   if (classstr.equals("class java.lang.string")) {
    getclassarrays(string.class);
   } else if (classstr.equals("int")) {
    getclassarrays(int.class);
   } else if (classstr.equals("double")) {
    getclassarrays(double.class);
   } else if (classstr.equals("boolean")) {
    getclassarrays(boolean.class);
   } else if (classstr.equals("float")) {
    getclassarrays(float.class);
   } else if (classstr.equals("class java.lang.integer")) {
    getclassarrays(integer.class);
   }else if(classstr.equals("class java.lang.float")){
    getclassarrays(float.class);
   }
  }
  oj =  clazz.newinstance();//返回当前对象 具体的实例化构造在bdoperater
  return oj;
 }
 /**
  * 通过构造函数得到具体的实例类
  * @param clazz
  * @return
  * @throws illegalargumentexception
  * @throws securityexception
  * @throws instantiationexception
  * @throws illegalaccessexception
  * @throws invocationtargetexception
  * @throws nosuchmethodexception
  */
 public object getobjcon(class<?> clazz) throws illegalargumentexception, securityexception, instantiationexception, illegalaccessexception, invocationtargetexception, nosuchmethodexception{
  object obj=null;
   obj = this.getclassnewinstance(clazz);
  return obj;
 }
 /**
  * 得到对象的实例
  * @param clazz
  * @return
  * @throws instantiationexception
  * @throws illegalaccessexception
  */
 public object getnewinstance(class clazz) throws instantiationexception, illegalaccessexception{
  object obj = null;
  obj =  clazz.newinstance();
  return obj;
 }
 /**
  * 根据反射得到类中的所有属性
  * @param clazz 需要被获取属性的类
  * @return 属性集合
  * @throws securityexception
  * @throws illegalargumentexception
  * @throws instantiationexception
  * @throws illegalaccessexception
  * @throws invocationtargetexception
  * @throws nosuchmethodexception
  */
 public field[] getfielsdarray(class<object> clazz) throws securityexception, illegalargumentexception, instantiationexception, illegalaccessexception, invocationtargetexception, nosuchmethodexception{
  field[] fields = null;
  fields = clazz.getdeclaredfields();
  return fields;
 }
    /**
     * 根据字符串得到setter格式的属性
     * @param str 需要格式化的属性
     * @return
     */
 public string getsetterstr(string str){
  string info = null;
  string strvalue = str.substring(0,1).touppercase();
  info = "set"+strvalue+str.substring(1,str.length());
  return info;
 }
 /**
  * 把setxx还原为xx
  * @param str
  * @return
  */
 public string setsetstr(string str){
  string info = null;
  string strvalue = str.substring(3,str.length());
  string lower = strvalue.substring(0).tolowercase().substring(0,1);
  info = lower+str.substring(4,str.length());
  return info;
 }
 /**
  * 得到类中的方法
  * @param clazz 需要的得到方法的类
  * @return
  */
 public method[] getmethodsarray(class clazz){
  method[] methods = clazz.getmethods();
  return methods;
 }
 /**
  * 根据list<map>实例化构造函数
  * @param listmap
  * @param clazz
  * @param tablename 数据库名称
  * @return
 * @throws nosuchmethodexception
 * @throws invocationtargetexception
 * @throws securityexception
 * @throws illegalargumentexception
 * @throws illegalaccessexception
 * @throws instantiationexception
  */
 @suppresswarnings({ "unchecked" })
 public list<object> getlistbymap(list<map<string,object>> listmap,class clazz,string tablename) throws instantiationexception, illegalaccessexception, illegalargumentexception, securityexception, invocationtargetexception, nosuchmethodexception{
  list<object> listlast = new arraylist<object>();
  list<string> metalist = getmetadatacloumname.getcloumnamelist(tablename);
  iterator<map<string,object>> it = listmap.iterator();
  while(it.hasnext()){
           map<string,object> map = it.next();
           iterator<string> iitt = metalist.iterator();
           while(iitt.hasnext()){
             string info = iitt.next();
             this.getobjectarrays(map.get(info));
           }
            system.out.println("调用反射:"+this.cl.length+"    "+this.ob.length);
           object tobj = this.getclassnewinstance(clazz).getclass().getconstructor(this.cl).newinstance(this.ob);
           listlast.add(tobj);
           this.setnulltoarrays();
  }
  return listlast;
 }
 public static void main(string[] args) {
  getnewinstances ge = new getnewinstances();
  system.out.println(ge.getsetterstr("namespace")=="setnamespace");
  system.out.println("1a"=="1a");
  system.out.println(ge.setsetstr("setnamespace"));
 }
}