- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Java内部有很多注解,如SuppressWarning
、FunctionalInterface
等,可以通过注解限制类的成员、扩展类甚至指定编译器选项,但是普通程序员如何编写这样的注释呢?
我搜索了注释主题,我所能找到的只是向注释添加一些元值,如 this ,以及如何使用注释,但我找不到任何解释如何实现高级注释的内容。任何指示都会有所帮助。
最佳答案
您正在寻找的是编译时注释。
基本上可以根据它的RetentionPolicy
来进行注解处理。根据 Java docs ,有 3 种类型的 RetentionPolicy
-
CLASS Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
RUNTIME Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.
SOURCE Annotations are to be discarded by the compiler.
编译时注释处理与 RetentionPolicy.SOURCE
相关,因为您想在编译时对源文件进行处理,类似于 @Override
等其他注释。
下面是一个简单的编译时注解处理器的例子——
创建 Maven 项目 - Annotation_Compile_Time -
(A) 在本项目中创建编译时Annotation MyAnnotation
-
package xxx;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target(ElementType.TYPE)
@Inherited
@Retention(RetentionPolicy.SOURCE)
public @interface MyAnnotation {
}
(B) 创建注解处理器MyAnnotationProcessor -
package xxx;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
@SupportedAnnotationTypes("xxx.MyAnnotation ")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class MyAnnotationProcessor extends AbstractProcessor {
public MyAnnotationProcessor () {
super();
}
@Override
public boolean process(Set<? extends TypeElement> typeElementSet, RoundEnvironment roundEnv) {
for (Element e : roundEnv.getRootElements()) {
String className = e.toString();
String message = "Annotated class - " + className;
System.out.println(message);
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message);
}
return false;
}
}
(C) 在目录中创建 javax.annotation.processing.Processor
文件 - src/main/resources/META-INF/services
内容如下-
xxx.MyAnnotationProcessor
(D) 使用构建配置更新 pom.xml
-
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
(E) 使用 mvn clean install
编译并安装此项目。
创建另一个 Maven 项目 - Annotation_User - 该项目将使用上述项目中定义的注解。在此项目中创建 2 个源文件并使用此注释进行注释
(A) AnnotationUser1 -
package xxx.consumer;
import xxx.MyAnnotation;
@MyAnnotation
public class AnnotationUser1 {
private String message;
public AnnotationUser1(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
(B) AnnotationUser2 -
package xxx.consumer;
import xxx.MyAnnotation;
@MyAnnotation
public class AnnotationUser2 {
private String message;
public AnnotationUser1(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
(C) 使用Annotation项目依赖更新pom.xml
-
<dependency>
<groupId>xxx</groupId>
<artifactId>Annotation_Compile_Time</artifactId>
<version>1.0</version>
</dependency>
现在,无论何时,您将使用mvn clean compile
编译此项目,您在项目 1 中定义的注释处理器将被调用。目前,处理器只是打印使用此注释注释的类的名称。
也可以引用this详情页面。
下一步是分析源文件并计算编号。的方法。因为,它是编译时处理,所以你不能使用 Reflection API
来获取 no。的方法。一种解决方案是使用 Eclipse AST用于解析源文件并计算编号。方法,或者您可以编写自己的逻辑。
Project lombok主要是基于Compile-time注解处理。想做点有用的,不如好好研究一下Project lombok source code
关于java - 如何像FunctionalInterface一样实现注解来限制成员函数的数量、函数类型等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48570033/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!