反射的定义:通过用字符串创建对象,通过字符串来调用函数。
例:
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);
}
}
}
}