Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7068707
  • 博文数量: 702
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12031
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院云平台架构师,专注于数字化、智能化,技术方向:云、Linux内核、AI、MES/ERP/CRM/OA、物联网、传感器、大数据、ML、微服务。

文章分类

全部博文(702)

分类: Java

2014-02-20 18:47:35

最近好好的看了一下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;

import org.springframework.stereotype.Component;

/**
 * @see jp.hiyachen.fw.web.struts.action.resolver.BLogicActionResolver
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Component
public @interface ActionComponent {

    String value() default "";

    String[] path() default {};
}

package jp.hiyachen.fw.web.struts.action.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;

import org.springframework.stereotype.Component;

/**
 * @see jp.terasoluna.fw.web.struts.action.resolver.BLogicActionResolver
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Component
public @interface BLogicComponent {

    String value() default "";

    String[] path() default {};
}

package atest.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Description {
    String value();
}

package atest.annotation;

@Description("hiyatest,做最棒的软件开发交流社区")
public class Hiyatest {
    @Name(originate="创始人:hubert",community="hiyatest")
    public String getName()
    {
        return null;
     }
     @Name(originate="创始人:红红火火",community="springside")
     public String getName2()
     {
         return "借用两位的id一用,写这一个例子,请见谅!";
     }
}

package atest.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Name {
     String originate();
     String community();
}

package atest.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

public class TestAnnotation {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        String  CLASS_NAME = "atest.annotation.Hiyatest";
        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());
             System.out.println("-----------------");
         }

         //把JavaEyer这一类有利用到@Name的全部方法保存到Set中去
         Set set = new HashSet();
         for(int i=0;i          {
             boolean otherFlag = method[i].isAnnotationPresent((Class) Name.class);
             if(otherFlag) set.add(method[i]);
         }
         for(Method m: set)
         {
             Name name = m.getAnnotation(Name.class);
             System.out.println(name.originate());
             System.out.println("创建的社区:"+name.community());
         }
      }

}

阅读(2441) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~