日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Field setAccessible()方法的作用及應(yīng)用場(chǎng)景

作者:咖啡不苦** 更新時(shí)間: 2022-06-08 編程語(yǔ)言

一、更改string的value值,但是其指向不變

曾經(jīng)看到過(guò)這樣的一個(gè)題目:

public static void main(String[] args) {
String s=new String(“abc”);
//在這中間添加N行代碼,但是必須保證s引用的指向不變,最終將輸出變成abcd
System.out.println(s);
}

看到如上題目,該如何加代碼,能讓其指向不變呢?解決辦法如下:

String s = new String(“abc”);
Field value = s.getClass().getDeclaredField(“value”);
value.setAccessible(true);
value.set(s, “abcd”.toCharArray());
System.out.println(s);

采用了反射機(jī)制實(shí)現(xiàn)了其功能實(shí)現(xiàn),我們發(fā)現(xiàn)代碼中有一個(gè)setAccessible(true);如果去掉這句話會(huì)如何呢?

Exception in thread "main" java.lang.IllegalAccessException: Class FileTest can not access a member of class java.lang.String with modifiers "private final"
	at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
	at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
	at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
	at java.lang.reflect.Field.set(Field.java:761)
	at FileTest.main(FileTest.java:17)

Class FileTest can not access a member of class java.lang.String with modifiers "private final"意思就是類 FileTest 不能訪問(wèn)帶有修飾符“private final”的類 java.lang.String 的成員。

二、實(shí)體demo 獲取成員變量的值

1)私有成員變量

在平常創(chuàng)建正常model使用過(guò)程中,都是設(shè)置private類型的成員變量

public class Student {
    private String name;
    private Integer age;
        Student student=new Student();
		student.setName("huo");
		Field value1=Student.class.getDeclaredField("name");
		// value1.setAccessible(true);
		System.out.println(value1.isAccessible());
		System.out.println(value1.get(student));

執(zhí)行如上代碼,會(huì)輸出什么?

false
Exception in thread "main" java.lang.IllegalAccessException: Class FileTest can not access a member of class Student with modifiers "private"
	at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
	at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
	at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
	at java.lang.reflect.Field.get(Field.java:390)
	at FileTest.main(FileTest.java:27)

其實(shí)這種情況和上述對(duì)string的demo是一樣的。 那么我們能如何獲取值呢?
兩種辦法:

  1. 將私有變量改為公有變量
  2. 配置 value1.setAccessible(true);

2)公有成員變量

public class Student {
    public String name;
    public Integer age;

看執(zhí)行結(jié)果:

false
huo

3)setAccessible

        Student student=new Student();
		student.setName("huo");
		Field value1=Student.class.getDeclaredField("name");
		value1.setAccessible(true);
		System.out.println(value1.isAccessible());
		System.out.println(value1.get(student));

看執(zhí)行結(jié)果:

true
huo

4)成員變量為public,isAccessible會(huì)輸出什么?

可以測(cè)試一下,答案為true,為什么呢?

三、setAccessible的作用到底是什么?

在這里插入圖片描述
看如上解釋,清晰明了。為TRUE表示反射在使用時(shí)應(yīng)禁止java語(yǔ)言訪問(wèn)檢查,值為false表示反射對(duì)象應(yīng)強(qiáng)制執(zhí)行java語(yǔ)言訪問(wèn)檢查。

這樣就能很明了設(shè)置了setAccessible為true之后,就能編輯final變量以及訪問(wèn)private變量了。
下篇講述java API之AccessibleObject,也就是Field的父類到底干了點(diǎn)什么?

原文鏈接:https://blog.csdn.net/huo065000/article/details/119919185

欄目分類
最近更新