注解
package reflect;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
@myAnnotation()
public class Test01 {
@myAnnotation(value=1)
public static void main(String[] args) throws Exception {
Class c = Class.forName("reflect.Test01");
Method method=c.getDeclaredMethod("main", String[].class);
myAnnotation declaredAnnotation = method.getDeclaredAnnotation(myAnnotation.class);
//获得注解的值
int value=declaredAnnotation.value();
String name=declaredAnnotation.name();
System.out.println(value);
System.out.println(name);
//判断某个方法上是否存在某注解。
boolean annotationPresent = method.isAnnotationPresent(myAnnotation.class);
System.out.println(annotationPresent);
}
}
/**
* 定义一个注解
*/
@Target(value = {ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface myAnnotation{
int value() default -1;//注解
String name() default "zhangsan";
}
反射
动态语言
在运行时可以根据某些条件改变其自身结构的语言。
动态语言:C# JavaScript PHP PYTHON
静态语言
与动态语言相反。
例如:java C C++
但是因为反射机制,java有了一定的动态性,称为“准动态语言”
反射概念
允许程序在执行期间借助Reflection API 取得任何类的内部信息,并能够操作任意对象的内部属性和方法。
一个类只有一个Class对象,其包含了完整的类信息。通过这个对象看到类的结构
User类
package reflect;
public class User {
int id;
String name;
int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public void print(){
System.out.println(this);
}
private void printName(String name){
System.out.println(name);
}
}
测试反射
package reflect;
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Map;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException {
//反射得到class对象的三种方法
Class c1=User.class;
Class c2=Class.forName("reflect.User");
User u=new User();
Class c3=u.getClass();
Class<Integer> type1 = Integer.TYPE;
System.out.println(c1.hashCode());
System.out.println(c3.hashCode());
System.out.println(c2.hashCode());
//根据反射的class对象得到构造方法
Constructor[] constructors=c1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
//根据class对象创建实例
User user=(User) c1.newInstance();
System.out.println(user);
//根据指定的构造方法创建对象实例
Constructor constructor=c1.getConstructor(int.class,String.class,int.class);
User user1= (User) constructor.newInstance(1, "zhangsan", 18);
System.out.println(user1);
//获取域
Field[] fields=c1.getFields();
for (Field field : fields) {
System.out.println(field);
}
fields=c1.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
Field field=c1.getDeclaredField("name");
field.setAccessible(true);
field.set(user1,"lisi");
System.out.print(user1);
Method[] methods=c1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}
methods=c1.getMethods();
for (Method method : methods) {
System.out.println(method);
}
Method method=c1.getDeclaredMethod("printName",String.class);
method.setAccessible(true);
method.invoke(user,"wangwu");
method=Class.forName("reflect.Test02").getMethod("fanxing",Map.class);
Type[] types=method.getGenericParameterTypes();
for (Type type : types) {
System.out.println(type);
if(type instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
}
public void fanxing(Map<String,User> map){
map=new HashMap<>();
}
}