java反射技术与类使用示例

时间:2021-09-14 06:34:34 


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

标签:java,反射
0
投稿

猜你喜欢

  • Java的Spring框架中bean的继承与内部bean的注入

    2023-06-17 18:50:44
  • C#判断一天、一年已经过了百分之多少的方法

    2022-07-16 15:23:11
  • String类型传递是值传递,char[]类型传递是引用传递的实现

    2022-06-01 09:33:44
  • 详解Java设计模式之备忘录模式的使用

    2023-09-10 09:38:32
  • C#操作XML文件步骤

    2021-11-04 21:51:44
  • Java List转换成String数组几种实现方式详解

    2023-11-10 07:19:41
  • Java实战之实现用户登录

    2022-08-03 14:42:55
  • java多线程和并发包入门示例

    2022-05-10 12:29:34
  • JavaSE static final及abstract修饰符实例解析

    2021-07-01 10:54:54
  • Java OOP三大特征之封装继承与多态详解

    2023-11-11 01:11:56
  • springboot 整合fluent mybatis的过程,看这篇够了

    2022-09-14 01:16:53
  • JavaFX实现简单日历效果

    2023-05-16 08:43:30
  • Java方法重载和重写原理区别解析

    2022-09-28 14:29:04
  • spring boot项目没有mainClass如何实现打包运行

    2021-10-24 11:29:20
  • 一文带你了解Java选择排序的原理与实现

    2022-05-13 21:01:31
  • Java自动生成趋势比对数据的方法分享

    2023-11-25 18:29:18
  • 基于javaWeb 项目SSM配置要点及可能遇到的问题和解决方法

    2023-10-27 21:45:16
  • Java中的字符串常量池详细介绍

    2023-03-08 09:16:41
  • javaweb Servlet开发总结(一)

    2023-04-08 22:52:32
  • Dwr3.0纯注解(纯Java Code配置)配置与应用浅析二之前端调用后端

    2023-08-19 17:32:33
  • asp之家 软件编程 m.aspxhome.com