如何实现类似@Override Java注释的东西吗? [英] How to implement something similar to the @Override java annotation?
问题描述
随着此JDK code ../爪哇/郎/ Override.java
,
With this jdk code in ../java/lang/Override.java
,
package java.lang;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
刚走标注声明中,java编译器有足够的智能来检测错误(编译时间):
having just annotation declaration, java compiler is intelligent enough to detect error(compile time):
的方法toString123()类型的示例必须覆盖或实现超方式
在下面的问题code。
in the below problem code.
package annotationtype;
public class Example {
@Override public String toString() {
return "Override the toString() of the superclass";
}
@Override public String toString123() {
return "Override the toString123() of the superclass";
}
public static void main(String[] args) {
}
}
有关注释声明覆盖
只是被编译,
Annotation declaration for Override
just gets compiled to,
interface java.lang.Override extends java.lang.annotation.Annotation{
}
这只不过是一个 界面更加
。
因此,
如何接口java.lang.Override
语法帮助Java编译器在编译时发现上面的错误?
How does interface java.lang.Override
syntax help java compiler to detect above error at compile time?
推荐答案
这会触发编译错误不在于标注的实施,它位于Java编译器。
The implementation that triggers the compile error doesn't lie in the annotation, it lies in the Java compiler.
如果你想要写自己的类似注解处理器,可以使用注解处理器API:<一href=\"http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Processor.html\">http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Processor.html
If you want to write your own similar annotation processor, you would use the annotation processor API: http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Processor.html
这篇关于如何实现类似@Override Java注释的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!