コード例 #1
0
ファイル: Fake.java プロジェクト: tariqabdulla/fiji
 /*
  * This function inspects a .class file for a given .java file,
  * infers the package name and all used classes, and adds to "all"
  * the class file names of those classes used that have been found
  * in the same class path.
  */
 protected void java2classFiles(
     String java, File cwd, File buildDir, List<String> result, Set<String> all) {
   if (java.endsWith(".java")) java = java.substring(0, java.length() - 5) + ".class";
   else if (!java.endsWith(".class")) {
     if (!all.contains(java)) {
       if (buildDir == null) sortClassesAtEnd(result);
       result.add(java);
       all.add(java);
     }
     return;
   }
   byte[] buffer = Util.readFile(Util.makePath(cwd, java));
   if (buffer == null) {
     if (!java.endsWith("/package-info.class"))
       err.println("Warning: " + java + " does not exist.  Skipping...");
     return;
   }
   ByteCodeAnalyzer analyzer = new ByteCodeAnalyzer(buffer);
   String fullClass = analyzer.getPathForClass() + ".class";
   if (!java.endsWith(fullClass))
     throw new RuntimeException("Huh? " + fullClass + " is not a suffix of " + java);
   java = java.substring(0, java.length() - fullClass.length());
   for (String className : analyzer.getClassNames()) {
     String path = java + className + ".class";
     if (new File(Util.makePath(cwd, path)).exists() && !all.contains(path)) {
       result.add(path);
       all.add(path);
       java2classFiles(path, cwd, buildDir, result, all);
     }
   }
 }
コード例 #2
0
ファイル: Fake.java プロジェクト: tariqabdulla/fiji
  // TODO: we really need string pairs; real path and desired path.
  protected void makeJar(
      String path,
      String mainClass,
      List<String> files,
      File cwd,
      File buildDir,
      String configPath,
      String stripPath,
      boolean verbose)
      throws FakeException {
    path = Util.makePath(cwd, path);
    if (verbose) {
      String output = "Making " + path;
      if (mainClass != null) output += " with main-class " + mainClass;
      output += " from";
      for (String file : files) output += " " + file;
      err.println(output);
    }
    Manifest manifest = null;
    if (mainClass != null) {
      String text = "Manifest-Version: 1.0\nMain-Class: " + mainClass + "\n";
      InputStream input = new ByteArrayInputStream(text.getBytes());
      try {
        manifest = new Manifest(input);
      } catch (Exception e) {
      }
    }

    try {
      /*
       * Avoid SIGBUS when writing fake.jar: it may be
       * in use (mmap()ed), and overwriting that typically
       * results in a crash.
       */
      String origPath = null;
      try {
        if (Util.moveFileOutOfTheWay(path)) {
          origPath = path;
          path += ".new";
        }
      } catch (FakeException e) {
        path = moveToUpdateDirectory(path);
      }

      OutputStream out = new FileOutputStream(path);
      JarOutputStream jar =
          manifest == null ? new JarOutputStream(out) : new JarOutputStream(out, manifest);

      addPluginsConfigToJar(jar, configPath);
      String lastBase = stripPath;
      for (String realName : files) {
        if (realName.endsWith(".jar/")) {
          copyJar(Util.stripSuffix(Util.makePath(cwd, realName), "/"), jar, verbose);
          continue;
        }
        if (realName.endsWith("/") || realName.endsWith("\\") || realName.equals("")) {
          lastBase = realName;
          continue;
        }
        String name = realName;
        int bracket = name.indexOf('[');
        if (bracket >= 0 && name.endsWith("]")) {
          realName = name.substring(bracket + 1, name.length() - 1);
          name = name.substring(0, bracket);
        }
        byte[] buffer = Util.readFile(Util.makePath(cwd, realName));
        if (buffer == null)
          throw new FakeException(
              "File " + realName + " does not exist," + " could not make " + path);
        if (realName.endsWith(".class")) {
          ByteCodeAnalyzer analyzer = new ByteCodeAnalyzer(buffer);
          name = analyzer.getPathForClass() + ".class";
        } else if (lastBase != null && name.startsWith(lastBase)) {
          if (!lastBase.equals(stripPath))
            throw new FakeException("strip " + "path mismatch: " + lastBase + " != " + stripPath);
          name = name.substring(lastBase.length());
        }

        JarEntry entry = new JarEntry(name);
        writeJarEntry(entry, jar, buffer);
      }

      jar.close();
      if (origPath != null)
        throw new FakeException(
            "Could not remove "
                + origPath
                + " before building it anew\n"
                + "Stored it as "
                + path
                + " instead.");
    } catch (Exception e) {
      new File(path).delete();
      e.printStackTrace();
      throw new FakeException("Error writing " + path + ": " + e);
    }
  }