public static void main(String[] args) throws IntrospectionException {
    BeanInfo beanInfo = Introspector.getBeanInfo(User.class);

    System.out.println("==>MethodDescriptor");
    MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors();
    for (MethodDescriptor method : methodDescs) {
      System.out.println(method.getName());
      System.out.println(method.getDisplayName());
      System.out.println(method.getShortDescription());
      System.out.println(method.getValue("getName"));

      System.out.println("==>MethodDescriptor/ReflectionMethod");
      Method reflectMethod = method.getMethod();
      System.out.println(reflectMethod.getName());

      System.out.println("==>MethodDescriptor/ParameterDescriptor");
      ParameterDescriptor[] paramDescs = method.getParameterDescriptors();
      if (paramDescs != null) {
        for (ParameterDescriptor paramDesc : paramDescs) {
          System.out.println(paramDesc.getName());
          System.out.println(paramDesc.getDisplayName());
          System.out.println(paramDesc.getShortDescription());
          System.out.println(paramDesc.getValue("name"));
        }
      }
    }
  }
Example #2
0
 public static Method findSetter(Class<?> clazz, String name) throws IntrospectionException {
   BeanInfo info = Introspector.getBeanInfo(clazz);
   Method setter = null;
   for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
     if (name.equals(pd.getName())) {
       setter = pd.getWriteMethod();
       break;
     }
   }
   if (setter == null) {
     // sometimes this happens even when there is a setter. Don't know why.
     String expectedSetterName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
     for (MethodDescriptor md : info.getMethodDescriptors()) {
       if (md.getName().equals(expectedSetterName)) {
         setter = md.getMethod();
         break;
       }
     }
   }
   return setter;
 }