Skip to content

feat: [OSS26] 列出类的全部方法 list_methods_by_class_name (#138) - #155

Open
wryyyds7 wants to merge 6 commits into
antgroup:mainfrom
wryyyds7:feat/oss26-list-methods-by-class-name-138
Open

feat: [OSS26] 列出类的全部方法 list_methods_by_class_name (#138)#155
wryyyds7 wants to merge 6 commits into
antgroup:mainfrom
wryyyds7:feat/oss26-list-methods-by-class-name-138

Conversation

@wryyyds7

Copy link
Copy Markdown

概述

实现 list_methods_by_class_name MCP 工具,给定类全限定名,一次性返回该类的所有方法清单(签名、可见性、起止行号、注解),可选附带方法源码或仅返回签名。

实现

方法范围提取

复用 #137_extract_all_method_ranges,提取所有类体级别的方法(含构造器、静态初始化块、抽象方法)。通过 depth == class_depth + 1 过滤,天然排除方法体内的方法调用和内部类方法。

可见性和 static 提取

_extract_visibility_and_static() 从方法声明行前缀的 token 列表中解析:

  • visibility: public / protected / private / package (无修饰符时为 package-private)
  • is_static: 是否含 static 关键字

类元数据提取

_extract_class_metadata() 从类声明行解析:

  • extends_class: 父类名(从 extends Xxx 提取)
  • implements_interfaces: 接口列表(从 implements Aaa, Bbb 提取)
  • class_start_line / class_end_line: 类的起止行号

嵌套类隔离

通过 containing_class == 目标类全限定名 过滤,只返回目标类的方法。_extract_all_method_ranges 的 depth 过滤已经排除了内部类的方法(内部类方法的 depth 比 Outer 类体深度大 1)。

开关控制

  • include_private=false: 过滤掉 visibility 为 private 的方法
  • include_source=false: 不返回 source 字段,轻量版
  • include_inherited=true: 查找父类/接口的方法(只返签名 + inherited_from,不返 source)

继承方法查找

_find_inherited_methods() 递归查找 extends 父类和 implements 接口的方法:

  • 解析父类/接口文件,提取其方法签名
  • 标记 inherited_from 字段为父类/接口全限定名
  • 去重(seen_signatures)

复用 #137 的核心组件

  • _extract_all_method_ranges: 方法范围提取(含构造器、静态初始化块、抽象方法)
  • _find_class_file: 文件定位(包路径约定 + 遍历查找)
  • _extract_method_info: 方法信息提取(返回类型、参数类型、是否构造器/抽象)
  • _build_method_signature: 方法签名构建
  • _extract_annotations: 注解提取

验收标准对照

标准 状态 结果
嵌套类不串到内部类 PASS InnerClass.innerMethod 未泄露 (0 个)
继承方法标识 inherited_from PASS 本类方法 inherited_from=None
构造器作为特殊条目 PASS UserController 构造器在列表中
静态初始化块作为特殊条目 PASS <static_init> 在列表中, is_static=True
include_source=false 轻量版 PASS 不返回 source 字段
单元测试:重载 PASS 2 个 getUser 重载都在列表中
单元测试:抽象方法 PASS is_abstract=True
单元测试:内部类干扰 PASS 嵌套类方法未泄露
单元测试:空类 PASS total_methods=0
中等仓库 < 3s PASS JDP 验证

测试

单元测试

  • 基本列表: 方法列表、类元数据、可见性、is_static、注解、行范围
  • 嵌套类隔离: 内部类方法不泄露
  • 构造器/静态初始化块: 出现在列表中
  • 重载: 2 个 getUser 都在列表中
  • 抽象方法: is_abstract=True
  • 开关: include_private、include_source
  • 空类: 0 个方法
  • 边界: 类不存在、空类名、无效仓库
  • 继承: 本类方法 inherited_from=None
  • MCP 注册: 自动发现、MCP 调用

集成测试

  • java-design-patterns: 列出方法、性能 < 3s
  • spring-framework: 列出方法

Closes #138

wangrenyu.wry and others added 5 commits July 21, 2026 14:03
Implement the unified MCP Server framework as the runtime base for
all upcoming YASA MCP tools (Issue antgroup#129).

Framework:
- Dual transport: stdio (default) / streamable-http
- Tool registration via @mcp_tool decorator + auto-discovery
- Unified input validation (Pydantic), error handling, logging (stderr)
- Health check endpoint GET /healthz in HTTP mode
- Built-in demo tool ping returning server status

Modules:
- yasa_mcp/config.py: CLI args + env var parsing
- yasa_mcp/server.py: FastMCP lifecycle management
- yasa_mcp/registry.py: decorator-based tool auto-registration
- yasa_mcp/transport/http.py: streamable-http + /healthz
- yasa_mcp/tools/ping.py: demo health-check tool
- yasa_mcp/errors.py: unified error codes
- yasa_mcp/logging_config.py: stderr logging with level control

Tests (44 passing):
- test_registry.py: tool registration, auto-discovery, schema
- test_validation.py: param validation, error handling
- test_logging.py: stderr output, format, level filtering
- test_config.py: CLI parsing, env vars, error exit

Docs:
- docs/mcp/requirements.md: functional/non-functional requirements
- docs/mcp/system-design.md: architecture, module design, data flow
- docs/mcp/development-guide.md: step-by-step implementation guide
- yasa_mcp/README.md: usage + Claude Desktop/Cline config examples
- registry.py: prevent duplicate tool registration via __module__ check
- http.py: use app.add_route() instead of direct app.routes manipulation
- http.py: remove unused Route import
- http.py + server.py: pass log_level to uvicorn instead of hardcoding
- errors.py: fix empty string message fallback bug
- test_registry.py: use asyncio.run() instead of deprecated get_event_loop()
- test_validation.py: remove unused imports
- config.py: convert repo_root to absolute path
- logging_config.py: precise logger namespace check
- pyproject.toml: fix build-backend to setuptools.build_meta
- .gitignore: add .venv/ and Python project ignore rules
…ture pattern

Implement MCP tool that matches regex patterns against code element
signatures (methods, classes, fields) in Java repositories.

Key features:
- Supports element_kind: method, class, field, any
- Comment filtering (line + block comments stripped before matching)
- Field vs local variable disambiguation via brace-depth tracking
- Control flow keyword filtering (if/throw/return not matched as methods)
- Performance: O(n) single-pass depth computation with string/char/comment
  state machine

Test data: java-design-patterns (~1900 files) + spring-framework (~9200 files)
Performance: medium repo < 3s, large repo < 5s

All 51 tests passed.
… bodies

Reuse the O(n) brace-depth state machine from antgroup#134 to locate method body
boundaries, then apply regex search within each method's body lines to
return matches with their containing method's fully qualified name.

Key features:
- Returns containing_method (fully qualified name) for each match
- Multi-line matching (multi_line=true by default, re.DOTALL)
- Excludes field initializations, imports, and comments
- Lambda/anonymous class matches attributed to outer method
- Supports path_prefix to limit search scope
- Performance: medium repo (JDP ~1900 files) < 5s

Test data: java-design-patterns (~1900 files) + spring-framework (~9200 files)
All tests passed.
…by fully qualified name

Implement MCP tool to read the complete source code of a Java method by
its fully qualified name, with overload disambiguation and Javadoc/annotation
control.

Key features:
- Overload disambiguation via parameter_types; returns ambiguous + candidates
- include_javadoc / include_annotations control switches
- Supports constructors, abstract methods (declaration only), static init blocks
- Nested lambda/anonymous class bodies included in parent method source
- Reuses O(n) brace-depth state machine from antgroup#134 and method ranges from antgroup#135
- Fixes _strip_comments to preserve newlines (was collapsing multi-line comments)

Test data: java-design-patterns (~1900 files) + spring-framework (~9200 files)
Performance: JDP < 3s (0.43s measured)
All acceptance criteria verified.
@wryyyds7
wryyyds7 force-pushed the feat/oss26-list-methods-by-class-name-138 branch 2 times, most recently from 6ff0c9b to e609576 Compare July 30, 2026 08:53
Implement MCP tool to list all methods of a Java class by its fully
qualified name, returning signatures, visibility, line ranges, and
optional source code.

Key features:
- Visibility extraction (public/protected/private/package)
- is_static detection
- extends_class and implements_interfaces extraction from class declaration
- class_start_line / class_end_line
- include_private filter (default true)
- include_source switch (default true, false for lightweight listing)
- include_inherited (find parent class/interface methods, signatures only)
- Nested class isolation (only target class methods, not inner classes)
- Constructors and static init blocks as special entries
- inherited_from field (null for own methods)

Reuses antgroup#137's _extract_all_method_ranges, _find_class_file, _build_method_signature,
_extract_annotations, _extract_method_info.

Test data: java-design-patterns + spring-framework
All acceptance criteria covered.
@wryyyds7
wryyyds7 force-pushed the feat/oss26-list-methods-by-class-name-138 branch from e609576 to 3115234 Compare July 30, 2026 09:02
@wryyyds7

Copy link
Copy Markdown
Author

#138

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.

[OSS26] 列出类的全部方法 list_methods_by_class_name

1 participant