zoukankan      html  css  js  c++  java
  • JAVA反射获取类的属性及类型

    通过反射获取类属性字段 以及 调用类方法
    public class ModelClassHelper {

        public static HashMap<String,Class> init(String classPath)
        {
            try {
                //"com.geocompass.model.STSTBPRPModel"
                HashMap<String,Class> fieldHashMap=new HashMap<String,Class>();
                Class cls = Class.forName(classPath);  //com.geocompass.model.STSTBPRPModel
                Field[] fieldlist = cls.getDeclaredFields();
                for (int i = 0; i < fieldlist.length; i++) {
                    Field fld = fieldlist[i];
                    fieldHashMap.put(fld.getName(), fld.getType());
    //                System.out.println("name = " + fld.getName());
    //                System.out.println("decl class = " + fld.getDeclaringClass());
    //                System.out.println("type = " + fld.getType());
    //                System.out.println("-----");           
                    }
                return fieldHashMap;
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
     

     public static String getTableName(String classPath)
     {
      try {
       Class cls = Class.forName(classPath);
       Method test=cls.getDeclaredMethod("getTableName" );
       Object invoke = test.invoke(cls.newInstance());
       return invoke.toString();
       //cls.asSubclass(TabModel.class);
      } catch (ClassNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (SecurityException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (NoSuchMethodException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IllegalArgumentException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IllegalAccessException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (InvocationTargetException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (InstantiationException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } 
      return "";
     }}
     

    package org.apache.easframework.core.entity.impl;

    import java.lang.reflect.Field;

    public class TestEntity {
     
     private String code;
     private String name;
     
     public void setCode(String code)
     {
      this.code = code;
     }
     
     public String getCode()
     {
      return this.code;
     }
     
     public void setName(String name)
     {
      this.name = name;
     }
     
     public String getName()
     {
      return this.name;
     }
     
     public static void main(String[] args) throws SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException
     {
      TestEntity obj = new TestEntity();
      obj.setName("name value");
      obj.setCode("code value");
      Field[] fds = Class.forName
      ("org.apache.easframework.core.entity.impl.TestEntity").getDeclaredFields();
      
      System.out.println(fds.length);
      for(int i=0;i<fds.length;i++)
      {
       System.out.println(fds[i].get(obj));
       
      }
     }

    }

    转载自:

    http://blog.csdn.net/wpcxyking/article/details/6139549
     
     
  • 相关阅读:
    Hibernate 之核心接口
    [综]隐马尔可夫模型Hidden Markov Model (HMM)
    [zz]利用碎片时间健身
    [zz]简单有效,在家就能锻炼!
    matlab global 不能传向量/矩阵
    [zz] 基于国家标准的 EndNote 输出样式模板
    [zz] ROC曲线
    [ZZ] Equal Error Rate (EER)
    [综] Sparse Representation 稀疏表示 压缩感知
    [zz] Principal Components Analysis (PCA) 主成分分析
  • 原文地址:https://www.cnblogs.com/wuhenke/p/2382936.html
Copyright © 2011-2022 走看看