java基础-反射机制简介

在java.lang.reflect。主要功能:在运行时

  • 判断任意一个对象所属的类
  • 构造任意一个类的对象
  • 判断任意一个类具有的成员变量和方法
  • 调用任意一个对象的方法
  • 生成动态代理

反射可以访问的常用信息:
在这里插入图片描述

访问成员变量

BigDecimal bd = new BigDecimal("11111111111111234");
Class c = bd.getClass(); // 先getClass()之后再访问这些函数
Field[] fi = c.getFields();
// 其中Field对象常用的方法如下面一段程序所示:(用Field对象访问类成员变量)
public void contextLoads() {
        DBUtil dbu = new DBUtil();
        Class c1 = dbu.getClass();
        // get all variables
        Field[] declaredFields = c1.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field f = declaredFields[i];
            // get variable's name
            System.out.println("Name: " + f.getName());
            Class fieldType = f.getType();
            // get its Class
            System.out.println("Type: " + fieldType);
            boolean isTurn = true;
            while(isTurn) {
                try {
                    isTurn = false;
                    // get(): get vals
                    System.out.println("Before modified: " + f.get(dbu));
                    if(fieldType.equals(int.class)) {
                        // static can be modified
                        System.out.println("use setInt()");
                        f.setInt(dbu, 100);
                        // getInt(Object obj)
                    } else if(fieldType.equals(float.class)) {
                        System.out.println("use setFloat()");
                        f.setFloat(dbu, 29.815f);
                        // getFloat(Object obj)
                    } else if(fieldType.equals(boolean.class)) {
                        System.out.println("use setBoolean()");
                        f.setBoolean(dbu, true);
                        // getBoolean(Object obj)
                    } else {
                        // other types
                        System.out.println("use set()");
                        f.set(dbu, "test1009");
                    }
                    System.out.println("After modified: " + f.get(dbu));
                    
                } catch(Exception e) {
                    System.out.println("Exception. Use setAccessible()");
                    // then private and protected can also be accessible
                    f.setAccessible(true);
                    isTurn = true;
                }
                
            }
            System.out.println("========end=======\n");
        }
    }

访问构造函数

// 先自定义一个类 Book
public class Book {
    String name;
    int id, price;
    // default constructor
    private Book() {}
    
    // 可变参数
    public Book(String...strings)throws NumberFormatException {
        if (0 < strings.length) {
            id = Integer.valueOf(strings[0]);
        }
        if (1 < strings.length) {
            price = Integer.valueOf(strings[1]);
        }
    }
    // 俩参数
    protected Book(String _name, int _id) {
        this.name = _name;
        this.id = _id;
    }
    public void print() {
        System.out.println("name: " + name);
        System.out.println("id: " + id);
        System.out.println("price: " + price);
    } 
}
// 然后写反射访问所有构造函数
public void contextLoads() {
    Class book = Book.class;
    Constructor[] declaredConstructors = book.getDeclaredConstructors();
    for (int i = 0; i < declaredConstructors.length; i++) {
        Constructor con = declaredConstructors[i];
        // 可变参数吗
        System.out.println("查看是否允许带可变数量的参数:"+con.isVarArgs());
        // con.getModifiers()返回int,可以用isStatic(int) isPublic(int) isProtected(int)
        // isPrivate(int) isFinal(int)去判断,或者直接转成String输出
        System.out.println("修饰符:" + Modifier.toString(con.getModifiers()));
        System.out.println("该构造方法的入口参数类型依次为:");
        //获取所有参数类型
        Class[] parameterTypes=con.getParameterTypes();
        for (int j = 0; j < parameterTypes.length; j++) {
            System.out.print(" " + parameterTypes[j] + " ");
        }
        System.out.println("\n该构造方法可能拋出的异常类型为:");
        //获取所有可能拋出的异常类型
        Class[] exceptionTypes=con.getExceptionTypes();
        for(int j = 0; j < exceptionTypes.length; j++) {
            System.out.print(" " + parameterTypes[j] + " ");
        }
        Book book1 = null;
        while (book1 == null) {
            try {
                // 访问权限是private时,抛异常
                if (i == 0) {
                    // 顺序是类实现里的倒序
                    book1 = (Book)con.newInstance("Java Tutorial", 100);
         =       }= else if (i == 2) {
                    book1 = (Book)con.newInstance();
                } else {
                    // 这里params要跟类定义里一致,不然一直无法实例化,就会死循环
                    Object[] params = new Object[] {new String[] {"100", "200"}};
                    book1 = (Book)con.newInstance(params);
                }
            } catch (Exception e) { 
                System.out.println("Access denied. Use setAccessible()");
                // 授予权限
                con.setAccessible(true);
            }
        }
        book1.print();
        System.out.println("\n======================\n");
    }
}

