1.加载并获取该Class对象可以通过三种方式:

1.1:Class.forName(类的全路径)

 

 Class cl = Class.forName("Demo1.GetClass");

 

 

1.2:实例对象.getClass()方法

 

  Class c = model.getClass();

 

 

1.3:类名.class属性

 

        Class modelClass = TestModel.class;

 

2.进行反射测试的实体类

package Demo1;

import javax.annotation.Resource;
import java.io.InputStream;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.IllegalFormatCodePointException;

/**
 * Created by 123 on 2017/07/16.
 */
public class GetClass implements Serializable, Runnable {

    private String name;
    @Resource
    private int age;


    public GetClass() {
    }

    public GetClass(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    public String getStr(String name) throws SQLException, IllegalFormatCodePointException {
        return name;
    }


    public void run() {

    }
}

 

2.1——————–>cl就是class对象

 

 public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class cl = Class.forName("Demo1.GetClass");
        
        //包名
        GetPackNameAndClassName(cl);

        //获取父类名称+所有的接口
        getSuperAndInterface(cl);

        //字段
        getManyFields(cl);

        //获取方法
        getAllMethod(cl);

        //调用方法
        GoMethod(cl);

        //获取构造
        GetConstructors(cl);

        //方法抛出的异常
        getExceptions(cl);

        //拿到注解
        getManyAnnotions(cl);
    }

 

3.获取包名

 

 public static void GetPackNameAndClassName(Class cl) {
        System.out.println("包名" + cl.getPackage().getName());
    }

 

4.获取父类名称+所有的接口

public static void getSuperAndInterface(Class cl) {
        System.out.println("此类父类的类名是:" + cl.getSuperclass().getName());
        Class[] interfaces = cl.getInterfaces();
        StringBuffer sb = new StringBuffer("此类实现的接口有:");
        for (Class cs : interfaces) {
            sb.append(cs.getSimpleName());
        }
        System.out.println(sb.substring(0, sb.length() - 1));
    }

 

5.获取字段

 private static void getManyFields(Class cl) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        StringBuffer sb = new StringBuffer("此类中的字段有:");
        Field[] declaredFields = cl.getDeclaredFields();

        Field name = cl.getDeclaredField("name");
        System.out.println("name字段" + name.getName());


        name.setAccessible(true);

        Object o = cl.newInstance();
        System.out.println("name字段的值为:" + name.get(o));


        for (Field item : declaredFields) {
            //忽略字段privet
            item.setAccessible(true);
            sb.append("若多字段" + item.getName() + ",");
        }
        System.out.println(sb.substring(0, sb.length() - 1));
    }

 

6.调用方法

 private static void GoMethod(Class cl) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Method method = cl.getMethod("getStr", String.class);
        Object obj = cl.newInstance();
        Object invoke = method.invoke(obj, "厉害了");
        System.out.println(invoke);
    }

 

7.获取或执行构造

 private static void GetConstructors(Class cl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Constructor[] cs = cl.getConstructors();
        StringBuffer sb = new StringBuffer("此类中的构造有:");
        for (Constructor item : cs) {
            sb.append(item.getName() + ",");
        }
        System.out.println(sb.substring(0, sb.length() - 1));

        //执行带参构造
        Constructor cs1 = cl.getConstructor(String.class, Integer.class);
        //使用newinstance函数执行构造
        cs1.newInstance("呵呵", 18);

    }

 

8.获取方法抛出的异常

private static void getExceptions(Class cl) throws NoSuchMethodException {
        Method getStr = cl.getMethod("getStr", String.class);
        Class[] exces = getStr.getExceptionTypes();
        StringBuffer sb = new StringBuffer("此方法中抛出的异常有:");
        for (Class item : exces) {
            sb.append(item.getName() + ",");
        }
        System.out.println(sb.substring(0, sb.length() - 1));
    }

 

9.获取注解

private static void getManyAnnotions(Class cl) throws NoSuchFieldException {
        Field age = cl.getDeclaredField("age");
        Resource ans = age.getAnnotation(Resource.class);
        System.out.println(ans);
    }

 

原文地址:http://www.cnblogs.com/quliang/p/7223946.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性