营销型网站的建设重点是什么意思,手机网站知识,百度新闻客户端,wordpress被挂马在上一篇有关Java注释的文章中#xff0c;我概述了一个最近的用例#xff0c;并为您提供了一些自定义注释的示例以及如何使用它们。 在本文中#xff0c;我将更进一步#xff0c;并为您提供一些自定义注释的示例#xff0c;以及如何使用Java Reflection API处理这些自定义… 在上一篇有关Java注释的文章中我概述了一个最近的用例并为您提供了一些自定义注释的示例以及如何使用它们。 在本文中我将更进一步并为您提供一些自定义注释的示例以及如何使用Java Reflection API处理这些自定义注释。 学习完本教程后您应该对自定义注释可以提供的简单性和灵活性有了更好的了解。 因此让我们深入研究代码 自定义注释清单 我今天为示例代码创建了三个不同的注释分别是DoItLikeThis DoItLikeThat和DoItWithAWhiffleBallBat注释。 每个注释针对的是不同的元素类型并且具有稍微不同的属性因此我可以向您展示如何查找和相应地处理它们。 喜欢这个注释 DoItLikeThis注释针对ElementType TYPE这使其仅可用于Java类型。 该批注具有三个可选元素descriptionaction和一个布尔字段shouldDoItLikeThis。 如果在使用此批注时不为这些元素提供任何值则它们将默认为指定的值。 package com.keyhole.jonny.blog.annotations;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** Annotation created for doing it like this.*/
Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
public interface DoItLikeThis {/*** return - The description.*/String description() default ;/*** return - The action.*/String action() default ;/*** return - Should we be doing it like this.*/boolean shouldDoItLikeThis() default false;}像注释一样 DoItLikeThat注释是仅针对Java字段的注释。 此批注还具有一个类似的布尔元素名称为ShouldDoItLikeThat它没有指定默认值因此在使用批注时是必选元素。 批注还包含一个定义为String数组的元素该元素将包含应检查的用户角色列表。 package com.keyhole.jonny.blog.annotations;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** Annotation created for doing it like that* instead of like this.*/
Target(ElementType.FIELD)
Retention(RetentionPolicy.RUNTIME)
public interface DoItLikeThat {/*** return - Should we be doing it like that.*/boolean shouldDoItLikeThat();/*** return - List of user roles that can do it like that.*/String[] roles() default{};}DoWWithAWhiffleBallBat批注 DoItWithAWhiffleBallBat注释旨在仅与方法一起使用并且与其他注释类似。 它也有一个类似的布尔元素这个名字叫做shouldDoItWithAWhiffleBallBat。 还定义了另一个元素该元素使用WhiffleBallBat枚举定义了可用的不同类型的Whiffle球棒默认为经典的黄色经典Whiffle球棒。 package com.keyhole.jonny.blog.annotations;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** When you cant do it like this or do it like that,* do it with a whiffle ball bat.*/
Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
public interface DoItWithAWhiffleBallBat {/*** return - Should we be doing it with a whiffle ball bat.*/boolean shouldDoItWithAWhiffleBallBat() default false;/*** return - Sweet, which type of whiffle ball bat?*/WhiffleBallBat batType() default WhiffleBallBat.YELLOW_PLASTIC;}带注释的类 现在我们已经为示例定义了注释我们需要几个类进行注释。 每个类都提供了使用指定元素以及依赖默认值的注释的示例用法。 还包括其他未注释的字段和方法因此注释处理器不应对其进行处理。 这是两个示例类的源代码 带注释的一类 package com.keyhole.jonny.blog.annotations;import java.util.Date;DoItLikeThis
public class AnnotatedOne implements AnnotatedClass {DoItLikeThat(shouldDoItLikeThat false)private String field1;DoItLikeThat(shouldDoItLikeThat true, roles { admin, root })private String field2;private String field3;private Date dateDoneLikeThis;/* setters and getters removed for brevity */DoItWithAWhiffleBallBat(batType WhiffleBallBat.BLACK_PLASTIC, shouldDoItWithAWhiffleBallBat true)public void doWhateverItIs() {// method implementation}public void verifyIt() {// method implementation}}带注释的二级 package com.keyhole.jonny.blog.annotations;import java.util.Date;DoItLikeThis(action PROCESS, shouldDoItLikeThis true, description Class used for annotation example.)
public class AnnotatedTwo implements AnnotatedClass {DoItLikeThat(shouldDoItLikeThat true)private String field1;DoItLikeThat(shouldDoItLikeThat true, roles { web, client })private String field2;private String field3;private Date dateDoneLikeThis;/* setters and getters removed for brevity */DoItWithAWhiffleBallBat(shouldDoItWithAWhiffleBallBat true)public void doWhateverItIs() {// method implementation}public void verifyIt() {// method implementation}}处理注释 使用反射来处理注释实际上非常简单。 对于您可以为其创建和应用注释的每种元素类型这些元素上都有一些使用注释的方法。 您需要做的第一件事是检查元素以确定是否有任何注释或检查该元素是否存在特定注释。 每个元素类型ClassField和Method都实现了AnnotatedElement接口该接口定义了以下方法 getAnnotations –返回此元素上存在的所有注释包括所有继承的注释。 getDeclaredAnnotations –仅返回直接存在于此元素上的注释。 getAnnotationClass A注记类 –返回指定注解类型的元素注解如果找不到则返回null。 isAnnotation –如果要检查的元素是注释则返回true。 isAnnotationPresentClass Extends Annotation注解类 –如果所检查的元素上存在指定的注解则返回true。 在处理批注时我们要做的第一件事是检查批注是否存在。 为此我们将对批注处理进行以下检查 if (ac.getClass().isAnnotationPresent(DoItLikeThis.class)) {// process the annotation, ac being the instance of the object we are inspecting} 找到所需的批注后我们将获取该批注并为该批注进行任何处理。 至此我们将可以访问注释的元素及其值。 请注意没有任何用于访问注释元素的获取器或设置器。 DoItLikeThis anno ac.getClass().getAnnotation(DoItLikeThis.class);System.out.println(Action: anno.action());System.out.println(Description: anno.description());System.out.println(DoItLikeThis: anno.shouldDoItLikeThis()); 对于字段和方法检查当前注释会略有不同。 对于这些类型的元素我们需要遍历所有字段或方法以确定元素上是否存在注释。 您将需要从Class中获取所有字段或方法遍历Field或Method数组然后确定元素上是否存在注释。 看起来应该像这样 Field[] fields ac.getClass().getDeclaredFields();for (Field field : fields) {if (field.isAnnotationPresent(DoItLikeThat.class)) {DoItLikeThat fAnno field.getAnnotation(DoItLikeThat.class);System.out.println(Field: field.getName());System.out.println(DoItLikeThat: fAnno.shouldDoItLikeThat());for (String role : fAnno.roles()) {System.out.println(Role: role);}}}结论 如您所见创建自己的注释并进行处理非常简单。 在我提供的示例中我们只是将元素的值输出到控制台或日志。 希望您能看到这些的潜在用途并且将来可能会真正考虑创建自己的。 我在注释中看到的一些最佳用法是它们替换一些配置代码或经常使用的通用代码例如验证字段的值或将业务对象映射到Web表单。 最后这是完整的源代码以及一个简单的Java主类来执行代码 带注释的类处理器 package com.keyhole.jonny.blog.annotations;import java.lang.reflect.Field;
import java.lang.reflect.Method;public class AnnotatedClassProcessor {public void processClass(AnnotatedClass ac) {System.out.println(------Class Processing Begin---------);System.out.println(Class: ac.getClass().getName());if (ac.getClass().isAnnotationPresent(DoItLikeThis.class)) {// process the annotation, ac being the instance of the object we are inspectingDoItLikeThis anno ac.getClass().getAnnotation(DoItLikeThis.class);System.out.println(Action: anno.action());System.out.println(Description: anno.description());System.out.println(DoItLikeThis: anno.shouldDoItLikeThis());System.out.println(------Field Processing---------);Field[] fields ac.getClass().getDeclaredFields();for (Field field : fields) {if (field.isAnnotationPresent(DoItLikeThat.class)) {DoItLikeThat fAnno field.getAnnotation(DoItLikeThat.class);System.out.println(Field: field.getName());System.out.println(DoItLikeThat: fAnno.shouldDoItLikeThat());for (String role : fAnno.roles()) {System.out.println(Role: role);}}}System.out.println(------Method Processing---------);Method[] methods ac.getClass().getMethods();for (Method method : methods) {if ( method.isAnnotationPresent(DoItWithAWhiffleBallBat.class)) {DoItWithAWhiffleBallBat mAnno method.getAnnotation(DoItWithAWhiffleBallBat.class);System.out.println(Use WhiffleBallBat? mAnno.shouldDoItWithAWhiffleBallBat());System.out.println(Which WhiffleBallBat? mAnno.batType());}}}System.out.println(------Class Processing End---------);}
}运行处理器 package com.keyhole.jonny.blog.annotations;public class RunProcessor {/*** param args*/public static void main(String[] args) {AnnotatedClassProcessor processor new AnnotatedClassProcessor();processor.processClass(new AnnotatedOne());processor.processClass(new AnnotatedTwo());}}翻译自: https://www.javacodegeeks.com/2014/09/processing-java-annotations-using-reflection.html