Example #1
0
  public static void invokeSetMethodCaseInsensitive(Object obj, String prop, String value)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String alternateMethodName = null;
    Class cl = obj.getClass();

    String setMeth = "set" + prop;

    Method[] methodsList = cl.getMethods();
    boolean methodFound = false;
    int i = 0;
    for (i = 0; i < methodsList.length; ++i) {
      if (methodsList[i].getName().equalsIgnoreCase(setMeth) == true) {
        Class[] parameterTypes = methodsList[i].getParameterTypes();
        if (parameterTypes.length == 1) {
          if (parameterTypes[0].getName().equals("java.lang.String")) {
            methodFound = true;
            break;
          } else alternateMethodName = methodsList[i].getName();
        }
      }
    }
    if (methodFound == true) {
      Object[] params = {value};
      methodsList[i].invoke(obj, params);
      return;
    }
    if (alternateMethodName != null) {
      try {
        // try int method
        Class[] cldef = {Integer.TYPE};
        Method meth = cl.getMethod(alternateMethodName, cldef);
        Object[] params = {Integer.valueOf(value)};
        meth.invoke(obj, params);
        return;
      } catch (NoSuchMethodException nsmex) {
        // try boolean method
        Class[] cldef = {Boolean.TYPE};
        Method meth = cl.getMethod(alternateMethodName, cldef);
        Object[] params = {Boolean.valueOf(value)};
        meth.invoke(obj, params);
        return;
      }

    } else throw new NoSuchMethodException(setMeth);
  }
Example #2
0
  public static void invokeSetMethod(Object obj, String prop, String value)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Class cl = obj.getClass();
    // change first letter to uppercase
    String setMeth = "set" + prop.substring(0, 1).toUpperCase() + prop.substring(1);

    // try string method
    try {
      Class[] cldef = {String.class};
      Method meth = cl.getMethod(setMeth, cldef);
      Object[] params = {value};
      meth.invoke(obj, params);
      return;
    } catch (NoSuchMethodException ex) {
      try {
        // try int method
        Class[] cldef = {Integer.TYPE};
        Method meth = cl.getMethod(setMeth, cldef);
        Object[] params = {Integer.valueOf(value)};
        meth.invoke(obj, params);
        return;
      } catch (NoSuchMethodException nsmex) {
        // try boolean method
        Class[] cldef = {Boolean.TYPE};
        Method meth = cl.getMethod(setMeth, cldef);
        Object[] params = {Boolean.valueOf(value)};
        meth.invoke(obj, params);
        return;
      }
    }
  }
Example #3
0
  /** Verify and invoke main if present in the specified class. */
  public static void invokeApplicationMain(Class mainClass, String[] args)
      throws InvocationTargetException, IllegalAccessException, ClassNotFoundException {
    String err = localStrings.getLocalString("utility.no.main", "", new Object[] {mainClass});

    // determine the main method using reflection
    // verify that it is public static void and takes
    // String[] as the only argument
    Method mainMethod = null;
    try {
      mainMethod = mainClass.getMethod("main", new Class[] {String[].class});
    } catch (NoSuchMethodException msme) {
      _logger.log(Level.SEVERE, "enterprise_util.excep_in_utility", msme);
      throw new ClassNotFoundException(err);
    }

    // check modifiers: public static
    int modifiers = mainMethod.getModifiers();
    if (!Modifier.isPublic(modifiers) || !Modifier.isStatic(modifiers)) {
      err =
          localStrings.getLocalString(
              "utility.main.notpublicorstatic",
              "The main method is either not public or not static");
      _logger.log(Level.SEVERE, "enterprise_util.excep_in_utility_main.notpublicorstatic");
      throw new ClassNotFoundException(err);
    }

    // check return type and exceptions
    if (!mainMethod.getReturnType().equals(Void.TYPE)) {
      err =
          localStrings.getLocalString(
              "utility.main.notvoid", "The main method's return type is not void ");
      _logger.log(Level.SEVERE, "enterprise_util.excep_in_utility_main.notvoid");
      throw new ClassNotFoundException(err);
    }

    // build args to the main and call it
    Object params[] = new Object[1];
    params[0] = args;
    mainMethod.invoke(null, params);
  }