4 ms·
I think that's like asking if you use salt when you cook. I can't imagine how you'd get things done any other way. Here's a quick & dirty Java class file pars
by Dove 16y ago
I think that's like asking if you use salt when you cook. I can't imagine how you'd get things done any other way.
Here's a quick & dirty Java class file parser I have open in the other window, a representative sample of how I work. I can't imagine how such a thing could be written legibly liberal use of blank lines.
sub parse_class_file {
my($fn) = @_;
my ($name, $super);
# my $interfaces = []; # Not used yet
my $constant_pool = [];
my $fields = {}; # Referenced by name
my $methods = {}; # Referenced by name_description
open(C, "$fn") or die("Could not open '$fn': $!\n");
# Cafebabe
my $magic = read_field(\*C, "H*", 4);
# Class file format version
my $minor = read_field(\*C, "n");
my $major = read_field(\*C, "n");
# Load constant pool
load_constant_pool(\*C, $constant_pool);
# Class name, inheritance
my $access_flags = read_field(\*C, "n");
my $this_class_index = read_field(\*C, "n");
my $super_class_index = read_field(\*C, "n");
# Load interfaces -- just throw them away for right now
my $interface_count = read_field(\*C, "n");
for(my $i = 0; $i < $interface_count; $i++) { read_field(\*C, "n"); }
load_fields(\*C, $fields, $constant_pool);
load_methods(\*C, $methods, $constant_pool);
close(C);
return( { name => $constant_pool->[$this_class_index]->{name},
super => $constant_pool->[$super_class_index]->{name},
fields => $fields,
methods => $methods } );
}