Skip to content

fix(parse_file): handle EnumDeclaration and AnnotationDeclaration in get_classes_and_methods#19

Merged
sheilaliuxl merged 2 commits into
amazon-science:mainfrom
luyuzhe111:fix_ast_node_type
May 21, 2026
Merged

fix(parse_file): handle EnumDeclaration and AnnotationDeclaration in get_classes_and_methods#19
sheilaliuxl merged 2 commits into
amazon-science:mainfrom
luyuzhe111:fix_ast_node_type

Conversation

@luyuzhe111
Copy link
Copy Markdown
Contributor

Summary

parse_file.get_classes_and_methods only sets current_class when it encounters ClassDeclaration or InterfaceDeclaration. Top-level enums and annotations fall through, so when the AST traversal reaches a method nested inside an enum constant's anonymous body, current_class is None and the function raises:

ValueError: Unable to find out class for method <name>

This crashes same_classes_and_methods (and any caller of get_classes_and_methods) on otherwise-valid Java sources.

Repos affected

Three repos in AmazonScience/migration-bench-java-full deterministically hit this crash during reward evaluation (8/8 rollouts each in one training step):

  • thomasmueller/tinyStats — top-level enum with abstract method overridden in each constant's anonymous body (the canonical reproducer).
  • justauth/JustAuth — same shape, different method name (authorize).
  • orphan-oss/ognl — same shape, different method name (getValue).

Fix

Add EnumDeclaration and AnnotationDeclaration to the dispatch in get_classes_and_methods, alongside two new module-level constants ENUM and ANNOTATION (mirroring the existing CLASS / INTERFACE pair):

elif isinstance(node, javalang.tree.EnumDeclaration):
    current_class = node.name
    nodes.append((ENUM, current_class))
elif isinstance(node, javalang.tree.AnnotationDeclaration):
    current_class = node.name
    nodes.append((ANNOTATION, current_class))

same_classes_and_methods compares the same file pre/post-migration, so the new tuple types align on both sides — no change required there.

Tests

Added two fixtures and two parametrized cases covering the new shapes:

  • testdata/Status.java — top-level enum with constants overriding an abstract
    method (matches the tinyStats shape that triggered the original crash).
  • testdata/Marker.java — top-level @interface with a method element.

The new cases are appended to the existing @parameterized.expand block on test_get_classes_and_methods and assert the expected tuple sequences:

  • Status.java(("enum", "Status"), ("method", ("describe", False)), ...)
  • Marker.java(("annotation", "Marker"),)

Local run: python -m unittest migration_bench.lang.java.eval.test_parse_file -v — 19/19 pass (5 pre-existing get_classes_and_methods cases + 2 new + 12 unrelated).

End-to-end validation

Beyond unit tests, validated using a vLLM-served Qwen3-Coder-30B as the migration agent:

  • thomasmueller/tinyStats rollout: pre-fix the reward function raisedValueError: Unable to find out class for method describe and the rollout errored out. Post-fix, the same repo runs end-to-end and produces a numeric reward (reward=1.0, build + test equivalence both pass) — the agent's full migration was successfully scored for the first time.
  • The other two enum/annotation repos (JustAuth, ognl) no longer crash; they now flow through the reward function normally and either succeed or fail on legitimate migration criteria.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.


CLASS = "class"
INTERFACE = "interface"
ENUM = "enum"
Copy link
Copy Markdown
Contributor

@sheilaliuxl sheilaliuxl May 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: They're sorted
Otherwise LGTM

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sheilaliuxl thanks! should be fixed now!

Comment thread src/migration_bench/lang/java/eval/test_parse_file.py
@sheilaliuxl sheilaliuxl merged commit 19acd9e into amazon-science:main May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants