设置静态字段值
Arthas提供getstatic或通过ognl表达式可以获得静态字段的值, 但是并没有提供类似setstatic的方法,那么如何通过Arthas设置静态字段的值呢?
考虑通过ognl反射实现。
bash
# 查看某个类的信息及字段
[arthas@9912]$ sc demo.MathGame -df
class-info demo.MathGame
code-source /C:/Users/A/.arthas/lib/3.6.7/arthas/math-game.jar
name demo.MathGame
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name MathGame
modifier public
annotation
interfaces
super-class +-java.lang.Object
class-loader +-jdk.internal.loader.ClassLoaders$AppClassLoader@6d06d69c
+-jdk.internal.loader.ClassLoaders$PlatformClassLoader@27245100
classLoaderHash 6d06d69c
fields name random
type java.util.Random
modifier private,static
value java.util.Random@3b73462c
name illegalArgumentCount
type int
modifier private
# 查看当前静态字段的值
[arthas@9912]$ ognl '@demo.MathGame@random'
@Random[
serialVersionUID=@Long[3905348978240129619],
seed=@AtomicLong[146325647347848],
multiplier=@Long[25214903917],
addend=@Long[11],
mask=@Long[281474976710655],
DOUBLE_UNIT=@Double[1.1102230246251565E-16],
BadBound=@String[bound must be positive],
BadRange=@String[bound must be greater than origin],
BadSize=@String[size must be non-negative],
seedUniquifier=@AtomicLong[-5237447342009718228],
nextNextGaussian=@Double[0.0],
haveNextNextGaussian=@Boolean[false],
serialPersistentFields=@ObjectStreamField[][isEmpty=false;size=3],
unsafe=@Unsafe[jdk.internal.misc.Unsafe@1e2957fa],
seedOffset=@Long[24],
]
# 修改静态字段random的值
[arthas@9912]$ ognl '#field=@demo.MathGame@class.getDeclaredField("random"),#field.setAccessible(true),#field.set(null, new java.security.SecureRandom())'
null
# 查看修改后静态字段的值 发现值已经修改成功
[arthas@9912]$ ognl @demo.MathGame@random'
@SecureRandom[
pdebug=null,
skipDebug=@Boolean[false],
provider=@Sun[isEmpty=false;size=140],
secureRandomSpi=@DRBG[Hash_DRBG,SHA-256,128,reseed_only],
threadSafe=@Boolean[true],
algorithm=@String[DRBG],
seedGenerator=null,
serialVersionUID=@Long[4940670005562187],
state=null,
digest=null,
randomBytes=null,
randomBytesUsed=@Integer[0],
counter=@Long[0],
serialVersionUID=@Long[3905348978240129619],
seed=@AtomicLong[0],
multiplier=@Long[25214903917],
addend=@Long[11],
mask=@Long[281474976710655],
DOUBLE_UNIT=@Double[1.1102230246251565E-16],
BadBound=@String[bound must be positive],
BadRange=@String[bound must be greater than origin],
BadSize=@String[size must be non-negative],
seedUniquifier=@AtomicLong[-5237447342009718228],
nextNextGaussian=@Double[0.0],
haveNextNextGaussian=@Boolean[false],
serialPersistentFields=@ObjectStreamField[][isEmpty=false;size=3],
unsafe=@Unsafe[jdk.internal.misc.Unsafe@1e2957fa],
seedOffset=@Long[24],
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84