程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java反射技巧與類應用示例

java反射技巧與類應用示例

編輯:關於JAVA

java反射技巧與類應用示例。本站提示廣大學習愛好者:(java反射技巧與類應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java反射技巧與類應用示例正文



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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved