[antlr-interest] Java and static arrays: a serious joke
Terence Parr
parrt at cs.usfca.edu
Fri Jun 2 15:41:59 PDT 2006
I have to laugh...Java is really showing growing pains. I cannot
allocate even small static arrays because it requires 6 bytes of code
for each byte I want to put into a static array. Yes, Java makes a
class init method that manually builds these arrays! At least it
doesn't seem to wipe to zero first (or does it!? ack) ;)
See http://www.jroller.com/page/rreyelts for a good explanation. See
March 16th entry.
public class T {
static short[] a = {10, 20};
}
generates:
static {};
Code:
0: iconst_2
1: newarray short
3: dup
4: iconst_0
5: bipush 10 <- YIKES!
7: sastore
8: dup
9: iconst_1
10: bipush 20
12: sastore
13: putstatic #2; //Field a:[S
16: return
Same thing, but in ctor when nonstatic. :(
So, what the HELL are we going to do in order to store transition
tables for DFA in Java? Load the data from the disk? gawwwd!
Seriously? How can you load static data into a Java class?
Regarding all that extra code laying around, one comment on that page
says:
"So the obvious answer is to put the initializer into a separate
class, and to always create a new ClassLoader to load that class, and
let the ClassLoader and class code be garbage-collected after you've
created your initial instance of the array which you can then clone()
at will. "
But that doesn't solve the issue that you still have to get it to FIT
into a single class.
AH HA! Just looked at JFLex..they store them as strings, which are
UTF8 in the class file.
public class T {
static final String duh = "\1\2\3";
}
generates everything directly in the classfile constant pool!
Hooray! Now I have to encode, decode, but it's doable! Thanks, JFlex!
Ter
More information about the antlr-interest
mailing list