示例#1
0
  /**
   * Override this method to create you own classes on the fly. The name contains the special token
   * $$BCEL$$. Everything before that token is consddered to be a package name. You can encode you
   * own arguments into the subsequent string. You must regard however not to use any "illegal"
   * characters, i.e., characters that may not appear in a Java class name too<br>
   * The default implementation interprets the string as a encoded compressed Java class, unpacks
   * and decodes it with the Utility.decode() method, and parses the resulting byte array and
   * returns the resulting JavaClass object.
   *
   * @param class_name compressed byte code with "$$BCEL$$" in it
   */
  protected JavaClass createClass(String class_name) {
    int index = class_name.indexOf("$$BCEL$$");
    String real_name = class_name.substring(index + 8);

    JavaClass clazz = null;
    try {
      byte[] bytes = Utility.decode(real_name, true);
      ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");

      clazz = parser.parse();
    } catch (Throwable e) {
      e.printStackTrace();
      return null;
    }

    // Adapt the class name to the passed value
    ConstantPool cp = clazz.getConstantPool();

    ConstantClass cl =
        (ConstantClass) cp.getConstant(clazz.getClassNameIndex(), Constants.CONSTANT_Class);
    ConstantUtf8 name = (ConstantUtf8) cp.getConstant(cl.getNameIndex(), Constants.CONSTANT_Utf8);
    name.setBytes(class_name.replace('.', '/'));

    return clazz;
  }
  private DbJVPackage importJarFile(
      File jarFile,
      DbJVPackage topMostPackage,
      Controller controller,
      int startJobDone,
      int endJobDone)
      throws DbException {
    DbJVClass dbClass;
    String filename = jarFile.getName();

    try {
      ZipFile zip = new ZipFile(jarFile);
      int i = 0, nb = zip.size();
      int span = endJobDone - startJobDone;

      for (Enumeration<?> e = zip.entries(); e.hasMoreElements(); ) {
        int jobDone = startJobDone + (i * span) / nb;

        ZipEntry entry = (ZipEntry) e.nextElement();
        String entryName = entry.getName();
        int idx = entryName.lastIndexOf('.');
        String ext = (idx == -1) ? null : entryName.substring(idx + 1);
        if ("class".equals(ext)) {
          InputStream is = zip.getInputStream(entry);

          ClassParser parser = new ClassParser(is, filename);
          JavaClass claz = parser.parse();
          dbClass = importClass(claz, controller);

          if (dbClass != null) {
            DbJVPackage pack = (DbJVPackage) dbClass.getCompositeOfType(DbJVPackage.metaClass);
            topMostPackage = findTopMostPackage(topMostPackage, pack);
            addToImportedPackage(pack);
          } // end if
        } // end if

        // check job done
        controller.checkPoint(jobDone);
        i++;

        // stop to reverse engineer if user has cancelled
        boolean finished = controller.isFinalState();
        if (finished) {
          break;
        }
      } // end for

      zip.close();

    } catch (IOException ex) {
      controller.println(ex.toString());
      dbClass = null;
    }

    return topMostPackage;
  } // end importClassFile()
  private DbJVClass importClassFile(String filename, Controller controller) throws DbException {
    DbJVClass dbClass;

    try {
      ClassParser parser = new ClassParser(filename);
      JavaClass claz = parser.parse();
      dbClass = importClass(claz, controller);

    } catch (IOException ex) {
      controller.println(ex.toString());
      dbClass = null;
    }

    return dbClass;
  } // end importClassFile()