Example #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");
   }
 }
  /**
   * Create a class loader based on the SimpleLoader
   *
   * @param parent parent class loader
   * @param path traditional classpath
   * @return the new ClassLoader
   */
  public static DynamicClassLoader create(ClassLoader parent, Path path) {
    DynamicClassLoader loader = new DynamicClassLoader(parent, false);

    loader.addLoader(new SimpleLoader(loader, path));

    loader.init();

    return loader;
  }
  /**
   * Create a class loader based on the SimpleLoader
   *
   * @param parent parent class loader
   * @param path traditional classpath
   * @param prefix the class prefix restriction
   * @return the new ClassLoader
   */
  public static DynamicClassLoader create(ClassLoader parent, Path path, String prefix) {
    DynamicClassLoader loader = new DynamicClassLoader(parent, false);

    SimpleLoader simpleLoader = new SimpleLoader(loader, path, prefix);
    simpleLoader.init();

    loader.addLoader(simpleLoader);

    loader.init();

    return loader;
  }
  public DependencyContainer(ClassLoader loader) {
    _classLoaderRef = new WeakReference<ClassLoader>(loader);

    _checkInterval = DynamicClassLoader.getGlobalDependencyCheckInterval();

    for (; loader != null; loader = loader.getParent()) {
      if (loader instanceof DynamicClassLoader) {
        _checkInterval = ((DynamicClassLoader) loader).getDependencyCheckInterval();
        break;
      }
    }
  }
Example #5
0
  /**
   * 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 + "\")");
    }
  }
  /** Calculates a MD5 digest of the class. */
  public String getDigest() {
    try {
      if (_className == null || "".equals(_className)) return "";

      DynamicClassLoader loader =
          (DynamicClassLoader) Thread.currentThread().getContextClassLoader();

      ClassLoader tmpLoader = loader.getNewTempClassLoader();

      Class cl = Class.forName(_className, false, tmpLoader);

      if (cl == null) return "";

      MessageDigest digest = MessageDigest.getInstance("MD5");

      addDigest(digest, cl.getName());

      addDigest(digest, cl.getModifiers());

      Class superClass = cl.getSuperclass();
      if (superClass != null) addDigest(digest, superClass.getName());

      Class[] interfaces = cl.getInterfaces();
      for (int i = 0; i < interfaces.length; i++) addDigest(digest, interfaces[i].getName());

      Field[] fields = cl.getDeclaredFields();

      Arrays.sort(fields, new FieldComparator());

      if (_checkFields) {
        for (Field field : fields) {
          if (Modifier.isPrivate(field.getModifiers()) && !_checkPrivate) continue;
          if (Modifier.isProtected(field.getModifiers()) && !_checkProtected) continue;

          addDigest(digest, field.getName());
          addDigest(digest, field.getModifiers());
          addDigest(digest, field.getType().getName());

          addDigest(digest, field.getAnnotations());
        }
      }

      Method[] methods = cl.getDeclaredMethods();
      Arrays.sort(methods, new MethodComparator());

      for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];

        if (Modifier.isPrivate(method.getModifiers()) && !_checkPrivate) continue;
        if (Modifier.isProtected(method.getModifiers()) && !_checkProtected) continue;
        if (Modifier.isStatic(method.getModifiers()) && !_checkStatic) continue;

        addDigest(digest, method.getName());
        addDigest(digest, method.getModifiers());
        addDigest(digest, method.getName());

        Class[] param = method.getParameterTypes();
        for (int j = 0; j < param.length; j++) addDigest(digest, param[j].getName());

        addDigest(digest, method.getReturnType().getName());

        Class[] exn = method.getExceptionTypes();
        for (int j = 0; j < exn.length; j++) addDigest(digest, exn[j].getName());

        addDigest(digest, method.getAnnotations());
      }

      byte[] digestBytes = new byte[256];

      int len = digest.digest(digestBytes, 0, digestBytes.length);

      return digestToBase64(digestBytes, len);
    } catch (Exception e) {
      log.log(Level.FINER, e.toString(), e);

      return "";
    }
  }