You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否提取接口方法引用的注解信息?以ControllerInterface::get为例

Is this achievable? Absolutely!

Your request to extract annotation info via a method reference like giveMeAnnotaions(ControllerInterface::get) is totally doable—how you pull it off just depends on the programming language you’re using. Since your example syntax looks identical to PHP’s method reference style, let’s use that as our primary context to break this down.

Step-by-step implementation in PHP

The core here is using reflection (PHP’s built-in tool for inspecting code metadata) to pull annotations from the target method. Here’s a concrete example:

1. First, define your annotation and interface

Let’s start with a common scenario: a route annotation for an API interface.

// Define a custom attribute (annotation)
#[Attribute(Attribute::TARGET_METHOD)]
class Route {
    public function __construct(
        public string $path,
        public string $httpMethod = 'GET'
    ) {}
}

// Your interface with the annotated method
interface ControllerInterface {
    #[Route(path: '/api/users', httpMethod: 'GET')]
    public function get();
}

2. Build the giveMeAnnotaions function

This function will take the method reference, parse it to get the class and method name, then use reflection to extract annotations.

use ReflectionMethod;
use InvalidArgumentException;

function giveMeAnnotaions(callable $methodReference) {
    // Parse the method reference to get class and method names
    if (is_array($methodReference)) {
        [$className, $methodName] = $methodReference;
    } elseif (is_string($methodReference) && str_contains($methodReference, '::')) {
        [$className, $methodName] = explode('::', $methodReference);
    } else {
        throw new InvalidArgumentException("Invalid method reference provided");
    }

    // Create a reflection object for the target method
    $reflectionMethod = new ReflectionMethod($className, $methodName);

    // Extract and instantiate all annotations (attributes)
    $annotations = [];
    foreach ($reflectionMethod->getAttributes() as $attribute) {
        $annotationInstance = $attribute->newInstance();
        $annotations[$attribute->getName()] = $annotationInstance;
    }

    return $annotations;
}

3. Test the call

Now you can call it exactly like you wanted:

$methodAnnotations = giveMeAnnotaions(ControllerInterface::get);
print_r($methodAnnotations);

This will return an array containing the Route annotation instance, with its path and httpMethod properties accessible.

What about other languages?

If you’re using a different language (like Java, C#, or Python), the core idea stays the same: use the language’s reflection or metaprogramming APIs to inspect the method’s metadata. For example:

  • In Java, you’d use Method.getAnnotations() after resolving the method from the reference.
  • In C#, MethodInfo.GetCustomAttributes() does the trick once you’ve retrieved the method info.
  • In Python, inspect.getmembers() or direct access to __annotations__ (for built-in annotation support) works.

The key requirement is that your language supports runtime access to method metadata—which most modern languages do.

内容的提问来源于stack exchange,提问作者Dilshod Tadjibaev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:27:14