-
用反射的方式获取父类中的所有属性和方法
- package com.syh.jdbc.reflection_super;
-
-
- public class Parent {
-
- public String publicField = "1";
-
- String defaultField = "2";
-
- protected String protectedField = "3";
-
- private String privateField = "4" ;
-
- public void publicMethod() {
- System.out.println("publicMethod...");
- }
-
- void defaultMethod() {
- System.out.println("defaultMethod...");
- }
-
- protected void protectedMethod() {
- System.out.println("protectedMethod...");
- }
-
- private void privateMethod() {
- System.out.println("privateMethod...");
- }
-
- }
- package com.syh.jdbc.reflection_super;
-
-
- public class Son extends Parent{
-
- }
- package com.syh.jdbc.reflection_super;
-
- import java.lang.reflect.Field;
-
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
-
-
- public class ReflectionUtils {
-
-
-
- public static Method getDeclaredMethod(Object object, String methodName, Class<?> ... parameterTypes){
- Method method = null ;
-
- for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
- try {
- method = clazz.getDeclaredMethod(methodName, parameterTypes) ;
- return method ;
- } catch (Exception e) {
-
-
- }
- }
-
- return null;
- }
-
-
-
- public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
- Object [] parameters) {
-
- Method method = getDeclaredMethod(object, methodName, parameterTypes) ;
-
-
- method.setAccessible(true) ;
-
- try {
- if(null != method) {
-
-
- return method.invoke(object, parameters) ;
- }
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
-
- return null;
- }
-
-
-
- public static Field getDeclaredField(Object object, String fieldName){
- Field field = null ;
-
- Class<?> clazz = object.getClass() ;
-
- for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
- try {
- field = clazz.getDeclaredField(fieldName) ;
- return field ;
- } catch (Exception e) {
-
-
- }
- }
-
- return null;
- }
-
-
-
- public static void setFieldValue(Object object, String fieldName, Object value){
-
-
- Field field = getDeclaredField(object, fieldName) ;
-
-
- field.setAccessible(true) ;
-
- try {
-
- field.set(object, value) ;
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
-
- }
-
-
-
- public static Object getFieldValue(Object object, String fieldName){
-
-
- Field field = getDeclaredField(object, fieldName) ;
-
-
- field.setAccessible(true) ;
-
- try {
-
- return field.get(object) ;
-
- } catch(Exception e) {
- e.printStackTrace() ;
- }
-
- return null;
- }
- }
- package com.syh.jdbc.reflection_super;
-
- import static org.junit.Assert.*;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
-
- import org.junit.Test;
-
-
- public class ReflectionUtilsTest {
-
-
-
- @Test
- public void testGetDeclaredMethod() {
-
- Object obj = new Son() ;
-
-
- System.out.println(publicMethod.getName());
-
-
- System.out.println(defaultMethod.getName());
-
-
- System.out.println(protectedMethod.getName());
-
-
- System.out.println(privateMethod.getName());
- }
-
-
-
- @Test
- public void testInvokeMethod() throws Exception {
- Object obj = new Son() ;
-
-
-
-
-
-
-
-
- }
-
-
-
- @Test
- public void testGetDeclaredField() {
-
- Object obj = new Son() ;
-
-
- System.out.println(publicField.getName());
-
-
- System.out.println(defaultField.getName());
-
-
- System.out.println(protectedField.getName());
-
-
- System.out.println(privateField.getName());
-
- }
-
- @Test
- public void testSetFieldValue() {
-
- Object obj = new Son() ;
-
- System.out.println("原来的各个属性的值: ");
- System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
- System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
- System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
- System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
-
- ReflectionUtils.setFieldValue(obj, "publicField", "a") ;
- ReflectionUtils.setFieldValue(obj, "defaultField", "b") ;
- ReflectionUtils.setFieldValue(obj, "protectedField", "c") ;
- ReflectionUtils.setFieldValue(obj, "privateField", "d") ;
-
- System.out.println("***********************************************************");
-
- System.out.println("将属性值改变后的各个属性值: ");
- System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
- System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
- System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
- System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
-
- }
-
- @Test
- public void testGetFieldValue() {
-
- Object obj = new Son() ;
-
- System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
- System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
- System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
- System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
- }
-
- }
-
相关阅读:
Linux性能优化之CPU优化(一)
MongoDB CPU使用较高,如何排查?
MongoDB 安全配置
open-falcon v0.2 监控部署记录
有关redis相关的性能优化及内存说明
kafka 基础知识梳理
Java 进程占用 VIRT 虚拟内存超高的问题研究
【nodejs】文件上传demo实现
translate和position的比较
setAttribute()、getAttribute()与ele[attr]与自定义属性
-
原文地址:https://www.cnblogs.com/toSeeMyDream/p/5680007.html
Copyright © 2020-2023
润新知