Skip to content

Commit

Permalink
class: added functions for looking up fields and methods
Browse files Browse the repository at this point in the history
Signed-off-by: Vegard Nossum <[email protected]>
  • Loading branch information
Vegard Nossum committed Jun 5, 2008
1 parent 2bb0337 commit 69b74ef
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/cafebabe/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ int cafebabe_class_constant_get_utf8(struct cafebabe_class *c, uint16_t i,
int cafebabe_class_constant_get_class(struct cafebabe_class *c, uint16_t i,
struct cafebabe_constant_info_class **r);

int cafebabe_class_get_field(struct cafebabe_class *c,
const char *name, const char *descriptor,
struct cafebabe_field_info **r);
int cafebabe_class_get_method(struct cafebabe_class *c,
const char *name, const char *descriptor,
struct cafebabe_method_info **r);

#endif
3 changes: 3 additions & 0 deletions include/cafebabe/constant_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@ int cafebabe_constant_pool_init(struct cafebabe_constant_pool *cp,
struct cafebabe_stream *s);
void cafebabe_constant_pool_deinit(struct cafebabe_constant_pool *cp);

int cafebabe_constant_info_utf8_compare(
const struct cafebabe_constant_info_utf8 *s1, const char *s2);

#endif
74 changes: 74 additions & 0 deletions src/cafebabe/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,77 @@ cafebabe_class_constant_get_class(struct cafebabe_class *c, uint16_t i,
*r = &pool->class;
return 0;
}

int
cafebabe_class_get_field(struct cafebabe_class *c,
const char *name, const char *descriptor,
struct cafebabe_field_info **r)
{
for (uint16_t i = 0; i < c->fields_count; ++i) {
struct cafebabe_constant_info_utf8 *field_name;
if (cafebabe_class_constant_get_utf8(c,
c->fields[i].name_index, &field_name))
{
return 1;
}

if (cafebabe_constant_info_utf8_compare(field_name, name))
continue;

struct cafebabe_constant_info_utf8 *field_descriptor;
if (cafebabe_class_constant_get_utf8(c,
c->fields[i].descriptor_index, &field_descriptor))
{
return 1;
}

if (cafebabe_constant_info_utf8_compare(
field_descriptor, descriptor))
{
continue;
}

*r = &c->fields[i];
return 0;
}

/* Not found */
return 1;
}

int
cafebabe_class_get_method(struct cafebabe_class *c,
const char *name, const char *descriptor,
struct cafebabe_method_info **r)
{
for (uint16_t i = 0; i < c->methods_count; ++i) {
struct cafebabe_constant_info_utf8 *method_name;
if (cafebabe_class_constant_get_utf8(c,
c->methods[i].name_index, &method_name))
{
return 1;
}

if (cafebabe_constant_info_utf8_compare(method_name, name))
continue;

struct cafebabe_constant_info_utf8 *method_descriptor;
if (cafebabe_class_constant_get_utf8(c,
c->methods[i].descriptor_index, &method_descriptor))
{
return 1;
}

if (cafebabe_constant_info_utf8_compare(
method_descriptor, descriptor))
{
continue;
}

*r = &c->methods[i];
return 0;
}

/* Not found */
return 1;
}
8 changes: 8 additions & 0 deletions src/cafebabe/constant_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cafebabe/constant_pool.h"
#include "cafebabe/stream.h"
Expand Down Expand Up @@ -347,3 +348,10 @@ cafebabe_constant_pool_deinit(struct cafebabe_constant_pool *cp)
break;
}
}

int
cafebabe_constant_info_utf8_compare(
const struct cafebabe_constant_info_utf8 *s1, const char *s2)
{
return strncmp((const char *) s1->bytes, s2, s1->length);
}
7 changes: 7 additions & 0 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ main(int argc, char *argv[])
method_name->length, method_name->bytes);
}

struct cafebabe_method_info *main_method;
if (!cafebabe_class_get_method(&class,
"main", "([Ljava/lang/String;)V", &main_method))
{
printf("class has main method\n");
}

cafebabe_class_deinit(&class);
cafebabe_stream_close(&stream);

Expand Down

0 comments on commit 69b74ef

Please sign in to comment.