zoukankan      html  css  js  c++  java
  • java Annotation注解的运用

    1、对于其概念和定义就不说了,直接看一下代码。

    2、

    Description.java

    package com.lixx.annotation;

    import java.lang.annotation.Documented;

    import java.lang.annotation.ElementType;

    import java.lang.annotation.Retention;

    import java.lang.annotation.RetentionPolicy;

    import java.lang.annotation.Target;

    @Target(ElementType.TYPE)

    @Retention(RetentionPolicy.RUNTIME)

    @Documented

    public @interface Description {

    String value();

    }

    Name.java

    package com.lixx.annotation;

    import java.lang.annotation.Documented;

    import java.lang.annotation.ElementType;

    import java.lang.annotation.Retention;

    import java.lang.annotation.RetentionPolicy;

    import java.lang.annotation.Target;

    @Target(ElementType.METHOD)

    @Retention(RetentionPolicy.RUNTIME)

    @Documented

    public @interface Name {

    String originate();

    String community();

    }

    JavaEyer.java

    package com.lixx.annotation;

    @Description(value="javaeye,做最棒的软件开发交流社区")

    public class JavaEyer {

    private String name;

    @Name(originate="创始人:robbin",community="javaEye")

    public String getName() {

    return name;

    }

    @Name(originate="创始人:江南白衣",community="springside")

    public void setName(String name) {

    this.name = name;

    }

    }

    TestAnnotation.java

    package com.lixx.annotation;

    import java.lang.reflect.Method;

    import java.util.HashSet;

    import java.util.Set;

    public class TestAnnotation {

    public static void main(String[] args) throws ClassNotFoundException{

    ClassNotFoundException ex ;

    String CLASS_NAME = "com.lixx.annotation.JavaEyer";

    try {

    Class test = Class.forName(CLASS_NAME);

    Method[] method = test.getMethods();

    boolean flag = test.isAnnotationPresent(Description.class);

    if(flag){

    Description des = (Description)test.getAnnotation(Description.class);

    System.out.println("描述为:"+des.value());

    }

    /**

    * 将javaeyer类中所有用到@Name的全部方法保存到set集合中去

    */

    Set<Method> set = new HashSet<Method>();

    for(Method methd : method){

    boolean otherFlag = methd.isAnnotationPresent(Name.class);

    if(otherFlag){

    set.add(methd);

    }

    }

    for(Method m : set){

    Name name = m.getAnnotation(Name.class);

    System.out.println(name.originate()+"  "+"创建的社区:"+name.community());

    }

    return ;

    } catch (ClassNotFoundException e) {

    ex = e;

    }

    if(null != ex){

    throw ex;

    }

    }

    }

    可以试着运行下....

  • 相关阅读:
    VScode中Python的交互式命令环境使用笔记
    jmeter beanshell判断响应的json串,参数的值是否正确;
    类和类的继承 实现关系;
    类与类依赖关系,实例;
    类与类包含关系,实例;
    java中有package的编译执行;java编译乱码;
    java 类与类之间的关系,方法重写与方法重载的区别
    beanshell sampler构造响应数据;
    Scanner类及其中方法的使用;
    java 构造方法 代码块 this
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100467.html
Copyright © 2011-2022 走看看