Java Class.getDeclaredMethods Examples

Java Class.getDeclaredMethods - 30 examples found. These are the top rated real world Java examples of Class.getDeclaredMethods extracted from open source projects. You can rate examples to help us improve the quality of examples.
  /**
   * Return the Method object for the given methodName and parameterTypes on the given objClass.
   *
   * @param objClass
   * @param methodName
   * @param parameterTypes
   * @return
   */
  public static Method findMethod(Class objClass, String methodName, Class... parameterTypes) {

    LOG.debug(
        "Looking for method "
            + methodName
            + "("
            + formatClassNames(parameterTypes)
            + ") on "
            + objClass.getName()
            + ".");
    Method result = null;

    Method[] methods = objClass.getDeclaredMethods();
    for (Method m : methods) {

      if (m.getName().compareToIgnoreCase(methodName) == 0) {

        Class[] methodParameterTypes = m.getParameterTypes();
        if (parametersMatch(methodParameterTypes, parameterTypes)) {

          result = m;
          break;
        }
      }
    }

    // Handle collections
    if (result == null) {

      methods = objClass.getDeclaredMethods();
      for (Method m : methods) {

        if (m.getName().compareToIgnoreCase(methodName + "s") == 0) {

          Class[] methodParameterTypes = m.getParameterTypes();
          if (parametersMatch(methodParameterTypes, parameterTypes)) {

            result = m;
            break;
          }
        }
      }
    }

    if (result == null) {
      LOG.debug(
          "Method "
              + methodName
              + "("
              + formatClassNames(parameterTypes)
              + ") on "
              + objClass.getName()
              + " not found.");
    }
    return result;
  }
Example #2
0
  /**
   * Register.
   *
   * @param clazz the clazz
   */
  public static void register(Class<?> clazz) {
    Library annotatedLibrary = clazz.getAnnotation(Library.class);
    if (annotatedLibrary != null) {
      register(annotatedLibrary);
      registerNativeMethods(annotatedLibrary.defaultAPI(), clazz.getDeclaredMethods());
    } else {
      registerNativeMethods(Library.API, clazz.getDeclaredMethods());
    }

    invokeStaticInitializerOnClass(clazz);
  }
 // ===================================================================================
 //                                                                       Set up Method
 //                                                                       =============
 @Override
 protected void setupMethod(S2ActionMapping actionMapping, Class<?> actionClass) {
   // copied from super and adjust a little bit
   S2ExecuteConfig allSelectedExecuteConfig = null;
   for (Class<?> clazz = actionClass; clazz != Object.class; clazz = clazz.getSuperclass()) {
     for (Method method : clazz.getDeclaredMethods()) {
       if (isOutOfTargetMethod(actionMapping, actionClass, method)) {
         continue;
       }
       checkActionMethod(actionClass, method);
       checkDuplicateExecute(actionMapping, actionClass, method);
       final S2ExecuteConfig executeConfig = createExecuteConfig();
       setupExecuteConfig(executeConfig, actionMapping, actionClass, method);
       if (executeConfig.isUrlPatternAllSelected()) {
         if (allSelectedExecuteConfig != null) {
           throw new MultipleAllSelectedUrlPatternRuntimeException(
               allSelectedExecuteConfig.getUrlPattern(), executeConfig.getUrlPattern());
         }
         allSelectedExecuteConfig = executeConfig;
       } else {
         actionMapping.addExecuteConfig(executeConfig);
       }
     }
   }
   registerAllSelectedExecuteConfig(actionMapping, allSelectedExecuteConfig);
   checkExecuteConfigSize(actionMapping, actionClass);
 }
Example #4
0
 public static Method findPreDestroyMethod(Class<?> c, String name) {
   if (Object.class == c || null == c) {
     return null;
   }
   for (Method m : c.getDeclaredMethods()) {
     if (name != null) {
       if (m.getName().equals(name)) {
         return m;
       }
     } else if (m.getAnnotation(PreDestroy.class) != null) {
       return m;
     }
   }
   Method m = findPreDestroyMethod(c.getSuperclass(), name);
   if (m != null) {
     return m;
   }
   for (Class<?> i : c.getInterfaces()) {
     m = findPreDestroyMethod(i, name);
     if (m != null) {
       return m;
     }
   }
   return null;
 }
Example #5
0
  private boolean winShellExecute(final String filePath, final String verb) throws Exception {
    final Class<?> clz = WinAPIWrapper.class;
    Method stringToByteArray = null, shellExecute = null;
    for (final Method m : clz.getDeclaredMethods()) {
      if (m.getName().equals("stringToByteArray")) {
        stringToByteArray = m;
        continue;
      } else if (m.getName().equals("shellExecute")) {
        shellExecute = m;
        continue;
      }
    }
    stringToByteArray.setAccessible(true);
    shellExecute.setAccessible(true);

    byte[] filePathBytes = (byte[]) stringToByteArray.invoke(null, filePath);
    byte[] verbBytes = (byte[]) stringToByteArray.invoke(null, verb);
    final Integer result = (Integer) shellExecute.invoke(null, filePathBytes, verbBytes);
    logger.debug(
        "ShellExecute(NULL, \""
            + verb
            + "\", \""
            + filePath
            + "\", NULL, NULL, SW_SHOWNORMAL); returned "
            + result);
    return (result != null && result.intValue() > 32);
  }
Example #6
0
  public static OptionSchema newOptionSchema(Class<?> optionHolderType) {
    OptionSchema optionSchema = new OptionSchema();

    // traverses through super classes
    for (Class<?> optionHolderClass = optionHolderType;
        optionHolderClass != null;
        optionHolderClass = optionHolderClass.getSuperclass()) {
      // looks for bean methods annotated with Option or Argument
      for (Method eachMethod : optionHolderClass.getDeclaredMethods()) {
        if (eachMethod.getAnnotation(Option.class) != null) optionSchema.addOptionItem(eachMethod);

        if (eachMethod.getAnnotation(Argument.class) != null)
          optionSchema.addArgumentItem(eachMethod);
      }

      // looks for bean fields annotated with Option or Argument
      for (Field f : optionHolderClass.getDeclaredFields()) {
        if (f.getAnnotation(Option.class) != null) optionSchema.addOptionItem(f);

        if (f.getAnnotation(Argument.class) != null) optionSchema.addArgumentItem(f);
      }

      if (optionHolderClass.getAnnotation(Usage.class) != null) {
        optionSchema.setUsage(optionHolderClass);
      }
    }

    return optionSchema;
  }