java反射技術與類使用樣本_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

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"));
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.