Skip to content

Commit

Permalink
[java] Only emit methodObject when it's used
Browse files Browse the repository at this point in the history
See #195
  • Loading branch information
ojwb committed Mar 27, 2024
1 parent 87827d4 commit 34f3612
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 4 additions & 0 deletions compiler/analyser.c
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,9 @@ static struct node * make_among(struct analyser * a, struct node * p, struct nod
}
x->substring = substring;
if (substring != 0) substring->among = x;

if (x->function_count > 0) ++a->among_with_function_count;

return p;
}

Expand Down Expand Up @@ -1612,6 +1615,7 @@ extern struct analyser * create_analyser(struct tokeniser * t) {
a->program = 0;
a->amongs = 0;
a->among_count = 0;
a->among_with_function_count = 0;
a->groupings = 0;
a->mode = m_forward;
a->modifyable = true;
Expand Down
8 changes: 5 additions & 3 deletions compiler/generator_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,9 +1194,11 @@ static void generate_class_begin(struct generator * g) {
w(g, g->options->parent_class_name);
w(g, " {~+~N"
"~N"
"~Mprivate static final long serialVersionUID = 1L;~N"
"~Mprivate static final java.lang.invoke.MethodHandles.Lookup methodObject = java.lang.invoke.MethodHandles.lookup();~N"
"~N");
"~Mprivate static final long serialVersionUID = 1L;~N");
if (g->analyser->among_with_function_count > 0) {
w(g, "~Mprivate static final java.lang.invoke.MethodHandles.Lookup methodObject = java.lang.invoke.MethodHandles.lookup();~N");
}
w(g, "~N");
}

static void generate_class_end(struct generator * g) {
Expand Down
1 change: 1 addition & 0 deletions compiler/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ struct analyser {
struct among * amongs_end;
int among_count;
int amongvar_needed; /* used in reading routine definitions */
int among_with_function_count; /* number of amongs with functions */
struct grouping * groupings;
struct grouping * groupings_end;
struct node * substring; /* pending 'substring' in current routine definition */
Expand Down

0 comments on commit 34f3612

Please sign in to comment.