java Annotation

1.getDeclaredMethods() 和getMethods()的区别

 getDeclaredMethods()
   返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。 

 getMethods()
   返回一个包含某些 Method 对象的数组,这些对象反映此 Class 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。

2.Class.forName()用法

Class.forName(xxx.xx.xx)返回的是一个类
Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类,
也就是说JVM会执行该类的静态代码段


3.cls.isAnnotationPresent(myAnnotation1.class);//判断该类是否是参数中类的注解,是true,否false
4.举例学习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 myAnnotation1 {

 String value();

}

import java.lang.annotation.Documented;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)   //这句如果没有,后面就检测不到该Annotation

@Documented

public @interface myAnnotation2 {  

String description();

boolean isAnnotation(); }

 

@myAnnotation1("this is annotation1")

public class AnnotationDemo {  

@myAnnotation2(description = "this is annotaion2", isAnnotation = true)

 public void sayHello() {   System.out.println("hello world!");

 }

}

package com.mmscn.test2;

import java.lang.reflect.Method;

import android.util.Log;

public class TestAnnotation {  

public void test() throws ClassNotFoundException, NoSuchMethodException {

  Class<?> cls = Class.forName("com.mmscn.test2.AnnotationDemo");

  boolean flag = cls.isAnnotationPresent(myAnnotation1.class);  

 Log.i("flag", "=====" + flag);  

 if (flag) {   

 System.out.println("判断类是annotation");   

 myAnnotation1 annotation1 = cls.getAnnotation(myAnnotation1.class);    

System.out.println(annotation1.value());  

 }

  Method method = cls.getMethod("sayHello");

  Log.i("method", "=====*****" + method.getName());   

flag = method.isAnnotationPresent(myAnnotation2.class);  

 Log.i("flag", "=====*****" + flag);   

if (flag) {    

System.out.println("判断方法也是annotation");    

myAnnotation2 annotation2 = method.getAnnotation(myAnnotation2.class);   

 System.out.println(annotation2.description() + "===-=="      + annotation2.isAnnotation());  

 }  }

 public TestAnnotation() {

  // TODO Auto-generated constructor stub  }

}

 

public class MainActivity extends Activity {


 @Override  

protected void onCreate(Bundle savedInstanceState) {   

super.onCreate(savedInstanceState);  

 setContentView(R.layout.activity_main);  

 TestAnnotation atestAnnotation = new TestAnnotation();   

try {    

atestAnnotation.test();   

}

catch (ClassNotFoundException e) {    

// TODO Auto-generated catch block    

e.printStackTrace();   

} catch (NoSuchMethodException e) {  

  // TODO Auto-generated catch block    

e.printStackTrace();   }  }}

 

 


posted @ 2015-09-23 18:46  xiaoxiaoxiaoV  阅读(168)  评论(0编辑  收藏  举报