java反射机制——getMethod/getField与getDeclaredMethod/getDeclaredField区别

本文探讨了Java反射中的getMethod和getDeclaredMethod的区别,重点在于它们对访问权限的处理。通过测试代码展示了即使在父类中,只有public方法和字段才能被getMethod获取,而getDeclaredMethod则能获取到类声明的所有方法,包括非public的。
摘要由CSDN通过智能技术生成

测试代码:

package com.jason.corejava.reflex;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflexMain {
	
	public static void main(String[] args) throws IOException{
		Log log = Log.createLog("C:\\Users\\kdhfkd\\logText.txt");
		
		log.print("---------now is getMethod and getField---------");
		
		Class<?> classA = ObjectA.class;
		try {
			Object objectA = classA.newInstance();
			log.print("objectA created\r\n");
			
			try {
				Method setFieldA = classA.getMethod("setFieldA", String.class);
				setFieldA.invoke(objectA, "getMethod");
				log.print("setFieldA ok\r\n");
				Method getFieldA = classA.getMethod("getFieldA");
				String result = (String) getFieldA.invoke(objectA);
				log.print("getFieldA ok result = " + result + "\r\n");
				
			} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldA get/set err:"+e.toString()+"\r\n");
			}
			
			try {
				Method setFieldB = classA.getMethod("setFieldB", String.class);
				setFieldB.invoke(objectA, "getMethod");
				log.print("setFieldB ok\r\n");
				Method getFieldB = classA.getMethod("getFieldB");
				String result = (String) getFieldB.invoke(objectA);
				log.print("getFieldB ok result = " + result + "\r\n");
			} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldB get/set err:"+e.toString()+"\r\n");
			}
			
			try {
				Method setFieldC = classA.getMethod("setFieldC", String.class);
				setFieldC.invoke(objectA, "getMethod");
				log.print("setFieldC ok\r\n");
				Method getFieldC = classA.getMethod("getFieldC");
				String result = (String) getFieldC.invoke(objectA);
				log.print("getFieldC ok result = " + result + "\r\n");
			} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldC get/set err:"+e.toString()+"\r\n");
			}
			
			try {
				Field fieldA = classA.getField("fieldA");
				String result = (String) fieldA.get(objectA);
				log.print("fieldA = " + result + "\r\n");
			} catch (NoSuchFieldException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldA get fail:" + e.toString() + "\r\n");
			}
			
			try {
				Field fieldB = classA.getField("fieldB");
				String result = (String) fieldB.get(objectA);
				log.print("fieldB = " + result + "\r\n");
			} catch (NoSuchFieldException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldB get fail:" + e.toString() + "\r\n");
			}
			
			try {
				Field fieldC = classA.getField("fieldC");
				String result = (String) fieldC.get(objectA);
				log.print("fieldC = " + result + "\r\n");
			} catch (NoSuchFieldException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldC get fail:" + e.toString() + "\r\n");
			}
			
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.print("objectA err:"+e.toString()+"\r\n");
		}
		
		Class<?> classB = ObjectB.class;
		try {
			Object objectB = classB.newInstance();
			log.print("objectB created\r\n");
			
			try {
				Method setFieldA = classB.getMethod("setFieldA", String.class);
				setFieldA.invoke(objectB, "getMethod");
				log.print("setFieldA ok\r\n");
				Method getFieldA = classB.getMethod("getFieldA");
				String result = (String) getFieldA.invoke(objectB);
				log.print("getFieldA ok result = " + result + "\r\n");
				
			} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldA get/set err:"+e.toString()+"\r\n");
			}
			
			try {
				Method setFieldB = classB.getMethod("setFieldB", String.class);
				setFieldB.invoke(objectB, "getMethod");
				log.print("setFieldB ok\r\n");
				Method getFieldB = classB.getMethod("getFieldB");
				String result = (String) getFieldB.invoke(objectB);
				log.print("getFieldB ok result = " + result + "\r\n");
			} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldB get/set err:"+e.toString()+"\r\n");
			}
			
			try {
				Method setFieldC = classB.getMethod("setFieldC", String.class);
				setFieldC.invoke(objectB, "getMethod");
				log.print("setFieldC ok\r\n");
				Method getFieldC = classB.getMethod("getFieldC");
				String result = (String) getFieldC.invoke(objectB);
				log.print("getFieldC ok result = " + result + "\r\n");
			} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.print("fieldC get/set err:"+e.toString()+"\r\n");
			}
			
			try {
				Method setFieldD = classB.getMethod("setFieldD", String.class);
				setFieldD.invoke(objectB, "getMethod");
				log.print("setFieldD ok\r\n");
				Method getFieldD = classB.getMethod("getFieldD");
				String result = (String) getFieldD.invoke(objectB);
				log.print("getFieldD ok result = " + result + "\r\n");
			} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值