diff --git a/src/main/java/com/example/springshell/utils/Util.java b/src/main/java/com/example/springshell/utils/Util.java new file mode 100644 index 0000000..63a0578 --- /dev/null +++ b/src/main/java/com/example/springshell/utils/Util.java @@ -0,0 +1,40 @@ +package com.example.springshell.utils; + +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.servlet.DispatcherServlet; + +import java.lang.reflect.Field; + +public class Util { + + public static Object getFieldValue(Object obj, String fieldName) { + try { + Field field = obj.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(obj); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static Field getField(Class clazz, String fieldName) { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return field; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public DispatcherServlet getServlet() { + try { + WebApplicationContext context = (WebApplicationContext) RequestContextHolder.currentRequestAttributes() + .getAttribute("org.springframework.web.servlet.DispatcherServlet.CONTEXT", 0); + return (DispatcherServlet) context.getBean("dispatcherServlet"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +}