java 反射机制

网友投稿 206 2023-06-15


java 反射机制

本文导引:

通过反射机制

获取类的基本信息

获取类的注解信息

获取泛型信息

package reflection;

@AnnotationUserTable("datebaseExample")

public class User {

@AnnotationUserField(uName="name",type="varchar",length=10)

private String name;

@AnnotationUserField(uName="age",type="int",length=3)

private int age;

@AnnotationUserField(uName="sex",type="char",length=2)

private String sex;

public User() {

super();

}

public User(String name, int age, String sex) {

super();

this.name = name;

this.age = age;

this.sex = sex;

}

public String getName() {

return name;

}

public void setName() {

this.name = "test";

}

public int getAge() {

return age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

}

bean:User

package reflection;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(value={ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationUserTable {

String value();

}

自定义注解:类注解

package reflection;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(value={ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationUserField {

String uName();

String type();

int length();

}

自定义注解:属性注解

package reflection;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class Demo01 {

static Class> c = null;

public static void main(String[] args) {

try {

c = Class.forName("reflection.User");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

test();//获取类的属性、方法等信息

}

static void test(){

try {

// 获取类的名称

System.out.println("获取类的名称");

System.out.println("getName():" + c.getName());// 获得包名+类名

System.out.println("getSimpleName():" + c.getSimpleName());// 获得类名

System.out.println("getCanonicalName():" + c.getCanonicalName());// 获得类名

System.out.println("*******************************");

// 获取属性信息

System.out.println("获取属性信息");

Field[] fields = c.getDeclaredFields();

// Field[] fields = c.getFields(); 只能获取public修饰的属性信息

for (Field f : fields) {

String fName = f.getName();

System.out.println(c.getDeclaredField(fName));

}

System.out.println("*******************************");

// 获取方法信息

System.out.println("获取方法信息");

Method[] methods = c.getDeclaredMethods();

for (Method m : methods) {

// String mName = m.getName();

System.out.println(m.getName() + "-->" + m);

}

System.out.println("通过名称单独获取对应的getName方法:" + c.getDeclaredMethod("getName"));

System.out.println("通过名称单独获取对应的setSex方法:" + c.getDeclaredMethod("setSex", String.class));// 方法有参,必须传递参数类型

System.out.println("*******************************");

// 获取构造器信息

System.out.println("获取构造器信息");

Constructor>[] constructor = c.getConstructors();

for (Constructor> cons : constructor) {

System.out.println(cons);

}

} catch (NoSuchFieldException | SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

}

}

main1:通过反射机制获取类的基本信息

output:

获取类的名称

getName():reflection.User

getSimpleName():User

getCanonicalName():reflection.User

*******************************

获取属性信息

private java.lang.String reflection.User.name

private int reflection.User.age

private java.lang.String reflection.User.sex

****http://***************************

获取方法信息

getName-->public java.lang.String reflection.User.getName()

setName-->public void reflection.User.setName()

setSex-->public void reflection.User.setSex(java.lang.String)

getSex-->public java.lang.String reflection.User.getSex()

getAge-->public int reflection.User.getAge()

通过名称单独获取对应的getName方法:public java.lang.String reflection.User.getName()

通过名称单独获取对应的setSex方法:public void reflection.User.setSex(java.lang.String)

*******************************

获取构造器信息

public reflection.User()

public reflection.User(java.lang.String,int,java.lang.String)

View Console

下面的例子,是通过反射机制获取类的注解信息。

package reflection;

import java.lang.reflect.Field;

/**

* 获取类的属性、方法等信息

* 1.获取元素对象(如属性)(注意:读取类的注解,看似要少一步)

* 2.获取该元素对象的指定类型的注解对象

* 3.读取注解对象相应的值

*/

public class Test02 {

static Class> c = null;

public static void main(String[] args) {

try {

c = Class.forName("reflection.User");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

test();

}

static void test(){

try {

// 获取类的指定注解

System.out.println("***********类的指定注解**************");

AnnotationUserTable table = (AnnotationUserTable)c.getAnnotation(AnnotationUserTable.class);

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

// 获取属性的指定注解

System.out.println("***********属性的指定注解*************");

Field field = c.getDeclaredField("name");

AnnotationUserField annoField = (AnnotationUserField)field.getAnnotation(AnnotationUserField.class);

System.out.println(annoField.uName()+"\t"+annoField.type()+"\t"+annoField.length());

// 根据获得的表名、字段的信息,拼写出DDL语句,然后通过JDBC连接数据库查询

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

}

}

}

output:

***********类的指定注解**************

datebaseExample

***********属性的指定注解*************

name varchar 10

下面的例子,是通过反射机制获取泛型信息

package reflection;

import java.lang.reflect.Method;

import java.lang.reflect.Type;

import java.util.List;

import java.util.Map;

/**

* 通过反射机制获取泛型

* @author Administrator

*

*/

public class Test03 {

public static void main(String[] args) {

Class> c = Test03.class;

keaHZqlH try {

System.out.println("*******获取参数值的类型**********");

Method m1 = c.getDeclaredMethod("method01", Map.class,List.class);

Type[] types = m1.getGenericParameterTypes();

for(Type t:types){

System.out.println(t.getTypeName());

System.out.println(t.toString());

}

System.out.println("*******获取返回值的类型**********");

Method m2 = c.getDeclaredMethod("method02");

Type ret = m2.getGenericReturnType();

System.out.println(ret.getTypeName());

System.out.println(ret.toString());

} catch (NoSuchMethodException | SecurityException e) {

e.printStackTrace();

}

}

public void method01(Map args1,List args2){

}

public Map method02(){

return null;

}

}

通过反射机制获取泛型信息

output:

java.util.Map

java.util.Map

java.util.Map

java.util.Map

java.util.List

java.util.List

View Console


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:BootStrap table删除指定行的注意事项(笔记整理)
下一篇:Spring集成MyBatis框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~