示例#1
0
  /** Generate output JAR-file and packages */
  public void outputToJar() throws IOException {
    // create the manifest
    final Manifest manifest = new Manifest();
    final java.util.jar.Attributes atrs = manifest.getMainAttributes();
    atrs.put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.2");

    final Map map = manifest.getEntries();
    // create manifest
    Enumeration classes = _bcelClasses.elements();
    final String now = (new Date()).toString();
    final java.util.jar.Attributes.Name dateAttr = new java.util.jar.Attributes.Name("Date");
    while (classes.hasMoreElements()) {
      final JavaClass clazz = (JavaClass) classes.nextElement();
      final String className = clazz.getClassName().replace('.', '/');
      final java.util.jar.Attributes attr = new java.util.jar.Attributes();
      attr.put(dateAttr, now);
      map.put(className + ".class", attr);
    }

    final File jarFile = new File(_destDir, _jarFileName);
    final JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);
    classes = _bcelClasses.elements();
    while (classes.hasMoreElements()) {
      final JavaClass clazz = (JavaClass) classes.nextElement();
      final String className = clazz.getClassName().replace('.', '/');
      jos.putNextEntry(new JarEntry(className + ".class"));
      final ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
      clazz.dump(out); // dump() closes it's output stream
      out.writeTo(jos);
    }
    jos.close();
  }
  private DbJVClass importClass(JavaClass claz, Controller controller) throws DbException {

    String packName = claz.getPackageName();
    String qualifiedName = claz.getClassName();
    int idx = qualifiedName.lastIndexOf('.');
    String classname = qualifiedName.substring(idx + 1);
    DbJVClass dbClaz = null;

    try {
      if (m_classModel != null) {
        DbJVPackage pack = findPackageByName(packName);

        // create class or interface
        int value = claz.isInterface() ? JVClassCategory.INTERFACE : JVClassCategory.CLASS;
        JVClassCategory catg = JVClassCategory.getInstance(value);
        dbClaz =
            (pack == null) ? new DbJVClass(this.m_classModel, catg) : new DbJVClass(pack, catg);
        dbClaz.setName(classname);

        // set class modifiers
        dbClaz.setAbstract(claz.isAbstract());
        dbClaz.setFinal(claz.isFinal());
        dbClaz.setStatic(claz.isStatic());
        dbClaz.setStrictfp(claz.isStrictfp());
        dbClaz.setVisibility(toVisibility(claz));

        // create inheritances
        importInheritances(dbClaz, claz);

        // create fields
        if (m_params.createFields) {
          Field[] fields = claz.getFields();
          for (Field field : fields) {
            importField(dbClaz, field);
          }
        }

        // create methods
        if (m_params.createMethods) {
          Method[] methods = claz.getMethods();
          for (Method method : methods) {
            importMethod(dbClaz, method);
          }
        }

        // keep user informed of progression
        String pattern = LocaleMgr.misc.getString("0SuccessfullyCreated");
        String msg = MessageFormat.format(pattern, qualifiedName);
        controller.println(msg);
      } // end if
    } catch (DbException ex) {
      controller.println(ex.toString());
    } // end try

    return dbClaz;
  } // end importClass()
示例#3
0
  public void dumpClass(JavaClass clazz) {

    if (_outputType == FILE_OUTPUT || _outputType == BYTEARRAY_AND_FILE_OUTPUT) {
      File outFile = getOutputFile(clazz.getClassName());
      String parentDir = outFile.getParent();
      if (parentDir != null) {
        File parentFile = new File(parentDir);
        if (!parentFile.exists()) parentFile.mkdirs();
      }
    }

    try {
      switch (_outputType) {
        case FILE_OUTPUT:
          clazz.dump(
              new BufferedOutputStream(new FileOutputStream(getOutputFile(clazz.getClassName()))));
          break;
        case JAR_OUTPUT:
          _bcelClasses.addElement(clazz);
          break;
        case BYTEARRAY_OUTPUT:
        case BYTEARRAY_AND_FILE_OUTPUT:
        case BYTEARRAY_AND_JAR_OUTPUT:
        case CLASSLOADER_OUTPUT:
          ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
          clazz.dump(out);
          _classes.addElement(out.toByteArray());

          if (_outputType == BYTEARRAY_AND_FILE_OUTPUT)
            clazz.dump(
                new BufferedOutputStream(
                    new FileOutputStream(getOutputFile(clazz.getClassName()))));
          else if (_outputType == BYTEARRAY_AND_JAR_OUTPUT) _bcelClasses.addElement(clazz);

          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#4
0
  /**
   * Initialize with existing class.
   *
   * @param clazz JavaClass object (e.g. read from file)
   */
  public ClassGen(JavaClass clazz) {
    class_name_index = clazz.getClassNameIndex();
    superclass_name_index = clazz.getSuperclassNameIndex();
    class_name = clazz.getClassName();
    super_class_name = clazz.getSuperclassName();
    file_name = clazz.getSourceFileName();
    access_flags = clazz.getAccessFlags();
    cp = new ConstantPoolGen(clazz.getConstantPool());
    major = clazz.getMajor();
    minor = clazz.getMinor();

    Attribute[] attributes = clazz.getAttributes();
    Method[] methods = clazz.getMethods();
    Field[] fields = clazz.getFields();
    String[] interfaces = clazz.getInterfaceNames();

    for (int i = 0; i < interfaces.length; i++) addInterface(interfaces[i]);

    for (int i = 0; i < attributes.length; i++) addAttribute(attributes[i]);

    for (int i = 0; i < methods.length; i++) addMethod(methods[i]);

    for (int i = 0; i < fields.length; i++) addField(fields[i]);
  }
示例#5
0
 /**
  * Add clazz to repository if there isn't an equally named class already in there.
  *
  * @return old entry in repository
  */
 public static JavaClass addClass(JavaClass clazz) {
   JavaClass old = _repository.findClass(clazz.getClassName());
   _repository.storeClass(clazz);
   return old;
 }