<legend id='kQeVr'><style id='kQeVr'><dir id='kQeVr'><q id='kQeVr'></q></dir></style></legend>

    1. <i id='kQeVr'><tr id='kQeVr'><dt id='kQeVr'><q id='kQeVr'><span id='kQeVr'><b id='kQeVr'><form id='kQeVr'><ins id='kQeVr'></ins><ul id='kQeVr'></ul><sub id='kQeVr'></sub></form><legend id='kQeVr'></legend><bdo id='kQeVr'><pre id='kQeVr'><center id='kQeVr'></center></pre></bdo></b><th id='kQeVr'></th></span></q></dt></tr></i><div id='kQeVr'><tfoot id='kQeVr'></tfoot><dl id='kQeVr'><fieldset id='kQeVr'></fieldset></dl></div>
        <bdo id='kQeVr'></bdo><ul id='kQeVr'></ul>
    2. <small id='kQeVr'></small><noframes id='kQeVr'>

        <tfoot id='kQeVr'></tfoot>

        在 Annotation Processor for java 中发现方法调用的类

        时间:2023-09-28

        <tfoot id='oidOE'></tfoot>

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

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

                  本文介绍了在 Annotation Processor for java 中发现方法调用的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在为我们的构建系统编写一些工具,以对属于包含某些注释的类的方法强制执行一些严格的调用约定.

                  I am writing some tools for our build system to enforce some strict calling conventions on methods belonging to classes containing certain annotations.

                  我正在使用编译器树 API...

                  I'm using the Compiler Tree API...

                  我想知道的是,在遍历树"时,如何判断 MethodInvocation 的类/接口类型.

                  What i'm wondering is when traversing the 'tree', how can you tell the type of class/interface for a MethodInvocation.

                  我将 TreePathScanner 子类化为:

                  I'm subclassing TreePathScanner with :

                  @Override
                  public Object visitMethodInvocation(MethodInvocationTree node, Trees trees) {
                  
                  }
                  

                  我希望有一种方法可以告诉您尝试调用该方法的类(或接口)的类型.我会以错误的方式解决这个问题吗?感谢您的任何想法...

                  I'm hoping theres a way to tell the type of the class(or interface) that you're trying to invoke the method on. Am I going about this the wrong way? Thanks for any ideas...

                  推荐答案

                  这里有几个问题.你可以有兴趣知道方法调用接收器的 Java 类型,或者只知道知道调用方法上的类.Java信息更多信息丰富,因为它也为您提供了泛型类型,例如列表<字符串>而 Elements 只会为您提供类,例如列表.

                  There are a couple of issues here. You can either be interested in knowing the Java type of the method invocation receiver or just knowing the class on the method is invoked. Java information is more informative as it gives you generic types as well, e.g. List<String> while Elements would only provide you with the class, e.g. List<E>.

                  获取元素

                  要获取调用该方法的类的元素,您可以这样做以下:

                  To get the Element of the class the method is invoked on, you can do the following:

                  
                    MethodInvocationTree node = ...;
                    Element method =
                          TreeInfo.symbol((JCTree)node.getMethodSelect());
                    TypeElement invokedClass = (TypeElement)method.getEnclosingElement();
                  

                  角落案例:

                  1.invokedClass 可能是接收者类型的超类.所以跑步new ArrayList<String>.equals(null) 上的代码段将返回AbstractList 而不是 ArrayList,因为实现了 equals()在 AbstractList 而不是 ArrayList.

                  1. invokedClass might be a superclass of the receiver type. So running the snippet on new ArrayList<String>.equals(null) would return AbstractList rather than ArrayList, since equals() is implemented in AbstractList not ArrayList.

                  2.在处理数组调用时,例如new int[].clone(),你会获取类 ArrayTypeElement.

                  2. When handling array invocations, e.g. new int[].clone(), you would get TypeElement of class Array.

                  获取实际类型

                  要获取类型,没有直接的方法可以确定它是什么接收器类型是.处理方法调用有一些复杂性在没有明确给出接收者的内部类中(例如,与 OuterClass.this.toString() 不同).这是一个示例实现:

                  To get the type, there is no direct way for determining it what the receiver type is. There is some complexity in handling method invocations within inner classes where the receiver is not given explicitly (e.g. unlike OuterClass.this.toString()). Here is a sample implementation:

                  
                    MethodInvocationTree node = ...;
                    TypeMirror receiver;
                    if (methodSel.getKind() == Tree.Kind.MEMBER_SELECT) {
                      ExpressionTree receiver = ((MemberSelectTree)methodSel).getExpression();
                      receiverType = ((JCTree)receiver).type;
                    } else if (methodSel.getKind() == Tree.Kind.IDENTIFIER) {
                      // need to resolve implicit this, which is described in
                      //  JLS3 15.12.1 and 15.9.2
                  
                      // A bit too much work that I don't want to work on now
                      // Look at source code of
                      //   Attr.visitApply(JCMethodInvocation)
                      //   resolveImplicitThis(DiagnosticPosition, Env, Type)
                    } else
                      throw new AssertionError("Unexpected type: " + methodSel.getKind());
                  

                  注意:

                  receiver 类型需要是 TypeMirror 而不是 DeclaredType很遗憾.当调用 new int[5].clone() 时,receiver 会是int[]ArrayType,比前面的信息量大方法.

                  The receiver type needs to be TypeMirror not DeclaredType unfortunately. When calling new int[5].clone(), receiver would be an ArrayType of int[], which is more informative than the previous method.

                  让它运行

                  前面两种方法都需要编译器解析类型类的信息.通常情况下,编译器只解析方法声明的类型,而不解析主体.因此,前面描述的方法将返回 null.

                  Both of the previous methods require the compiler to resolve the type information for the classes. In usual circumstances, the compiler only resolve the types for method declarations but not the bodies. Hence, the methods described earlier would return null instead.

                  要让编译器解析类型信息,您可以执行以下操作之一有以下几种方式:

                  To have the compiler resolve the type information, you can do one of the following ways:

                  1.使用刚刚添加到的 AbstractTypeProcessor 类JDK 7 的编译器存储库.查看 JSR 上的工作308 及其编译器.虽然这项工作主要针对带注释的类型,但它可能有用.编译器允许您向后使用提供的类与 Java 5 兼容.

                  1. Use AbstractTypeProcessor class that just got added to the compiler repository for JDK 7. Check out the work on JSR 308 and their compiler. While the work is mainly on annotated types, it might be useful for. The compiler allows you to use the provided class in a backward compatible manner with Java 5.

                  这种方法允许您编写只被调用的处理器就像你现在的处理器一样.

                  This approach allows you to write processors that get invoked just like your current processors.

                  2.请改用 JavacTask 并调用 JavacTask.analyze().看着this javac test 看看如何在类上调用您的访问者.

                  2. Use JavacTask instead and call JavacTask.analyze(). Look at the main method of this javac test to see how to invoke your visitor on the classes.

                  这种方法使您的处理器看起来更像一个分析工具而不是编译器的插件,因为您需要调用它直接而不是让它成为一个常规过程.

                  This approach makes your processor look more like an analysis tool rather than a plug-in to the compiler, as you would need to invoke it directly rather than have it be a regular process.

                  这篇关于在 Annotation Processor for java 中发现方法调用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:有没有办法在 Java 中使用注释来替换访问器? 下一篇:注释 SOURCE 保留政策

                  相关文章

                  最新文章

                1. <small id='pntu7'></small><noframes id='pntu7'>

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

                      <bdo id='pntu7'></bdo><ul id='pntu7'></ul>
                    <legend id='pntu7'><style id='pntu7'><dir id='pntu7'><q id='pntu7'></q></dir></style></legend><tfoot id='pntu7'></tfoot>