Java注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:15:41  来源:igfitidea点击:

Java Annotations

javaannotationsglossary

提问by Lehane

What is the purpose of annotations in Java? I have this fuzzy idea of them as somewhere in between a comment and actual code. Do they affect the program at run time?

Java中注解的目的是什么?我有一个模糊的想法,它们介于注释和实际代码之间。它们会在运行时影响程序吗?

What are their typical usages?

它们的典型用途是什么?

Are they unique to Java? Is there a C++ equivalent?

它们是 Java 独有的吗?有 C++ 等价物吗?

采纳答案by Anders Sandvig

Annotations are primarily used by code that is inspecting other code. They are often used for modifying (i.e. decorating or wrapping) existing classes at run-time to change their behavior. Frameworks such as JUnitand Hibernateuse annotations to minimize the amount of code you need to write yourself to use the frameworks.

注释主要由正在检查其他代码的代码使用。它们通常用于在运行时修改(即装饰或包装)现有类以改变它们的行为。诸如JUnitHibernate 之类的框架使用注释来最大限度地减少使用这些框架所需的代码量。

Oracle has a good explanation of the concept and its meaning in Javaon their site.

Oracle在其网站上对Java 中的概念及其含义进行了很好的解释

回答by Konrad Rudolph

Also, are they unique to Java, is there a C++ equivalent?

另外,它们是 Java 独有的,是否有 C++ 等价物?

No, but VB and C# have attributeswhich are the same thing.

不,但 VB 和 C# 具有相同的属性

Their use is quite diverse. One typical Java example, @Overridehas no effect on the code but it can be used by the compiler to generate a warning (or error) if the decorated method doesn't actually override another method. Similarly, methods can be marked obsolete.

它们的用途非常多样化。一个典型的 Java 示例@Override对代码没有影响,但如果装饰方法实际上没有覆盖另一个方法,则编译器可以使用它来生成警告(或错误)。同样,方法可以被标记为过时。

Then there's reflection. When you reflect a type of a class in your code, you can access the attributes and act according to the information found there. I don't know any examples in Java but in .NET this is used by the compiler to generate (de)serializationinformation for classes, determine the memory layoutof structures and declare function importsfrom legacy libraries (among others). They also control how the IDE form designer works.

然后是反思。当您在代码中反映类的类型时,您可以访问属性并根据在那里找到的信息进行操作。我不知道 Java 中的任何示例,但在 .NET 中,编译器使用它来为类生成(反)序列化信息,确定结构的内存布局声明从旧库(以及其他)导入的函数。它们还控制 IDE 表单设计器的工作方式。

/EDIT: Attributes on classes are comparable to tag interfaces (like Serializablein Java). However, the .NET coding guidelines say not to use tag interfaces. Also, they only work on class level, not on method level.

/编辑:类上的属性类似于标记接口(如Java 中的Serializable)。但是,.NET 编码指南说不要使用标签接口。此外,它们仅适用于类级别,而不适用于方法级别。

回答by Eli Courtwright

Anders gives a good summary, and here's an example of a JUnit annotation

Anders 给出了很好的总结,这里有一个 JUnit 注释的例子

@Test(expected=IOException.class)
public void flatfileMissing() throws IOException {
    readFlatFile("testfiles"+separator+"flatfile_doesnotexist.dat");
}

Here the @Testannotation is telling JUnit that the flatfileMissingmethod is a test that should be executed and that the expected result is a thrown IOException. Thus, when you run your tests, this method will be called and the test will pass or fail based on whether an IOExceptionis thrown.

这里的@Test注解告诉 JUnit 该flatfileMissing方法是一个应该执行的测试,并且预期的结果是一个 throw IOException。因此,当您运行测试时,将调用此方法,并且测试将根据是否IOException抛出an来通过或失败。

回答by pek

Java also has the Annotation Processing Tool (apt) where not only you create annotations, but decide also how do these annotations work on the source code.

Java 也有注释处理工具 (apt),您不仅可以在其中创建注释,还可以决定这些注释如何在源代码上工作。

Hereis an introduction.

是一个介绍。

回答by Scott Stanchfield

To see some cool stuff you can do with Annotations, check out my JavaBean annotations and annotation processor.

要查看可以使用 Annotations 做的一些很酷的事情,请查看我的 JavaBean annotations 和 annotation processor

They're great for generating code, adding extra validations during your build, and I've also been using them for an error message framework (not yet published -- need to clear with the bosses...).

它们非常适合生成代码,在构建过程中添加额外的验证,而且我也一直将它们用于错误消息框架(尚未发布 - 需要与老板清除......)。

回答by Krishan

By literal definition an annotation adds notes to an element. Likewise, Java annotations are tags that we insert into source code for providing more information about the code. Java annotations associate information with the annotated program element. Beside Java annotations Java programs have copious amounts of informal documentation that typically is contained within comments in the source code file. But, Java annotations are different from comments they annotate the program elements directly using annotation types to describe the form of the annotations. Java Annotations present the information in a standard and structured way so that it could be used amenably by processing tools.

根据字面定义,注释为元素添加注释。同样,Java 注释是我们插入到源代码中的标签,用于提供有关代码的更多信息。Java 注释将信息与带注释的程序元素相关联。除了 Java 注释之外,Java 程序还有大量的非正式文档,这些文档通常包含在源代码文件的注释中。但是,Java 批注不同于注释,它们直接使用批注类型对程序元素进行批注,以描述批注的形式。Java 注释以标准和结构化的方式呈现信息,以便处理工具可以适当地使用它。

To read in detail, there is a nice tutorial on Java Annotations

要详细阅读,Java Annotations有一个很好的教程

回答by Koushik Balaji Venkatesan

When do you use Java's @Override annotation and why?The link refers to a question on when one should use the override annotation(@override).. This might help understand the concept of annotation better.Check out.

你什么时候使用 Java 的 @Override 注释,为什么?该链接提到了一个关于何时应该使用覆盖注释(@override)的问题。这可能有助于更好地理解注释的概念。请查看。

回答by Harvester

Annotations when it comes to EJB is known as choosing Implicit middle-ware approach over an explicit middle-ware approach , when you use annotation you're customizing what you exactly need from the API for example you need to call transaction method for a bank transfer : without using annotation : the code will be

谈到 EJB 时,注释被称为选择隐式中间件方法而不是显式中间件方法,当您使用注释时,您正在自定义 API 中您真正需要的内容,例如您需要调用银行转账的交易方法:不使用注释:代码将是

transfer(Account account1, Account account2, long amount)    
{
   // 1: Call middleware API to perform a security check
   // 2: Call middleware API to start a transaction
   // 3: Call middleware API to load rows from the database
   // 4: Subtract the balance from one account, add to the other
   // 5: Call middleware API to store rows in the database
   // 6: Call middleware API to end the transaction
}

while using Annotation your code contains no cumbersome API calls to use the middle- ware services. The code is clean and focused on business logic

在使用 Annotation 时,您的代码不包含使用中间件服务的繁琐 API 调用。代码干净,专注于业务逻辑

transfer(Account account1, Account account2, long amount) 
{
   // 1: Subtract the balance from one account, add to the other
}