Beispiel #1
0
 void loadRobot() {
   // Creates new ClassLoader for whenever the class needs to be reloaded.
   ClassLoader parentClassLoader = DynamicClassLoader.class.getClassLoader();
   DynamicClassLoader newClassLoader = new DynamicClassLoader(parentClassLoader);
   String classFile =
       String.format(System.getProperty("user.dir") + "/" + robotName + "Robot.class");
   // A new instance is created of the main Robot class in order to start the normal and hear
   // threads
   try {
     Class<? extends SyncRobot> sr = newClassLoader.loadClass(robotName + "Robot", classFile);
     classFile =
         String.format(
             System.getProperty("user.dir")
                 + "/"
                 + robotName
                 + "Robot$"
                 + robotName
                 + "HearThread.class");
     newClassLoader.loadClass(robotName + "Robot$" + robotName + "HearThread", classFile);
     sr.newInstance().StartThreads();
   } catch (Exception e) {
     e.printStackTrace();
     System.out.println("Failed to load Robot class");
   }
 }
  /**
   * Gets information on the specified class. Information is returned as a list of lists that is
   * printed to System.out.
   *
   * @param className a <code>String</code> value
   * @param level access level i.e. public, protected, default, and private
   */
  public static void getClassInfo(String className, int level) {
    try {
      DynamicClassLoader dcl = new DynamicClassLoader();
      Class c = dcl.loadClass(className);
      if (c != null) {
        StringBuffer sb = new StringBuffer(3000);
        sb.append(START_LIST);
        sb.append(NL);
        listClassInfo(c, level, sb);
        sb.append(END_PAREN);
        sb.append(NL);

        Writer out = new BufferedWriter(new OutputStreamWriter(System.out));
        try {
          out.write(sb.toString());
          out.flush();
        } catch (IOException e) {
        }
      }
    } catch (ClassNotFoundException e) {
      System.out.println(NIL);
    } catch (Exception e) {
      System.out.println(
          "(error \"Trying to load " + className + " caused a Java exception: " + e + "\")");
    } catch (NoClassDefFoundError e) {
      System.out.println(NIL);
    } catch (UnsatisfiedLinkError e) {
      // This occurs with classes that have native methods whose native
      // implementations cannot be found.
      System.out.println(
          "(error \"Trying to load "
              + className
              + " caused a Java UnsatisfiedLinkError: "
              + e
              + "\")");
    } catch (LinkageError e) {
      System.out.println(
          "(error \"Trying to load " + className + " caused a Java LinkageError: " + e + "\")");
    }
  }