java 注解@interface
类注解:
package com.cglibs; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; // 适用类、接口(包括注解类型)或枚举
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassInfo {
String value();
}
field属性注解
package com.cglibs; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; // 适用field属性,也包括enum常量
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldInfo {
int[] value();
}
方法注解
package com.cglibs; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; // 适用方法
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodInfo {
String name() default "long";
String data();
int age() default 27;
}
参数注解
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.cglibs; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoUserModel {
boolean value() default true;
boolean require() default true;
String message() default "user is null";
}
测试类
package com.cglibs; /**
* 测试运行时注解
*/
@ClassInfo("Test Class")
public class RuntimeAnnotation { @FieldInfo(value = {1, 2})
public String fieldInfo = "FiledInfo"; @FieldInfo(value = {10086})
public int i = 100; @MethodInfo(name = "BlueBird", data = "Big")
public static String getMethodInfo(@AutoUserModel(value = false,require = true) @AutoPlatformModel String a) {
System.out.println("RuntimeAnnotation.getMethodInfo "+a);
return RuntimeAnnotation.class.getSimpleName();
}
}
运行
package com.cglibs; import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays; /**
* 测试运行时注解
*/
public class TestRuntimeAnnotation {
/**
* 测试运行时注解
*/
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
Class<?> cls = RuntimeAnnotation.class;
// 获取指定类型的注解
sb.append("Class注解:").append("\n");
ClassInfo classInfo = cls.getAnnotation(ClassInfo.class);
if (classInfo != null) {
sb.append(Modifier.toString(cls.getModifiers())).append(" ")
.append(cls.getSimpleName()).append("\n");
sb.append("注解值: ").append(classInfo.value()).append("\n\n");
} sb.append("Field注解:").append("\n");
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
FieldInfo fieldInfo = field.getAnnotation(FieldInfo.class);
if (fieldInfo != null) {
sb.append(Modifier.toString(field.getModifiers())).append(" ")
.append(field.getType().getSimpleName()).append(" ")
.append(field.getName()).append("\n");
sb.append("注解值: ").append(Arrays.toString(fieldInfo.value())).append("\n\n");
}
} sb.append("Method注解:").append("\n");
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
if (methodInfo != null) {
sb.append(Modifier.toString(method.getModifiers())).append(" ")
.append(method.getReturnType().getSimpleName()).append(" ")
.append(method.getName()).append("\n");
sb.append("注解值: ").append("\n");
sb.append("name: ").append(methodInfo.name()).append("\n");
sb.append("data: ").append(methodInfo.data()).append("\n");
sb.append("age: ").append(methodInfo.age()).append("\n");
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (Annotation[] a : parameterAnnotations
) {
if (a != null) {
for (Annotation b : a
) {
if (AutoUserModel.class.isAssignableFrom(b.annotationType())) {
AutoUserModel b1 = (AutoUserModel) b;
sb.append("找到了 AutoUserModel =").append(b1.value()).append("\n");
}
if (AutoPlatformModel.class.isAssignableFrom(b.annotationType())) {
AutoPlatformModel b1 = (AutoPlatformModel) b;
sb.append("找到了 AutoPlatformModel =").append(b1.value()).append("\n");
}
}
} }
}
}
System.out.print(sb.toString());
}
}
java 注解@interface的更多相关文章
- JAVA注解@Interface基础知识
java注解是在JDK5时引入的新特性,大多数框架(SpringBoot.MyBatis.Quartz)背后都在大量使用注解开发. 一.先进行一个小试验,了解注解开发流程 建立maven项目annot ...
- @interface java注解
@Documented,@Retention,@Target,@Inherited 1. 编写自定义@Todo注解经常我们在写程序时,有时候有些功能在当前的版本中并不提供,或由于某些其它原因,有些方法 ...
- Java注解
Java注解其实是代码里的特殊标记,使用其他工具可以对其进行处理.注解是一种元数据,起到了描述.配置的作用,生成文档,所有的注解都隐式地扩展自java.lang.annotation.Annotati ...
- 19.Java 注解
19.Java注解 1.Java内置注解----注解代码 @Deprecated //不推荐使用的过时方法 @Deprecated ...
- Java注释@interface的用法【转】
Java用 @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@SuppressWarnings为 ...
- Java注解入门
注解的分类 按运行机制分: 源码注解:只在源码中存在,编译后不存在 编译时注解:源码和编译后的class文件都存在(如@Override,@Deprecated,@SuppressWarnin ...
- java注解(Annotation)解析
注解(Annotation)在java中应用非常广泛.它既能帮助我们在编码中减少错误,(比如最常见的Override注解),还可以帮助我们减少各种xml文件的配置,比如定义AOP切面用@AspectJ ...
- JAVA 注解的几大作用及使用方法详解
JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...
- Java注释@interface的用法
转---------- java用 @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@Suppr ...
随机推荐
- Controllers返回View的一个完整流程
详细说明一个MVC框架下,返回一个view的原理.如下图: 上图粗略的说明了一个返回View的流程,细节如下: 1.定义Model类: 2.定义接口添加接口约束为class: 3.定义接口实现类,即对 ...
- python_模块2
1.sys模块 import sys # 获取一个值的应用计数 a = [11,22,33] b = a print(sys.getrefcount(a)) # python默认支持的递归数量 v1 ...
- 22-系统SQL整理
1.查看内存使用情况 select * from sys.dm_os_sys_memory 2.查看当前进程的情况 select * from sys.sysprocesses 3.查看非系统进程的 ...
- express框架封装前戏
一.开启一文件,这里暂且命名为aexpressclass.js 声明一个app类,用来模仿http模块中的回调函数 //var route = require('http-route'); var u ...
- IIS上传限制大小
加入下面的配置即可 <?xml version="1.0" encoding="UTF-8"?> <configuration> < ...
- 【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
题目如下: Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square ...
- UCOSIII(一)
一,前后台系统和RTOS 1,前后台系统 早期嵌入式开发没有嵌入式操作系统的概念 ,直接操作裸机,在裸机上写程序,比如用51单片机基本就没有操作系统的概念.通常把程序分为两部分:前台系统和后台系统. ...
- Codeforces.520B.Two Buttons(正难则反)
题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...
- Echarts案例-折线图
一:先在官网下载 https://www.echartsjs.com/zh/download.html 然后再建立工程,导入这两个包: 写代码: <!DOCTYPE html> <h ...
- java经典算法题50道
原文 JAVA经典算法50题[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序 ...