-
1. Re: Get a private final static field
chiba Feb 5, 2005 11:30 AM (in response to dechamps)You can obtain the value of a final static field if you go down
to the lower-level API of Javassist (I agree that the next
release of Javassist support this function with the regular
API).
You must get a java.bytecode.FieldInfo object from CtField
and then obtain a ConstantAttribute object from that
FieldInfo. Finally, you must search a constant pool in a
class file to find the field value.
Note that this way works only for some basic types of fields,
for example, int or String. -
2. Re: Get a private final static field
dechamps Feb 8, 2005 4:23 PM (in response to dechamps)Thanks you Chiba, I'll do my best with the low-level API ....
I have tried the solution you give.
My field private final static String JDEdoc was created thru javassist using a static initializer.
Here under the output I obtain trying to get the value of JDEdoc in the class bytecode. According to the output,the JDEdoc field has no Attribute and no index in the Constant pool
Using the "ByteCode Viewer" tool, I can see the value in the clinit code
....
ldc #22 <..1p1yga ....here is the value I need .....>
invokespecial #25 <java/lang/String.>
putstatic #18 <JA/Tools/jdep/test/BCLASS.JDEdoc>
...
So I have to decompile the clinit bytecode and fetch inside it the relationship beetween entry 14 and entry 21?
Scanning for LDC/INVOKESPECIAL/PUTSTATIC sequence in clinit,I can obtain the value I need.
0=aconst_null
1=astore_1
2=jsr
5=return
6=astore_0
7=new
10=dup
11=ldc #22 1p1yga .........here is the value I need .....
13=invokespecial 25
16=putstatic 18 JDEdoc
Sequence found
FieldName=JDEdoc
Value=1p1ygaqw3uj ........here is the value I need .....
In that case, is there an easiest way to proceed?
Is it possible to find directly in constant pool the relation #22 <==> #18?
Dominique
//// THE OUTPUT ////
Field JDEdoc descriptor is Ljava/lang/String;
Field JDEdoc is private static final
Field JDEdoc has a constant_pool table.
1 Method #3, name&type #10
2 Class #11
3 Class #12
4 UTF8 ""
5 UTF8 "()V"
6 UTF8 "Code"
7 UTF8 "LineNumberTable"
8 UTF8 "SourceFile"
9 UTF8 "BCLASS.java"
10 NameAndType #4, type #5
11 UTF8 "JA/Tools/jdep/test/BCLASS"
12 UTF8 "java/lang/Object"
13 UTF8 ""
14 UTF8 "JDEdoc"
15 UTF8 "Ljava/lang/String;"
16 Class #11
17 NameAndType #14, type #15
18 Field #16, name&type #17
19 UTF8 "java/lang/String"
20 Class #19
21 UTF8 "1p1yga ....here is the value I need ...."
22 String #21
23 UTF8 "(Ljava/lang/String;)V"
24 NameAndType #4, type #23
25 Method #20, name&type #24
26 UTF8 "getDocumentation"
27 UTF8 "()Ljava/lang/String;"
28 NameAndType #14, type #15
29 Field #16, name&type #28
30 UTF8 "JA/Utils/Gzip"
31 Class #30
32 UTF8 "gunzip36"
33 UTF8 "(Ljava/lang/String;)[B"
34 NameAndType #32, type #33
35 Method #31, name&type #34
36 UTF8 "([B)V"
37 NameAndType #4, type #36
38 Method #20, name&type #37
39 UTF8 "JA/Interfaces/JDEDocumentation"
40 Class #39
41 UTF8 "JA/Interfaces/JDESource"
42 Class #41
43 UTF8 "JA/Interfaces/JDE"
44 Class #43
Field JDEdoc has no index into the constant_pool table.
FieldInfo for Field JDEdoc has 0 Attributes
Field JDEdoc has a null ConstantValue