类注解:

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的更多相关文章

  1. JAVA注解@Interface基础知识

    java注解是在JDK5时引入的新特性,大多数框架(SpringBoot.MyBatis.Quartz)背后都在大量使用注解开发. 一.先进行一个小试验,了解注解开发流程 建立maven项目annot ...

  2. @interface java注解

    @Documented,@Retention,@Target,@Inherited 1. 编写自定义@Todo注解经常我们在写程序时,有时候有些功能在当前的版本中并不提供,或由于某些其它原因,有些方法 ...

  3. Java注解

    Java注解其实是代码里的特殊标记,使用其他工具可以对其进行处理.注解是一种元数据,起到了描述.配置的作用,生成文档,所有的注解都隐式地扩展自java.lang.annotation.Annotati ...

  4. 19.Java 注解

    19.Java注解 1.Java内置注解----注解代码 @Deprecated                                    //不推荐使用的过时方法 @Deprecated ...

  5. Java注释@interface的用法【转】

    Java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@SuppressWarnings为 ...

  6. Java注解入门

    注解的分类   按运行机制分:   源码注解:只在源码中存在,编译后不存在 编译时注解:源码和编译后的class文件都存在(如@Override,@Deprecated,@SuppressWarnin ...

  7. java注解(Annotation)解析

    注解(Annotation)在java中应用非常广泛.它既能帮助我们在编码中减少错误,(比如最常见的Override注解),还可以帮助我们减少各种xml文件的配置,比如定义AOP切面用@AspectJ ...

  8. JAVA 注解的几大作用及使用方法详解

    JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...

  9. Java注释@interface的用法

    转---------- java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@Suppr ...

随机推荐

  1. 2020 WPF开发革命性时代,DevExpress为你护航

    下载DevExpress v19.2完整版 通过DevExpress WPF Controls,您能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸 ...

  2. phpstorm和ftp搭配使用

    简单使用

  3. Java锁机制总结

    锁是用于控制多线程对共享资源的访问. Java中的锁可以分为内置锁与显式锁Lock.其中内置锁指synchronized关键字. Synchronized synchronized可以修饰方法或代码块 ...

  4. [ES2019] Represent Collision-free String Constants as Symbols in JavaScript

    ES2019 introduces the Symbol.prototype.description property. In this lesson, we'll learn why this pr ...

  5. focus([[data],fn]) 当元素获得焦点时,触发 focus 事件。

    focus([[data],fn]) 概述 当元素获得焦点时,触发 focus 事件.直线电机选型 可以通过鼠标点击或者键盘上的TAB导航触发.这将触发所有绑定的focus函数,注意,某些对象不支持f ...

  6. 在使用Telnet连接localhost时所遇到的问题:出现 ‘telnet’ 不是内部或外部命令,也不是可运行的程序或批处理文件

    1.出现 ‘telnet’ 不是内部或外部命令,也不是可运行的程序或批处理文件.原因:因为本机的Telnet客户端默认是关闭的,所以我们要手动打开解决方案:打开控制面板–>程序–>打开或关 ...

  7. Codevs 1038 一元三次方程求解 NOIP 2001(导数 牛顿迭代)

    1038 一元三次方程求解 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 有形如:ax3+b ...

  8. 6502 assemble 条件判断

    LDA #$ CMP #$ BNE notequal STA $ notequal: BRK

  9. ORM SQLAlchemy - 对象关联

    >>> from sqlalchemy import Column, Integer, String >>> class User(Base): ... __tab ...

  10. 异步机制 - IO完成端口

    1 KQUEUE KeInitializeQueue VOID KeInitializeQueue( IN PKQUEUE  Queue, IN ULONG  Count  OPTIONAL ); l ...