lundi 27 décembre 2021

How to get method arguments using reflection

This method is first checked by beanDefintirions for the original Beans (Configureablelistablebeanfactory beanFactory is used; ).

ConfigurableListableBeanFactory beanFactory; injected.

Then all the methods of the original bean, which was obtained from the BeanFactory, are iterated over. After searching over the methods of a certain annotation, we get the Bean from the Applicationcontext.

This Bean is a proxy wrapper over the original Bean, which was formed at the - > postProcessBeforeInitialization() stage. Now through this bean, I call a method that has been marked with the annotation I need, but it requires another argument Obj ..args.

How do I get the missing argument ?

Использую Srping 5.x, java 11

private void runMethodWithPostProxyThirdPhaseAnnotation(String beanName, ApplicationContext applicationContext) {

      BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);


      try {
          String originalBeanClassName = beanDefinition.getBeanClassName();

          if (originalBeanClassName != null) {
              Class<?> originalClass = Class.forName(originalBeanClassName);
              Method[] methods = originalClass.getMethods();

              for (Method method : methods) {
                  if (method.isAnnotationPresent(PostProxyThirdPhase.class)) {
                      

                      String originalMethodName = method.getName();
                      Class<?>[] parameterTypesFromOriginalMethod = method.getParameterTypes();

                      Object beanAfterProxy = applicationContext.getBean(beanName);

                      Method methodFromProxyBean = beanAfterProxy
                              .getClass()
                              .getMethod(originalMethodName, parameterTypesFromOriginalMethod);

                      methodFromProxyBean.invoke(beanAfterProxy, ?);
                  }
              }
          }

      } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
          e.printStackTrace();
      }

}




Aucun commentaire:

Enregistrer un commentaire