Пример #1
0
  private static void writeFile(
      ByteCodeClass cls, File outputDir, ConcatenatingFileOutputStream writeBufferInstead)
      throws Exception {
    OutputStream outMain =
        writeBufferInstead != null
                && ByteCodeTranslator.output == ByteCodeTranslator.OutputType.OUTPUT_TYPE_IOS
            ? writeBufferInstead
            : new FileOutputStream(
                new File(
                    outputDir, cls.getClsName() + "." + ByteCodeTranslator.output.extension()));

    if (outMain instanceof ConcatenatingFileOutputStream) {
      ((ConcatenatingFileOutputStream) outMain).beginNextFile(cls.getClsName());
    }
    if (ByteCodeTranslator.output == ByteCodeTranslator.OutputType.OUTPUT_TYPE_IOS) {
      outMain.write(cls.generateCCode(classes).getBytes());
      outMain.close();

      // we also need to write the header file for iOS
      String headerName = cls.getClsName() + ".h";
      FileOutputStream outHeader = new FileOutputStream(new File(outputDir, headerName));
      outHeader.write(cls.generateCHeader().getBytes());
      outHeader.close();
    } else {
      outMain.write(cls.generateCSharpCode().getBytes());
      outMain.close();
    }
  }
Пример #2
0
  public static void writeOutput(File outputDirectory) throws Exception {
    System.out.println("outputDirectory is: " + outputDirectory.getAbsolutePath());
    if (ByteCodeClass.getMainClass() == null) {
      System.out.println(
          "Error main class is not defined. The main class name is expected to have a public static void main(String[]) method and it is assumed to reside in the com.package.name directory");
      System.exit(1);
    }
    String file = "Unknown File";
    try {
      for (ByteCodeClass bc : classes) {
        // special case for object
        if (bc.getClsName().equals("java_lang_Object")) {
          continue;
        }
        file = bc.getClsName();
        bc.setBaseClassObject(getClassByName(bc.getBaseClass()));
        List<ByteCodeClass> lst = new ArrayList<ByteCodeClass>();
        for (String s : bc.getBaseInterfaces()) {
          ByteCodeClass bcode = getClassByName(s);
          if (bcode == null) {
            System.out.println(
                "Error while working with the class: "
                    + s
                    + " file:"
                    + file
                    + " no class definition");
          } else {
            lst.add(getClassByName(s));
          }
        }
        bc.setBaseInterfacesObject(lst);
      }
      for (ByteCodeClass bc : classes) {
        file = bc.getClsName();
        bc.updateAllDependencies();
      }
      ByteCodeClass.markDependencies(classes);
      classes = ByteCodeClass.clearUnmarked(classes);

      // load the native sources (including user native code)
      readNativeFiles(outputDirectory);

      // loop over methods and start eliminating the body of unused methods
      eliminateUnusedMethods();

      generateClassAndMethodIndexHeader(outputDirectory);

      boolean concatenate = "true".equals(System.getProperty("concatenateFiles", "false"));
      ConcatenatingFileOutputStream cos =
          concatenate ? new ConcatenatingFileOutputStream(outputDirectory) : null;

      for (ByteCodeClass bc : classes) {
        file = bc.getClsName();
        writeFile(bc, outputDirectory, cos);
      }
      if (cos != null) cos.realClose();

    } catch (Throwable t) {
      System.out.println("Error while working with the class: " + file);
      t.printStackTrace();
      if (t instanceof Exception) {
        throw (Exception) t;
      }
      if (t instanceof RuntimeException) {
        throw (RuntimeException) t;
      }
    }
  }