Java Class.getDeclaredMethod Examples

Java Class.getDeclaredMethod - 30 examples found. These are the top rated real world Java examples of Class.getDeclaredMethod extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 public String getUserNameFull() {
   String s = "Это Линокс";
   if (isOSWindows()) {
     String className = "com.sun.security.auth.module.NTSystem";
     try {
       ClassLoader cL = ClassLoader.getSystemClassLoader();
       Class cls = cL.loadClass(className);
       Object obj = cls.newInstance();
       Method methodGetName = cls.getDeclaredMethod("getName");
       s = (String) methodGetName.invoke(obj);
       Method methodGetDomain = cls.getDeclaredMethod("getDomain");
       s = (String) methodGetDomain.invoke(obj) + "\\" + s;
     } catch (ClassNotFoundException e) {
       return "undefined";
     } catch (InstantiationException e) {
       return "undefined";
     } catch (IllegalAccessException e) {
       return "undefined";
     } catch (NoSuchMethodException e) {
       return "undefined";
     } catch (InvocationTargetException e) {
       return "undefined";
     }
   }
   return s;
 }
  public BaseClusterReceiver(ExecutorService executorService) {
    if (executorService == null) {
      throw new NullPointerException("Executor service is null");
    }

    _executorService = executorService;

    boolean hasDoViewAccepted = false;

    Class<?> clazz = getClass();

    try {
      clazz.getDeclaredMethod("doAddressesUpdated", List.class, List.class);

      hasDoViewAccepted = true;
    } catch (ReflectiveOperationException roe) {
    }

    _hasDoViewAccepted = hasDoViewAccepted;

    boolean hasDoCoordinatorAddressUpdated = false;

    try {
      clazz.getDeclaredMethod("doCoordinatorAddressUpdated", Address.class, Address.class);

      hasDoCoordinatorAddressUpdated = true;
    } catch (ReflectiveOperationException roe) {
    }

    _hasDoCoordinatorAddressUpdated = hasDoCoordinatorAddressUpdated;
  }
Example #3
0
  /**
   * 将查询结构设置给对应的域(一对多)
   *
   * @param list
   * @param majorMethod
   * @param fieldName
   * @param childEntities
   * @param childClass
   * @param foreignKey
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  private void setListValue(
      List<T> list,
      Method majorMethod,
      String fieldName,
      List<Entity> childEntities,
      Class<Entity> childClass,
      String foreignKey)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Map<Object, List<Entity>> childListMap = new HashMap<Object, List<Entity>>();

    for (Entity childEntity : childEntities) {
      Method foreignKeyMethod =
          childClass.getDeclaredMethod("get".concat(StringUtils.firstToUpperCase(foreignKey)));
      Object id = foreignKeyMethod.invoke(childEntity);

      List<Entity> childList = childListMap.get(id);
      if (childList == null) {
        childList = new ArrayList<Entity>();
        childListMap.put(id, childList);
      }
      childList.add(childEntity);
    }

    String listMethodName = "set".concat(StringUtils.firstToUpperCase(fieldName));
    for (T entity : list) {
      Object id = majorMethod.invoke(entity);
      List<Entity> childLis = childListMap.get(id);

      Method listMethod = entityClass.getDeclaredMethod(listMethodName, List.class);
      listMethod.invoke(entity, childLis);
    }
  }
Example #4
0
 /**
  * Launch a browser window to show the given url
  *
  * @param inUrl url to show
  */
 public static void launchBrowser(String inUrl) {
   // First choice is to try the Desktop library from java 6, if available
   try {
     Class<?> d = Class.forName("java.awt.Desktop");
     d.getDeclaredMethod("browse", new Class[] {URI.class})
         .invoke(d.getDeclaredMethod("getDesktop").invoke(null), new Object[] {URI.create(inUrl)});
     // above code mimics: Desktop.getDesktop().browse(URI.create(inUrl));
   } catch (Exception ignore) {
     // The Desktop call failed, need to try backup methods
     if (!_initialised) {
       init();
     }
     if (_browserCommand == null) {
       JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
     } else {
       try {
         // enclose url in quotes if necessary
         String url = inUrl;
         if (_urlNeedsQuotes) {
           url = "\"" + url + "\"";
         }
         // Fill in url in last element of command array
         _browserCommand[_browserCommand.length - 1] = url;
         // execute command to launch browser
         Runtime.getRuntime().exec(_browserCommand);
       } catch (Exception e) {
         JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);
       }
     }
   }
 }
 /**
  * Opens the specified web page in the user's default browser
  *
  * @param url A web address (URL) of a web page (ex: "http://www.google.com/")
  */
 public static void openURL(String url) {
   try { // attempt to use Desktop library from JDK 1.6+ (even if on 1.5)
     Class<?> d = Class.forName("java.awt.Desktop");
     d.getDeclaredMethod("browse", new Class[] {java.net.URI.class})
         .invoke(
             d.getDeclaredMethod("getDesktop").invoke(null),
             new Object[] {java.net.URI.create(url)});
     // above code mimics:
     //   java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
   } catch (Exception ignore) { // library not available or failed
     String osName = System.getProperty("os.name");
     try {
       if (osName.startsWith("Mac OS")) {
         Class.forName("com.apple.eio.FileManager")
             .getDeclaredMethod("openURL", new Class[] {String.class})
             .invoke(null, new Object[] {url});
       } else if (osName.startsWith("Windows"))
         Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
       else { // assume Unix or Linux
         boolean found = false;
         for (String browser : browsers)
           if (!found) {
             found = Runtime.getRuntime().exec(new String[] {"which", browser}).waitFor() == 0;
             if (found) Runtime.getRuntime().exec(new String[] {browser, url});
           }
         if (!found) throw new Exception(Arrays.toString(browsers));
       }
     } catch (Exception e) {
       JOptionPane.showMessageDialog(null, errMsg + "\n" + e.toString());
     }
   }
 }
Example #6
0
  /**
   * 将查询结构设置给对应的域(一对一/多对一)
   *
   * @param list
   * @param keyMethod
   * @param fieldName
   * @param childEntities
   * @param childClass
   * @param fileType
   * @param refKey
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  private void setSingleValue(
      List<T> list,
      Method keyMethod,
      String fieldName,
      List<Entity> childEntities,
      Class<Entity> childClass,
      Class<?> fileType,
      String refKey)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Map<Object, Entity> childMap = new HashMap<Object, Entity>();

    for (Entity childEntity : childEntities) {
      Method refKeyMethod =
          childClass.getDeclaredMethod("get".concat(StringUtils.firstToUpperCase(refKey)));
      Object id = refKeyMethod.invoke(childEntity);

      childMap.put(id, childEntity);
    }

    String fieldMethodName = "set".concat(StringUtils.firstToUpperCase(fieldName));
    for (T entity : list) {
      Object id = keyMethod.invoke(entity);
      Entity child = childMap.get(id);

      Method fieldMethod = entityClass.getDeclaredMethod(fieldMethodName, fileType);
      fieldMethod.invoke(entity, child);
    }
  }