Skip to content

[OSS26] 按方法名读取方法源码 read_method_source_by_method_name #137

Open

Description

@ponyha

接口介绍

给定方法的全限定名(可选形参类型用于重载消歧),精准返回该方法的源码片段(可选携带 Javadoc 与注解),不带文件其他部分;有重载且未消歧时返回所有候选。

工具名

read_method_source_by_method_name

背景与目标

文件级读取(read_file_by_path)是文件级粒度,LLM Agent 拿到上百行整文件后还要自己定位方法,token 开销大。本工具按"方法"为单位返回源码,是程序分析的最小颗粒上下文。

输入参数

参数 类型 必填 说明
method_full_name string 是 方法全限定名,如 com.example.UserController.getUser
parameter_types string[] 否 形参类型列表用于重载消歧;不填时:有重载则报错并返回所有候选
repository_path string 是 仓库本地绝对路径
include_javadoc bool 否 默认 true,是否带方法上方的 Javadoc 注释
include_annotations bool 否 默认 true,是否带方法上的注解

输出结构

{
  "method_full_name": "com.example.UserController.getUser",
  "method_signature": "getUser(HttpServletRequest, String): ResponseEntity<User>",
  "file_path": "src/main/java/com/example/UserController.java",
  "start_line": 87,
  "end_line": 105,
  "containing_class": "com.example.UserController",
  "source": "@GetMapping(\"/users/{id}\")\npublic ResponseEntity<User> getUser(HttpServletRequest req, @PathVariable String id) {\n    ...\n}",
  "javadoc": "/** Get a user by id */"
}

重载未消歧时:

{
  "method_full_name": "com.example.UserController.getUser",
  "found": false,
  "ambiguous": true,
  "candidates": [
    "getUser(HttpServletRequest, String): ResponseEntity<User>",
    "getUser(String): User"
  ]
}

验收标准

  • 正确返回方法的完整源码,包括嵌套 lambda/匿名类
  • 重载方法在不带 parameter_types 时返回 ambiguous + 候选
  • 注解、Javadoc 控制开关生效
  • 单元测试覆盖:重载、嵌套类方法、构造器、静态初始化块、抽象方法只声明
  • 中等仓库响应 < 3s

预估工作量

3-4 人日

Activity

  1. added
    OSS26-中等难度
    on Jun 18, 2026
  2. wryyyds7 commented on Jul 30, 2026

    @wryyyds7

    I can try it.

  3. wryyyds7 commented on Jul 30, 2026

    @wryyyds7

    Claiming #137. I plan to implement read_method_source_by_method_name as an MCP tool in yasa_mcp/tools/read_method_source_by_method_name.py that reads the complete source code of a Java method by its fully qualified name. It reuses the O(n) brace-depth state machine from #134 (_compute_line_depths) and method range extraction from #135 (_extract_method_ranges) to locate method body boundaries, then extracts source from the original file content. Key features: (1) overload disambiguation via parameter_types — returns ambiguous: true with candidate signatures when not specified; (2) Javadoc and annotation control via include_javadoc / include_annotations flags; (3) supports constructors (no return type), abstract methods (declaration only, single line), and static initialization blocks (static { ... }); (4) nested lambda/anonymous class bodies are included in the parent method source. File location uses package-path convention first (src/main/java/...), falling back to traversal with class name pattern matching. Tests cover overloads, nested classes, constructors, static init blocks, abstract methods, and integration with the java-design-patterns open-source project (< 3s response).

  4. wryyyds7 commented on Jul 30, 2026

    @wryyyds7

    Update: Implementation progress for read_method_source_by_method_name (#137)

    Summary

    Implemented read_method_source_by_method_name as an MCP tool in yasa_mcp/tools/read_method_source_by_method_name.py that reads the complete source code of a Java method by its fully qualified name, with overload disambiguation, Javadoc/annotation control, and support for constructors, abstract methods, and static initialization blocks.

    Key features

    Feature Implementation
    Method body boundary Reuses O(n) brace-depth state machine from #134 (_compute_line_depths) + enhanced _extract_all_method_ranges that accepts both { (method body) and ; (abstract/interface declaration)
    Overload disambiguation parameter_types list for exact match; returns ambiguous: true + candidate signatures when not specified
    Javadoc control include_javadoc=true (default) extracts /** ... */ above method as separate field; false omits it
    Annotation control include_annotations=true (default) includes @xxx lines in source; false starts source from method declaration
    Constructors Detected by absence of return type; User.User returns all overloads
    Abstract methods ; ending accepted; start_line == end_line (single line declaration)
    Static init blocks static { ... } detected via _STATIC_INIT_PATTERN; accessed as <static_init> method name
    Nested lambda/anonymous Included in parent method source (depth tracking ensures correct boundary)
    File location Package-path convention first (src/main/java/com/example/...), fallback to traversal with class name regex

    How it works

    1. Parse method_full_name → (package, class_name, method_name) via rsplit(".", 1)
    2. Locate file: try src/main/java/{package}/{class}.java, then traverse all .java files with class name pattern
    3. Strip comments (preserving newlines for line-number consistency) + compute brace depths O(n)
    4. Extract all class-body-level methods via _extract_all_method_ranges (filters by depth == class_depth + 1 to exclude method-internal calls)
    5. Filter candidates by method name + containing class
    6. If multiple candidates: disambiguate via parameter_types or return ambiguous
    7. Extract source from raw file content [source_start, actual_end] (annotations → method } line)
    8. Extract Javadoc separately by searching upward for /** ... */

    Verification against acceptance criteria

    Criterion Status Result
    Complete source including nested lambda/anonymous PASS lambda=True, anon=True, complete=True
    Overload returns ambiguous + candidates PASS ambiguous=True, 2 candidates with correct signatures
    Annotation/Javadoc control switches PASS include_javadoc=False omits javadoc; include_annotations=False omits @GetMapping
    Constructor support PASS User.User returns 2 constructor candidates
    Static init block support PASS <static_init> returns static { ... } source
    Abstract method declaration only PASS Single line, start_line == end_line, contains abstract
    Medium repo < 3s PASS JDP (1900 files): 0.43s

    Test coverage

    Category Tests Covers
    Full name parsing 4 Normal, inner class $, no package, empty
    Param type parsing 7 Simple, multiple, annotations, final, varargs, empty, generics
    Basic source read 5 Simple method, line range, signature, full body, relative path
    Overloading 3 Ambiguous without params, disambiguate first, disambiguate second
    Javadoc/annotations 4 Javadoc included, javadoc excluded, annotations included, annotations excluded
    Constructor 3 No-arg, ambiguous (2 ctors), disambiguated
    Static init block 2 Source content, line range
    Abstract method 2 Declaration only, concrete method in abstract class
    Nested structures 1 Lambda + anonymous class in method body
    Edge cases 4 Not found, class not found, empty name, nonexistent repo
    MCP registration 2 Auto-discovered, callable via MCP
    Integration (JDP) 3 Read method from open-source, toString, performance < 3s
    Integration (SF) 2 Read method from Spring, performance < 5s

    PR

    Will be created after tests pass.

  5. wryyyds7 commented on Jul 30, 2026

    @wryyyds7

    PR created: #154

  6. added 8 commits that reference this issue on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OSS26-中等难度

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions