3.1 Method中操作参数
Method类-关于方法参数的操作
(1) int getParameterCount()
获取该Method对象所表示方法的参数数量。
(2) Parameter[] getParameters()
返回一个Parameter对象数组,表示该Method对象所表示方法的所有参数。
(3) Class<?>[] getParameterTypes()
获取该Method对象所表示方法的所有参数类型的数组。
(4) Type[] getGenericParameterTypes()
获取该Method对象所表示方法的所有参数类型的数组,一般情况下,同getParameterTypes()方 法的返回值一样,如果存在泛型。则该方法会返回泛型的类型。
代码举例:
public class MethodReflect1 {
public static void main(String[] args) {
Class<? extends MyClass> aClass =MyClass.class;
Method[] methods=aClass.getDeclaredMethods() ;
for (Method method: methods) {
//获取该方法的参数数量
int parameterCount=method. getParameterCount () ;
//获取参数类型数组,getParameterTypes ()和getGenericParameterTypes()方法都可以获取,一般情况下相同,只有在泛型的情况下有所不同
Class<?>[] parameterTypes=method.getParameterTypes() ;
Type[] genericParameterTypes=method. getGenericParameterTypes() ;
for (int i=0; i<parameterTypes.length;i++) {
System. out. println("方法名: " +method. getName()+",参数个数:"+parameterCount+" ,参数类型:" +parameterTypes[i]. getTypeName ()
+"," +genericParameterTypes[i]. getTypeName()+"\t");
System. out. println();
}
}
}
}
class MyClass{
public void m1(int i) {
}
public void m2(Boolean bool) {
}
public void m3(String str,int i) {
}
public void m4(List<String> list) {
}
public void m5(Integer... nums) {
}
}
输出结果:
3.2 Method中操作返回值
Method类-关于方法返回值的操作
(1)Class<?> getReturnType()
返回一个Class对象,该对象表示此Method对象表示的方法的正式返回值类型。
(2)Type getGenericReturnType()
返回一个Type对象,该对象表示此Method对象表示的方法的正式返回值类型。(如果返回值类型不是泛型,那么两个方法的返回值是一-致的;如果是泛型,getGenericReturnType()方法会返回泛型的类型)
代码举例:
public class MethodReflect2 {
public static void main(String[] args) {
Class<? extends MyClass1> aClass =MyClass1.class;
Method[] methods=aClass. getDeclaredMethods();
for (Method method:methods) {
Class<?> returnType=method.getReturnType();
Type genericReturnType=method. getGenericReturnType();
System. out. println (returnType+" ,"+genericReturnType);
}
}
}
class MyClass1{
//基本数据类型的返回值
public int getInt() {
return 1 ;
}
//包装类型的返回值
public Boolean getBoolean() {
return Boolean.TRUE;
}
//引用数据类型的返回值
public String getString() {
return "xxx";
}
//泛型数据类型的返回值、
public List<String> getList() {
return new ArrayList<>();
}
//泛型数据类型的返回值
public Map<String, Object> getMap() {
return new HashMap<>() ;
}
}
运行结果:
3.3 Method中操作修饰符
Method类-关于方法返回值的操作
int getModifiers():
得到方法前面所定义的修饰符。
返回类、接口、变量、方法等以整数编码的Java语言修饰符,访问修饰符编码列表。Modifier类一共提供了12中修饰符的编码表示常量
public static final int PUBLIC= 0x00000001;
public static final int PRIVATE= 0x00000002;
public static final int PROTECTED= 0x00000004;
public static final int STATIC= 0x00000008;
public static final int FINAL= 0x00000010;
public static final int SYNCHRONIZED = 0x00000020;
public static final int VOLATILE= 0x00000040;
public static final int TRANSIENT= 0x00000080;
puhlic static final int NATIVE= 0x00000100;
pubtic static final int INTERFACE= 0x00000200;
public static final int ABSTRACT= 0x00000400;
public static final int STRICT= 0x00000800;
代码举例:
public class MethodReflect3 {
public static void main(String[] args) {
Class<? extends MyClass3> aClass =MyClass3. class;
Method[] methods=aClass. getDeclaredMethods() ;
for (Method method : methods) {
System. out. println("当前方法名称是:"+method.getName());
int i=method. getModifiers();
System. out. println(" modifier的值是: "+i) ;
System. out. println("私有访问权限?:"+Modifier.isPrivate(i)) ;
System. out. println("受保护的访问权限?: "+Modifier.isProtected(i));
System. out. println("公有访问权限?: " +Modifier.isPublic(i));
System. out. println("包含有static关键字?: "+Modifier.isStatic(i));
System.out.println("------------------------");
}
}
}
class MyClass3{
int getInt() {
return 1 ;
}
private static Boolean getBoolean() {
return Boolean.TRUE;
}
protected String getString() {
return "XXX" ;
}
public Map<String, Object> getMap() throws RuntimeException{
return new HashMap<> () ;
}
}
运行结果:
总结:
java.lang.reflcet.Modifier类提供解码类和成员访问修饰符的静态方法和常量,修饰符集合被表示为具有表示不同的修饰符的不同位置的整数。
java.lang.reflect.Modifier类在Java反射中起着非常重要的作用。它是Java反射API的一部分,主要用于提供有关类或成员变量(如字段、方法、构造器等)的访问修饰符的信息。
具体来说,Modifier类提供了以下功能:
(1)测试修饰符:它提供了一系列静态方法,如isPublic(), isPrivate(), isProtected(), isStatic(), isFinal()等,用于检查给定的整数修饰符位集中是否包含特定的修饰符。
(2)获取修饰符:Modifier类还提供了toString()方法和toString(int mod)方法,用于将整数修饰符位集转换为相应的字符串表示形式。
(3)设置和清除修饰符:虽然Modifier类本身不直接提供设置或清除修饰符的方法,但它提供了一些常量,如PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL等,这些常量可以作为位掩码来设置或清除类成员的修饰符。
(4)访问控制:Modifier类还提供了require()方法,用于在运行时检查调用者是否有权访问指定的类成员。
(5)通过Modifier类,开发者可以在运行时动态地获取和检查类或其成员的访问修饰符,从而进行更灵活和强大的反射操作。
3.4 Method中操作抛出的异常
Method类-关于所抛出的异常
Class[] method.getExceptionTypes():
方法在定义的时候可以通过throws关键字声名抛出异常,一个方法可以抛出多个异常。
public class MethodReflect4 {
public static void main(String[] args) {
Class<? extends MyClass4> aClass =MyClass4. class;
Method[] methods=aClass. getDeclaredMethods () ;
for (Method method:methods) {
Class<?> clazz[]=method.getExceptionTypes() ;
System.out.println("当前的方法名称是:" +method. getName()+",所抛出的异常如下: ");
for (Class cla:clazz)
System.out.println(cla);
System.out.println("----------");
}
}
}
class MyClass4{
private static synchronized Boolean getBoolean() throws
NullPointerException, IllegalArgumentException,SQLException {
return Boolean.TRUE;
}
public Map<String, Object> getMap() throws RuntimeException {
return new HashMap<>() ;
}
}
运行结果:
总结:
java.lang.reflect.Method
类的 getExceptionTypes()
方法用于获取一个方法可能抛出的所有异常类型的 Class
对象数组。当一个方法声明了在执行过程中可能会抛出的异常时,这些异常类型可以通过 getExceptionTypes()
方法获取。
这个方法返回一个 Class
对象数组,其中每个 Class
对象代表该方法可能抛出的一个异常类型。如果该方法不声明抛出任何异常(即它的异常列表为空),则此方法返回一个长度为零的数组。