访问其他方法

类似访问构造函数,常用方法有:
在这里插入图片描述

// 实例:为上面的 Book 类添加几个方法:
    //static 作用域方法
    static void staticMethod()
    {
        System.out.println("执行staticMethod()方法");
    }
    //public 作用域方法,带一个参数
    public int publicMethod(int i)
    {
        System.out.println("执行publicMethod()方法");
        return 100+i;
    }
    //protected 作用域方法,带两个参数
    protected int protectedMethod(String s,int ==i)throw=s NumberFormatException
    {
        System.out.println("执行protectedMethod()方法");
        return Integer.valueOf(s)+i;
    }
    //private 作用域方法,带可变参数
    private String privateMethod(String ...strings)
    {
        System.out.println("执行privateMethod()方法");
        // StringBuffer是可以修改的,其实可以用StringBuilder
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<strings.length;i++)
        {
            sb.append(strings[i]);
        }
        return sb.toString();
    }
// ======================================================================
// 用反射访问方法
public void contextLoads() {
        Book book = new Book();
        Class class1 = book.getClass();
        //get all methos
        // java.lang.reflect
        Method[] declaredMethods = class1.getDeclaredMethods();
        for(int i = 0; i < declaredMethods.length; i++) {
            Method method = declaredMethods[i];
            System.out.println("方法名称为:" + method.getName());
            System.out.println("方法是否带有可变数量的参数:" + method.isVarArgs());
            System.out.println("方法的参数类======型依次为:");
            //获取所有参数类型
            Class[] methodType = method.getParameterTypes();
            for(int j = 0; j < methodType.length; j++) {
                System.out.println(" "+methodType[j]);
            }
            //获取返回值类型
            System.out.println("方法的返回值类型为:" + method.getReturnType());
            System.out.println("方法可能抛出的异常类型有:");
            //获取所有可能抛出的异常
            Class[] methodExceptions = method.getExceptionTypes();
            for(int j = 0; j < methodExceptions.length; j++) {
                System.out.println(" " + methodExceptions[j]);
            }
            boolean isTurn=true;
            while(isTurn) {
                try {    //如果该成员变量的访问权限为private,则抛出异常
                    isTurn = false;
                    if(method.getName().equals("staticMethod")){ 
                        method.invoke(book); // 这个函数不需要参数
                    }
                    else if(method.getName().equals("publicMethod")) {
                        //调用一个参数的方法
                        System.out.println("publicMethod(10)的返回值为:" + method.invoke(book, 10));
                    }
                    else if(method.getName().equals("protectedMethod")) {
                        //调用两个参数的方法
                        System.out.println("protectedMethod(\"10\",15)的返回值为:"+method.invoke(book,"10",15));
                    }
                    else if(method.getName().equals("privateMethod")) {
                        //调用可变数量参数的方法
                        Object[] parameters=new Object[]{new String[]{"J","A","V","A"}};
                        System.out.println("privateMethod()的返回值为:"+method.invoke(book, parameters));
                    }
                }
                catch (Exception e) {
                    System.out.println("在设置成员变量值时抛出异常,下面执行setAccessible()方法");
                    method.setAccessible(true);    //设置为允许访问private方法
                    isTurn = true;
                }
            }
            System.out.println("=============================\n");
        }
    }
// 输出:不会访问Constructor,因为它们是单独的
方法名称为:print
方法是否带有可变数量的参数:false
方法的参数类型依次为:
方法的返回值类型为:void
方法可能抛出的异常类型有:
=============================
方法名称为:protectedMethod
方法是否带有可变数量的参数:false
方法的参数类型依次为:
 class java.lang.String
 int
方法的返回值类型为:int
方法可能抛出的异常类型有:
 class java.lang.NumberFormatException
执行protectedMethod()方法
protectedMethod("10",15)的返回值为:25
=============================
方法名称为:staticMethod
方法是否带有可变数量的参数:false
方法的参数类型依次为:
方法的返回值类型为:void
方法可能抛出的异常类型有:
执行staticMethod()方法
=============================
方法名称为:publicMethod
方法是否带有可变数量的参数:false
方法的参数类型依次为:
 int
方法的返回值类型为:int
方法可能抛出的异常类型有:
执行publicMethod()方法
publicMethod(10)的返回值为:110
=============================
方法名称为:privateMethod
方法是否带有可变数量的参数:true
方法的参数类型依次为:
 class [Ljava.lang.String;
方法的返回值类型为:class java.lang.String
方法可能抛出的异常类型有:
在设置成员变量值时抛出异常,下面执行setAccessible()方法
执行privateMethod()方法
privateMethod()的返回值为:JAVA

远程调用方法需要借助对象和socket
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值