当前位置: 首页 > news >正文

青岛网站建设有哪些公司/外链管理

青岛网站建设有哪些公司,外链管理,东台企业网站建设,通州网站建设电话场景: 通常情况下,我们已经明确了某个类,想要判断这个类拥有那个注解,直接使用clazz.getAnnotation()即可,但是某些场景下,我们可能需要进行反向操作,获取存在某个注解的类、方法、字段&#xf…

场景:
通常情况下,我们已经明确了某个类,想要判断这个类拥有那个注解,直接使用clazz.getAnnotation()即可,但是某些场景下,我们可能需要进行反向操作,获取存在某个注解的类、方法、字段;

获取类注解(伪代码):

			Class<?> clazz = Class.forName(classname);// 1、判断类中是否存在ClassAnnotation注解,如果存在则打印ClassAnnotation classAnnotation = clazz.getAnnotation(ClassAnnotation.class);if (classAnnotation != null) {// 获取属性System.out.println("类中注解——code:" + classAnnotation.code() + ";name:" + classAnnotation.name());}

获取方法注解(伪代码):

			Method[] methods = clazz.getMethods();for (Method method : methods) {MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class);if (methodAnnotation != null) {// 获取属性System.out.println("方法中注解——code:" + methodAnnotation.code() + ";name:" + methodAnnotation.name());}}

获取字段注解(伪代码):

			Field[] fields = clazz.getFields();for (Field field : fields) {FiledAnnotation filedAnnotation = field.getAnnotation(FiledAnnotation.class);if (filedAnnotation != null) {// 获取属性System.out.println("字段中注解——code:" + filedAnnotation.code() + ";name:" + filedAnnotation.name());}}

定义注解:

// 1、ClassAnnotation
import java.lang.annotation.*;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ClassAnnotation {/*** 定义注解需要的属性** @return*/String code();String name();
}// 2、MethodAnnotation
import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodAnnotation {/*** 定义注解需要的属性** @return*/String code();String name();
}// 3、FiledAnnotation
import java.lang.annotation.*;@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FiledAnnotation {/*** 定义注解需要的属性** @return*/String code();String name();
}

完整代码:

public class Main {public static void main(String[] args) throws Exception {//spring工具类,可以获取指定路径下的全部类ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();// 监听的包String basePackage = "com.lhz.wx.test";String resourcePattern = "/**/*.class";String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +ClassUtils.convertClassNameToResourcePath(basePackage) + resourcePattern;//  获取指定包路径下的类资源Resource[] resources = resourcePatternResolver.getResources(pattern);//MetadataReader 的工厂类MetadataReaderFactory readerfactory = new CachingMetadataReaderFactory(resourcePatternResolver);for (Resource resource : resources) {//用于读取类信息MetadataReader reader = readerfactory.getMetadataReader(resource);//扫描到的classString classname = reader.getClassMetadata().getClassName();Class<?> clazz = Class.forName(classname);// 1、判断类中是否存在ClassAnnotation注解,如果存在则打印ClassAnnotation classAnnotation = clazz.getAnnotation(ClassAnnotation.class);if (classAnnotation != null) {// 获取属性System.out.println("类中注解——code:" + classAnnotation.code() + ";name:" + classAnnotation.name());}// 2、判断类中方法否存在MethodAnnotation注解,如果存在则打印Method[] methods = clazz.getMethods();for (Method method : methods) {MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class);if (methodAnnotation != null) {// 获取属性System.out.println("方法中注解——code:" + methodAnnotation.code() + ";name:" + methodAnnotation.name());}}// 3、判断类中字段否存在FiledAnnotation注解,如果存在则打印Field[] fields = clazz.getFields();for (Field field : fields) {FiledAnnotation filedAnnotation = field.getAnnotation(FiledAnnotation.class);if (filedAnnotation != null) {// 获取属性System.out.println("字段中注解——code:" + filedAnnotation.code() + ";name:" + filedAnnotation.name());}}}}
}

效果:
在这里插入图片描述

http://www.lbrq.cn/news/1081153.html

相关文章:

  • 网站建设与维护 电子版/电脑培训班多少费用
  • 公司网站建设需要提供什么材料/每天看七个广告赚40元的app
  • 自己做的网站怎么设置地址/全国十大婚恋网站排名
  • 做网站推广对电脑有什么要求/超级推荐的关键词怎么优化
  • 古交网站建设/新品上市怎么推广词
  • 白银市网站建设/如何做品牌推广方案
  • 自做视频网站赚钱吗/完美日记网络营销策划书
  • 东莞做网站要多少钱/朋友圈推广
  • 做婚恋交友网站模板/线上推广有哪些渠道
  • 网站建设售后服务合同/国内最新的新闻
  • 56m做图片视频的网站是什么/热点新闻事件素材
  • 东莞长安/武汉网站seo
  • 网站建设新报价图片欣赏/品牌定位
  • 网站全屏轮播怎么做/网络营销策划书论文
  • 网站兼容怎么调/千网推软文推广平台
  • 企业腾讯邮箱/什么是seo如何进行seo
  • 16岁做分期网站/网络营销课程有哪些
  • 淘宝导购网站备案/注册域名费用一般多少钱
  • 只做女性的网站/长沙seo外包优化
  • 海口网站制作企业/拉新推广渠道
  • 电脑系统做的好的几个网站/天津债务优化公司
  • 企业网站建设如何去规划/seo 页面
  • 上海莱布拉网站建设/百度游戏app下载
  • 网站显示速度的代码/seo网站平台
  • 做的好的网站开发/北京建公司网站价格
  • 哈尔滨线下教学最新情况/通州优化公司
  • 深圳做门户网站/网络营销推广
  • 专业旅游网站开发系统/电商推广和网络推广的区别
  • 新手建什么网站赚钱吗/百度seo在线优化
  • 重庆建网站哪家售后服务比较好/网站seo的内容是什么
  • SpringMVC的高级特性
  • C#线程同步(三)线程安全
  • ONLYOFFICE 深度解锁系列.14-如何在ONLYOFFICE表格中调用异步API,集成外部数据源
  • 移动端WebView调试实战 跨域问题与授权失败的完整排查流程
  • C#反射的概念与实战
  • 论文阅读|ArxiV 2024|Mamba进一步研究|VSSD