<bdo id='teg9G'></bdo><ul id='teg9G'></ul>

  1. <tfoot id='teg9G'></tfoot>

    <small id='teg9G'></small><noframes id='teg9G'>

  2. <i id='teg9G'><tr id='teg9G'><dt id='teg9G'><q id='teg9G'><span id='teg9G'><b id='teg9G'><form id='teg9G'><ins id='teg9G'></ins><ul id='teg9G'></ul><sub id='teg9G'></sub></form><legend id='teg9G'></legend><bdo id='teg9G'><pre id='teg9G'><center id='teg9G'></center></pre></bdo></b><th id='teg9G'></th></span></q></dt></tr></i><div id='teg9G'><tfoot id='teg9G'></tfoot><dl id='teg9G'><fieldset id='teg9G'></fieldset></dl></div>
  3. <legend id='teg9G'><style id='teg9G'><dir id='teg9G'><q id='teg9G'></q></dir></style></legend>

    1. 在 Java 6 注解处理器中查找方法返回类型的类型参

      时间:2023-09-28
    2. <tfoot id='9lndz'></tfoot>

      <i id='9lndz'><tr id='9lndz'><dt id='9lndz'><q id='9lndz'><span id='9lndz'><b id='9lndz'><form id='9lndz'><ins id='9lndz'></ins><ul id='9lndz'></ul><sub id='9lndz'></sub></form><legend id='9lndz'></legend><bdo id='9lndz'><pre id='9lndz'><center id='9lndz'></center></pre></bdo></b><th id='9lndz'></th></span></q></dt></tr></i><div id='9lndz'><tfoot id='9lndz'></tfoot><dl id='9lndz'><fieldset id='9lndz'></fieldset></dl></div>

    3. <legend id='9lndz'><style id='9lndz'><dir id='9lndz'><q id='9lndz'></q></dir></style></legend>
        • <bdo id='9lndz'></bdo><ul id='9lndz'></ul>

            <tbody id='9lndz'></tbody>
        • <small id='9lndz'></small><noframes id='9lndz'>

                本文介绍了在 Java 6 注解处理器中查找方法返回类型的类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在编写一个工具,它使用注释处理器根据带注释的类的方法的返回类型生成源代码.返回类型始终是定义类型变量 T 的接口 A 的某个子类型(接口或类).

                I'm writing a tool that uses the annotation processor to generate source code depending on the return type of methods of an annotated class. The return type is always some subtype (interface or class) of an interface A that defines a type variable T.

                interface A<T>{T m();};
                

                我想找方法m()返回值类型变量T的类型参数.

                I would like to find the type parameter for the method m() return value type variable T.

                返回类型由注释处理器表示为 javax.lang.model.type.TypeMirror 实例.最简单的情况是直接返回A<T>.

                The return type is represented by the annotation processor as a javax.lang.model.type.TypeMirror instance. The simplest case is to return A<T> directly.

                @SomeAnnotation
                class SomeClass{
                    A<T> x();
                }
                

                找出T的处理器代码非常简单.(为了保持代码简单,我将在这里转换而不是使用访问者 API.)

                The processor code to find out T is quite simple. (I'll cast instead of using the visitor API here to keep the code simple.)

                DeclaredType type = (DeclaredType) typeMirror;
                TypeMirror t = type.getTypeArguments().get(0);
                

                返回类型的TypeMirror是一个javax.lang.model.type.DeclaredTypeT是第一个类型参数.结果 tTjavax.lang.model.type.TypeVariable.这同样适用于具体的返回类型 A<B>(B 是某种类型:interface B{}).t 的结果是一个代表 BDeclaredType.

                The TypeMirror of the return type is a javax.lang.model.type.DeclaredType and T is the first type argument. The result t is a javax.lang.model.type.TypeVariable for T. The same works for a concrete return type A<B> (B is some type: interface B{}). The result for t is a DeclaredType representing B.

                其他结果类型开始变得复杂:

                Things start to get complicated with other result types:

                interface Subtype<T> extends A<T>{}
                interface Concrete extends A<B>{};
                interface Multiple<B,T> extends A<T>{}
                interface Bounds<T extends B> extends A<T>{}
                interface Hierarchy extends Concrete{}
                

                Subtype<B>          -> DeclaredType B
                Subtype<T>          -> TypeVariable T
                Concrete            -> DeclaredType B
                Multiple<B,T>       -> TypeVariable T or DeclaredType B depeding on Multiple
                Multiple<B,B>       -> TypeVariable B
                <T extends B> A<T>  -> TypeVariable T with super class bound B
                Bound<B>            -> DeclaredType B
                Bound<C>            -> DeclaredType C (subtype of B)
                Hierarchy           -> TypeVariable T
                

                有没有办法在不镜像整个 java 类型系统的情况下为 T 找到正确的类型参数?

                Is there a way to find the correct type parameter for T without mirroring the whole java type system?

                推荐答案

                看看http://docs.oracle.com/javase/6/docs/api/javax/lang/model/util/Types.html#asMemberOf%28javax.lang.model.type.DeclaredType,%20javax.lang.model.element.Element%29

                我用它来解决这个问题,并在这个拉取请求中为 WsDoc 项目贡献了解决方案:https://github.com/versly/wsdoc/pull/7

                I used it to solve this problem and contributed the solution to the WsDoc project in this pull request: https://github.com/versly/wsdoc/pull/7

                我做了这样的事情:

                      Type.MethodType methodType = (Type.MethodType) processingEnv.getTypeUtils().asMemberOf(declaredTypeThatExtendsSomeGenericParent, methodToGetReturnTypeForAsExecutableElement);
                      TypeMirror type = methodType.getReturnType();
                

                这篇关于在 Java 6 注解处理器中查找方法返回类型的类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Javadoc 使用 @value 在内部类常量上显示值 下一篇:需要一个 Hibernate 中的主键 @OneToOne 映射示例

                相关文章

                最新文章

                  1. <tfoot id='gE2Vw'></tfoot>
                    • <bdo id='gE2Vw'></bdo><ul id='gE2Vw'></ul>
                    <legend id='gE2Vw'><style id='gE2Vw'><dir id='gE2Vw'><q id='gE2Vw'></q></dir></style></legend>
                    <i id='gE2Vw'><tr id='gE2Vw'><dt id='gE2Vw'><q id='gE2Vw'><span id='gE2Vw'><b id='gE2Vw'><form id='gE2Vw'><ins id='gE2Vw'></ins><ul id='gE2Vw'></ul><sub id='gE2Vw'></sub></form><legend id='gE2Vw'></legend><bdo id='gE2Vw'><pre id='gE2Vw'><center id='gE2Vw'></center></pre></bdo></b><th id='gE2Vw'></th></span></q></dt></tr></i><div id='gE2Vw'><tfoot id='gE2Vw'></tfoot><dl id='gE2Vw'><fieldset id='gE2Vw'></fieldset></dl></div>

                  2. <small id='gE2Vw'></small><noframes id='gE2Vw'>