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

  1. <legend id='X1BI5'><style id='X1BI5'><dir id='X1BI5'><q id='X1BI5'></q></dir></style></legend>
        <bdo id='X1BI5'></bdo><ul id='X1BI5'></ul>

    1. <tfoot id='X1BI5'></tfoot>
    2. <small id='X1BI5'></small><noframes id='X1BI5'>

      像 @Override 这样的注解在 Java 内部是如何工作的

      时间:2023-07-26
      <legend id='WYSqk'><style id='WYSqk'><dir id='WYSqk'><q id='WYSqk'></q></dir></style></legend>

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

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

                本文介绍了像 @Override 这样的注解在 Java 内部是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                谁能向我解释一下注解在 java 内部是如何工作的?

                Can anybody explain to me how annotations work internally in java?

                我知道如何在 java 中使用 java.lang.annotation 库创建自定义注释.但我仍然不明白它在内部是如何工作的,例如 @Override 注释.

                I know how we can create custom annotations by using java.lang.annotation library in java. But I still don't get how it's working internally for example, the @Override annotation.

                如果有人能详细解释一下,我将非常感激.

                I will be really thankful if anyone could explain that in detail.

                推荐答案

                注解种类的第一个主要区别是它们是在编译时使用然后被丢弃(如@Override)还是放置在已编译的类文件中并在运行时可用(如 Spring 的 @Component).这由 @Retention 决定注释的策略.如果您正在编写自己的注解,则需要确定该注解是在运行时有用(可能用于自动配置)还是仅在编译时有用(用于检查或代码生成).

                The first main distinction between kinds of annotation is whether they're used at compile time and then discarded (like @Override) or placed in the compiled class file and available at runtime (like Spring's @Component). This is determined by the @Retention policy of the annotation. If you're writing your own annotation, you'd need to decide whether the annotation is helpful at runtime (for autoconfiguration, perhaps) or only at compile time (for checking or code generation).

                编译带有注解的代码时,编译器看到注解就像看到源元素上的其他修饰符一样,例如访问修饰符 (public/private) 或 最终.当它遇到一个注解时,它会运行一个注解处理器,它就像一个插件类,它表示它对特定的注解感兴趣.注释处理器通常使用反射 API 来检查正在编译的元素,并且可以简单地对它们运行检查、修改它们或生成要编译的新代码.@Override 是第一个例子;它使用反射 API 来确保它可以在其中一个超类中找到方法签名的匹配项,如果不能,则使用 Messager 导致编译错误.

                When compiling code with annotations, the compiler sees the annotation just like it sees other modifiers on source elements, like access modifiers (public/private) or final. When it encounters an annotation, it runs an annotation processor, which is like a plug-in class that says it's interested a specific annotation. The annotation processor generally uses the Reflection API to inspect the elements being compiled and may simply run checks on them, modify them, or generate new code to be compiled. @Override is an example of the first; it uses the Reflection API to make sure it can find a match for the method signature in one of the superclasses and uses the Messager to cause a compile error if it can't.

                有许多关于编写注释处理器的教程;这是一个有用的.查看 Processor<上的方法/code> 接口 用于编译器如何调用注解处理器;主要操作发生在 process 方法中,每次编译器看到具有匹配注解的元素时都会调用该方法.

                There are a number of tutorials available on writing annotation processors; here's a useful one. Look through the methods on the Processor interface for how the compiler invokes an annotation processor; the main operation takes place in the process method, which gets called every time the compiler sees an element that has a matching annotation.

                这篇关于像 @Override 这样的注解在 Java 内部是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <tfoot id='0TTAz'></tfoot>

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

                          <small id='0TTAz'></small><noframes id='0TTAz'>