Java基础之--反射、自定义注解

反射的定义:通过用字符串创建对象,通过字符串来调用函数。

例:
package reflex;

import org.junit.Test;
/**
 * 反射实例
 * @author Super
 */
public class TestReflex {
  /**
    * 正常创建对象方式
    *(正常创建对象是用实现类的方式)
    */
    @Test
     public void Test01() {
         User u = new User();
              u.setId(1);
              u.setName( "嘿1");
         System. out.println(u);
     }
    /**
     * 通过反射创建对象方式
     *(反射创建对象的方式是通过字符串)
     */
     @Test
      public void Test02() {
          try {
                Class cla = Class.forName( "reflex.User");
                User u = (User) cla.newInstance();
                     u.setId(2);
                     u.setName( "哈2");
                System. out.println(u);
              } catch (ClassNotFoundException e) {
                       e.printStackTrace();
              } catch (InstantiationException e) {
                       e.printStackTrace();
              } catch (IllegalAccessException e) {
                       e.printStackTrace();
              }
     }
      /**
       * 通过反射创建对象,并调用对象中的函数(方法)
       */
       @Test
        public void Test03() {
             try {
                   Class cla = Class.forName( "reflex.User");
                   User u = (User) cla.newInstance();
                        u.setId(3);
                        u.setName( "嗯3");
                    Method method = cla.getMethod( "toString");
                    System. out.println(method.invoke(u));
                 } catch (ClassNotFoundException e) {
                         e.printStackTrace();
                 } catch (InstantiationException e) {
                         e.printStackTrace();
                 } catch (IllegalAccessException e) {
                         e.printStackTrace();
                 } catch (NoSuchMethodException e) {
                         e.printStackTrace();
                 } catch (SecurityException e) {
                         e.printStackTrace();
                 } catch (IllegalArgumentException e) {
                         e.printStackTrace();
                 } catch (InvocationTargetException e) {
                         e.printStackTrace();
                 }
          }
      /**
        * 通过反射创建对象,并调用对象中的静态方法
        */
        @Test
         public void Test04() {
            try {
                  Class cla = Class.forName( "reflex.User");
                  Method method = cla.getMethod( "say", int.class, String. class);
                  method.invoke(cla, 5, "你愁啥5");
            } catch (ClassNotFoundException e) {
                    e.printStackTrace();
            } catch (NoSuchMethodException e) {
                    e.printStackTrace();
            } catch (SecurityException e) {
                    e.printStackTrace();
            } catch (IllegalAccessException e) {
                    e.printStackTrace();
            } catch (IllegalArgumentException e) {
                    e.printStackTrace();
            } catch (InvocationTargetException e) {
                    e.printStackTrace();
            }

       }
}



package model;
/**
 * 自定义注解(annotation)
 * @author ShiXiaoLei
 *
 */
/*
 * @Retention(RetentionPolicy.RUNTIME):表示运行时刻执行,否则运行时刻不会检测这个注解
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface ShotDi {
   /*
    * 表示在ShotDi(注解)中定义了一个String类型,名为 str的属性,如果没有定义default,
    * 必须在该annotation中定义:@ShotDi(str="xxx")
    */
    String str() default "";
    /*
     * value为annotation中默认属性值,在定义的时候可以不用value=""定义
     * 直接写为:@ShotDi("xxx")
     * 特别注意:当需要定义两个或两个以上属性时,默认属性不起作用,
     * 写为:@ShotDi(value="xx",str="xx")
     */
     String value() default "";
}
通过反射获取注解中的值:
package annotation;

import java.lang.reflect.Method;

import org.junit.Test;

public class CheckAnnotation {
    @AnnotationTest( "xxxDao")
    public void setXxxDao(){}
    @AnnotationTest
    public void setOooDao(){}
    @Test
    public void test01(){
         Method[] methods = this.getClass().getDeclaredMethods();
               for(Method method : methods){
                     if(method.isAnnotationPresent(AnnotationTest.class)){
                             AnnotationTest at = method.getAnnotation(AnnotationTest. class);
                             String str = at.value();
                             if(str== null|| "". equals(str.trim())){
                                   str = method.getName().substring(3);
                                   str = str.substring(0,1).toLowerCase()+str.substring(1);
                             }
                             System. out.println(str);
                     }
               }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值