1.获取class类的实例

2.获取属性
2.1 获取声明为public的属性
field[] fields = clazz.getfields();
上述代码只能够获取声明为public的属性
代码演示:
import java.lang.reflect.constructor;
import java.lang.reflect.field;
import org.junit.test;
public class testconstructor {
@test
public void test1() throws exception {
string str = new string("exer03.person");
@suppresswarnings("unchecked")
class<person> clazz = (class<person>) class.forname(str);
//获取运行时类的属性
field[] fields = clazz.getfields();
for(int i = 0;i < fields.length;i++) {
system.out.println(fields[i]);//打印输出
}
}
}
在person类中,声明的属性有name、age

而通过field[] fields = clazz.getfields(); 获取的属性,结果如下:

2.1.1 如果将person类的属性的修饰符设定为非public

通过field[] fields = clazz.getfields(); 获取的属性,结果如下:

通过上述讲解,可以知道field[] fields = clazz.getfields(); 获取的属性的修饰符为public
2.1.2 获取父类的public的属性
父类creature
public class creature {
public boolean flag;
}
子类person


2.2 获取声明的属性
只要在类中声明的属性,都可以通过反射获取到
field[] fields = clazz.getdeclaredfields();
代码演示:
import java.lang.reflect.constructor;
import java.lang.reflect.field;
import org.junit.test;
public class testconstructor {
@test
public void test1() throws exception {
string str = new string("exer03.person");
@suppresswarnings("unchecked")
class<person> clazz = (class<person>) class.forname(str);
//获取运行时类的属性
field[] fields = clazz.getdeclaredfields();
for(field field : fields) {
system.out.println(field);
}
}
}在person类中,声明的属性有name、age、sex、address

而通过field[] fields = clazz.getdeclaredfields(); 获取的属性,结果如下:

2.3 getdeclaredfields()与getfields()区别
- getdeclaredfields() 只可以访问本类当中的属性
- getfields()只能访问声明为public的属性
- getdeclaredfields() 可以访问声明为任意权限修饰符的属性
- getfields() 可以访问父类中的声明为public的属性
2.4 通过属性获取权限修饰符、变量类型和变量名
2.4.1 获取权限修饰符
int i = field.getmodifiers();//返回值为int型
返回类型说明:
- 权限修饰符为default 返回值0
- 权限修饰符为public 返回值1
- 权限修饰符为private 返回值2
- 权限修饰符为protected 返回值4

使用modifier.tostring(int i) 以字符串形式打印出来
string str1 = modifier.tostring(i);//i 是指 0 1 2 4等
代码示例:
import java.lang.reflect.constructor;
import java.lang.reflect.field;
import java.lang.reflect.modifier;
import org.junit.test;
public class testconstructor {
@test
public void test1() throws exception {
string str = new string("exer03.person");
@suppresswarnings("unchecked")
class<person> clazz = (class<person>) class.forname(str);
//获取运行时类的属性
field[] fields = clazz.getdeclaredfields();
for(field field : fields) {
//1.获取属性的权限修饰符
int i = field.getmodifiers();//返回值为int型
string str1 = modifier.tostring(i);
system.out.print(i + " ");
}
}
}结果截图:

2.4.2 获取变量类型
class c = field.gettype();//返回值类型:class
代码示例:
import java.lang.reflect.constructor;
import java.lang.reflect.field;
import java.lang.reflect.modifier;
import org.junit.test;
public class testconstructor {
@test
public void test1() throws exception {
string str = new string("exer03.person");
@suppresswarnings("unchecked")
class<person> clazz = (class<person>) class.forname(str);
//获取运行时类的属性
field[] fields = clazz.getdeclaredfields();
for(field field : fields) {
//1.获取属性的权限修饰符
// int i = field.getmodifiers();//返回值为int型
// string str1 = modifier.tostring(i);
// system.out.print(str1 + " ");
//2.获取属性的变量类型
class c = field.gettype();
system.out.print(c.getname() + " ");
system.out.println();
}
}
}结果截图:

2.4.3 获取变量名
system.out.println(field.getname());
代码演示:
import java.lang.reflect.constructor;
import java.lang.reflect.field;
import java.lang.reflect.modifier;
import org.junit.test;
public class testconstructor {
@test
public void test1() throws exception {
string str = new string("exer03.person");
@suppresswarnings("unchecked")
class<person> clazz = (class<person>) class.forname(str);
//获取运行时类的属性
field[] fields = clazz.getdeclaredfields();
for(field field : fields) {
//1.获取属性的权限修饰符
int i = field.getmodifiers();//返回值为int型
string str1 = modifier.tostring(i);
system.out.print(str1 + " ");
//2.获取属性的变量类型
class c = field.gettype();
system.out.print(c.getname() + " ");
//3.获取属性的变量名
system.out.println(field.getname());
system.out.println();
}
}
}效果截图:

3.获取方法
3.1getmethods()和getdeclaredmethods()
获取方法与获取属性相似!同样具有两种方法
- method[] methods = clazz.getmethods();
- method[] methods2 = clazz.getdeclaredmethods();
import java.lang.reflect.method;
import org.junit.test;
public class testmethon {
@test
public void test1() {
class<person> clazz = person.class;
//getmethods():获取运行时类及其父类的声明为public的方法
method[] methods = clazz.getmethods();
for(method method : methods) {
system.out.println(method);
}
//getdeclaredmethods() : 获取运行时类的所有方法
method[] methods2 = clazz.getdeclaredmethods();
for(method method : methods2) {
system.out.println(method);
}
}
}结果截图:
method[] methods = clazz.getmethods();得到的结果

method[] methods2 = clazz.getdeclaredmethods();得到的结果

| 1 | 注解 |
| 2 | 权限修饰符 |
| 3 | 返回值类型 |
| 4 | 方法名 |
| 5 | 形参列表 |
| 6 | 异常 |
3.2 获取注解
annotation[] annotation = method.getannotations();
3.2.1 如何创建注解?
要想获取注解,就一定要有注解!
代码示例:(自定义注解)
import static java.lang.annotation.elementtype.constructor;
import static java.lang.annotation.elementtype.field;
import static java.lang.annotation.elementtype.local_variable;
import static java.lang.annotation.elementtype.method;
import static java.lang.annotation.elementtype.parameter;
import static java.lang.annotation.elementtype.type;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
@target({type, field, method, parameter, constructor, local_variable})
@retention(retentionpolicy.runtime)
public @interface myannotation {
string[] value();
}3.2.2 使用自定义注解
代码示例:
在一个类中的方法上,添加注解
@myannotation(value = "i am person")
public void show() {
system.out.println("我是person类");
}3.2.3 获取注解
import java.lang.annotation.annotation;
import java.lang.reflect.method;
import org.junit.test;
public class testmethon {
@test
public void test2() {
class<person> clazz = person.class;
method[] methods = clazz.getdeclaredmethods();
for(method method : methods) {
//1.获取注解
annotation[] annotation = method.getannotations();
for(annotation a : annotation) {
system.out.println(a);
}
//获取方法名
system.out.print(method.getname() + " ");
system.out.println();
}
}
}结果截图:

3.3 获取权限修饰符
获取权限修饰符,与前面获取属性的权限修饰符一样。
int i = method.getmodifiers();
代码演示:
import java.lang.annotation.annotation;
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import org.junit.test;
public class testmethon {
@test
public void test2() {
class<person> clazz = person.class;
method[] methods = clazz.getdeclaredmethods();
for(method method : methods) {
//1.获取注解
annotation[] annotation = method.getannotations();
for(annotation a : annotation) {
system.out.println(a);
}
//2.获取权限修饰符
int i = method.getmodifiers();
string str = modifier.tostring(i);
system.out.print(str + " ");
//获取方法名
system.out.print(method.getname() + " ");
system.out.println();
}
}
}效果截图:

3.4 获取返回值类型
class returntype = method.getreturntype();
代码示例:
import java.lang.annotation.annotation;
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import org.junit.test;
public class testmethon {
@test
public void test2() {
class<person> clazz = person.class;
method[] methods = clazz.getdeclaredmethods();
for(method method : methods) {
//1.获取注解
annotation[] annotation = method.getannotations();
for(annotation a : annotation) {
system.out.println(a);
}
//2.获取权限修饰符
int i = method.getmodifiers();
string str = modifier.tostring(i);
system.out.print(str + " ");
//3.获取返回类型
class returntype = method.getreturntype();
system.out.print(returntype.getname() + " ");
//获取方法名
system.out.print(method.getname() + " ");
system.out.println();
}
}
}效果截图:

3.5 获取方法名
system.out.print(method.getname() + " ");
3.6 获取参数列表
class[] param = method.getparametertypes();
打印输出参数列表
class[] param = method.getparametertypes();
system.out.print("(");
for(int j = 0;j < param.length;j++) {
if(j == param.length - 1) {
system.out.print(param[j].getname() + " args-" + j);
}else {
system.out.print(param[j].getname() + " args-" + j + ",");
}
}
system.out.print(")");代码示例
import java.lang.annotation.annotation;
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import org.junit.test;
public class testmethon {
@test
public void test2() {
class<person> clazz = person.class;
method[] methods = clazz.getdeclaredmethods();
for(method method : methods) {
//1.获取注解
annotation[] annotation = method.getannotations();
for(annotation a : annotation) {
system.out.println(a);
}
//2.获取权限修饰符
int i = method.getmodifiers();
string str = modifier.tostring(i);
system.out.print(str + " ");
//3.获取返回类型
class returntype = method.getreturntype();
system.out.print(returntype.getname() + " ");
//4.获取方法名
system.out.print(method.getname() + " ");
//5.获取参数列表
class[] param = method.getparametertypes();
system.out.print("(");
for(int j = 0;j < param.length;j++) {
if(j == param.length - 1) {
system.out.print(param[j].getname() + " args-" + j);
}else {
system.out.print(param[j].getname() + " args-" + j + ",");
}
}
system.out.print(")");
system.out.println();
}
}
}效果截图:

3.7 获取异常
class[] exceptions = method.getexceptiontypes();
打印输出异常
class[] exceptions = method.getexceptiontypes();
if(exceptions.length != 0) {
//如果有异常,则加上throws
system.out.print(" throws ");
}
for(int j = 0;j < exceptions.length;j++) {
system.out.print(exceptions[j].getname() + " ");
}代码示例(差不多是打印完整的方法结构)
import java.lang.annotation.annotation;
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import org.junit.test;
public class testmethon {
@test
public void test2() {
class<person> clazz = person.class;
method[] methods = clazz.getdeclaredmethods();
for(method method : methods) {
//1.获取注解
annotation[] annotation = method.getannotations();
for(annotation a : annotation) {
system.out.println(a);
}
//2.获取权限修饰符
int i = method.getmodifiers();
string str = modifier.tostring(i);
system.out.print(str + " ");
//3.获取返回类型
class returntype = method.getreturntype();
system.out.print(returntype.getname() + " ");
//4.获取方法名
system.out.print(method.getname());
//5.获取参数列表
class[] param = method.getparametertypes();
system.out.print("(");
for(int j = 0;j < param.length;j++) {
if(j == param.length - 1) {
system.out.print(param[j].getname() + " args-" + j);
}else {
system.out.print(param[j].getname() + " args-" + j + ",");
}
}
system.out.print(")");
//6.获取异常
class[] exceptions = method.getexceptiontypes();
if(exceptions.length != 0) {
//如果有异常,则加上throws
system.out.print(" throws ");
}
for(int j = 0;j < exceptions.length;j++) {
system.out.print(exceptions[j].getname() + " ");
}
system.out.println();
}
}
}效果截图:

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论