Java reflection techniques with class usage examples

  • 2020-04-01 03:14:22
  • OfStack


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[]{};
 }
 
 public Object[] getObjectArrays(Object obj) {
   ob = Arrays.copyOf(ob,ob.length + 1);
   ob[ob.length - 1] = obj;
   return ob;
 }
 
 @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;
  }
 }
 
 @SuppressWarnings("unchecked")
 public Object getClassNewInstance(Class<?> clazz)
   throws InstantiationException, IllegalAccessException,
   IllegalArgumentException, SecurityException,
   InvocationTargetException, NoSuchMethodException {
  Object oj = null;
  Constructor[] cons = clazz.getDeclaredConstructors();//Get the constructor
  Class[] cla = cons[1].getParameterTypes();
     System.out.println(" The user is prompted if a field needs to be added     The size of the constructor parameter :"+cla.length);
  for (int i = 0; i < cla.length; i++) {
   String classStr = cla[i].toString();
   //System.out.println(" type of parameter: "+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();//Returns the current object's concrete instantiation construct in the BDOperater
  return oj;
 }
 
 public Object getObjCon(Class<?> clazz) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
  Object obj=null;
   obj = this.getClassNewInstance(clazz);
  return obj;
 }
 
 public Object getNewinstance(Class clazz) throws InstantiationException, IllegalAccessException{
  Object obj = null;
  obj =  clazz.newInstance();
  return obj;
 }
 
 public Field[] getFielsdArray(Class<Object> clazz) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
  Field[] fields = null;
  fields = clazz.getDeclaredFields();
  return fields;
 }
    
 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;
 }
 
 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;
 }
 
 public Method[] getMethodsArray(Class clazz){
  Method[] methods = clazz.getMethods();
  return methods;
 }
 
 @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(" Call reflection: "+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"));
 }
}


Related articles: