test_class.declarator: countCallbacks = 0
test_class.modifier: private
test_class.original_string: private int countCallbacks = 0;
test_class.type: int
test_class.var_name: countCallbacks
test_class.file: lang-java/src/test/java/org/eclipse/steady/java/JarWriterTest.java
test_class.identifier: JarWriterTest
test_class.interfaces: implements JarEntryWriter
test_case.body: @Test
    public void testRewrite() {
      try {
        // Create a JarWriter and do some settings
        final JarWriter jw = new JarWriter(Paths.get("./src/test/resources/examples.jar"));
        jw.addManifestEntry("Test", "JUnit test entry");
        jw.setClassifier(StringUtil.getRandonString(6));
        jw.addFile("", Paths.get("./src/test/resources/Callgraph.class"), true);
        jw.addFile("WEB-INF/lib", Paths.get("./src/test/resources/examples.jar"), true);
  
        assertEquals("F22A5E25F37455867B5C2CF476BAC25189AC2B28", jw.getSHA1());
  
        // Callback for .class files and rewrite
        jw.register(".*.class$", this);
        final Path rewritten = jw.rewrite(Paths.get("./target"));
  
        // Callbacks for 6 class files
        assertEquals(6, this.countCallbacks);
  
        // Create a new JarWriter and check whether is recognized as rewritten (the original SHA1 is
        // taken from the manifest)
        final JarWriter jw2 = new JarWriter(rewritten);
        assertTrue(jw2.isRewrittenByVulas());
        assertEquals("F22A5E25F37455867B5C2CF476BAC25189AC2B28", jw2.getSHA1());
      } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
      }
    }
test_case.class_method_signature: JarWriterTest.testRewrite()
test_case.full_signature: @Test public void testRewrite()
test_case.identifier: testRewrite
test_case.invocations: get addManifestEntry setClassifier getRandonString addFile get addFile get assertEquals getSHA1 register rewrite get assertEquals assertTrue isRewrittenByVulas assertEquals getSHA1 printStackTrace assertTrue
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testRewrite()
focal_class.declarator: log = org.apache.logging.log4j.LogManager.getLogger()
focal_class.modifier: private static final
focal_class.original_string: private static final Logger log = org.apache.logging.log4j.LogManager.getLogger();
focal_class.type: Logger
focal_class.var_name: log
focal_class.declarator: dateFormat = new SimpleDateFormat("d MMM yyyy HH:mm:ss")
focal_class.modifier: public final
focal_class.original_string: public final SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
focal_class.type: SimpleDateFormat
focal_class.var_name: dateFormat
focal_class.declarator: MANIFEST_ENTRY_VULAS_MODIF = "VULAS-modifiedAt"
focal_class.modifier: public static final
focal_class.original_string: public static final String MANIFEST_ENTRY_VULAS_MODIF = "VULAS-modifiedAt";
focal_class.type: String
focal_class.var_name: MANIFEST_ENTRY_VULAS_MODIF
focal_class.declarator: MANIFEST_ENTRY_ORIG_SHA1 = "VULAS-originalSHA1"
focal_class.modifier: public static final
focal_class.original_string: public static final String MANIFEST_ENTRY_ORIG_SHA1 = "VULAS-originalSHA1";
focal_class.type: String
focal_class.var_name: MANIFEST_ENTRY_ORIG_SHA1
focal_class.declarator: hexArray = "0123456789ABCDEF".toCharArray()
focal_class.modifier: protected static final
focal_class.original_string: protected static final char[] hexArray = "0123456789ABCDEF".toCharArray();
focal_class.type: char[]
focal_class.var_name: hexArray
focal_class.declarator: originalJar = null
focal_class.modifier: private
focal_class.original_string: private JarFile originalJar = null;
focal_class.type: JarFile
focal_class.var_name: originalJar
focal_class.declarator: originalFileSize = 0
focal_class.modifier: private
focal_class.original_string: private long originalFileSize = 0;
focal_class.type: long
focal_class.var_name: originalFileSize
focal_class.declarator: originalManifest = null
focal_class.modifier: private
focal_class.original_string: private Manifest originalManifest = null;
focal_class.type: Manifest
focal_class.var_name: originalManifest
focal_class.declarator: sha1 = null
focal_class.modifier: private
focal_class.original_string: private String sha1 = null;
focal_class.type: String
focal_class.var_name: sha1
focal_class.declarator: mfEntriesToSkip = new HashSet<String>()
focal_class.modifier: private
focal_class.original_string: private Set<String> mfEntriesToSkip = new HashSet<String>();
focal_class.type: Set<String>
focal_class.var_name: mfEntriesToSkip
focal_class.declarator: mfEntriesToAdd = new HashMap<String, String>()
focal_class.modifier: private
focal_class.original_string: private Map<String, String> mfEntriesToAdd = new HashMap<String, String>();
focal_class.type: Map<String, String>
focal_class.var_name: mfEntriesToAdd
focal_class.declarator: classifier = null
focal_class.modifier: private
focal_class.original_string: private String classifier = null;
focal_class.type: String
focal_class.var_name: classifier
focal_class.declarator: callbacks = new HashMap<Pattern, JarEntryWriter>()
focal_class.modifier: private
focal_class.original_string: private Map<Pattern, JarEntryWriter> callbacks = new HashMap<Pattern, JarEntryWriter>();
focal_class.type: Map<Pattern, JarEntryWriter>
focal_class.var_name: callbacks
focal_class.declarator: rewrittenFile = null
focal_class.modifier: private
focal_class.original_string: private File rewrittenFile = null;
focal_class.type: File
focal_class.var_name: rewrittenFile
focal_class.declarator: additionalFiles = new HashMap<String, Path>()
focal_class.modifier: private
focal_class.original_string: private Map<String, Path> additionalFiles = new HashMap<String, Path>();
focal_class.type: Map<String, Path>
focal_class.var_name: additionalFiles
focal_class.file: lang-java/src/main/java/org/eclipse/steady/java/JarWriter.java
focal_class.identifier: JarWriter
focal_class.class_method_signature: JarWriter.JarWriter(Path _jar)
focal_class.full_signature: public  JarWriter(Path _jar)
focal_class.identifier: JarWriter
focal_class.modifiers: public
focal_class.parameters: (Path _jar)
focal_class.signature:  JarWriter(Path _jar)
focal_class.class_method_signature: JarWriter.getFileSize()
focal_class.full_signature: public long getFileSize()
focal_class.identifier: getFileSize
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: long
focal_class.signature: long getFileSize()
focal_class.class_method_signature: JarWriter.getInstrumentedFileSize()
focal_class.full_signature: public long getInstrumentedFileSize()
focal_class.identifier: getInstrumentedFileSize
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: long
focal_class.signature: long getInstrumentedFileSize()
focal_class.class_method_signature: JarWriter.getOriginalManifest()
focal_class.full_signature: public Manifest getOriginalManifest()
focal_class.identifier: getOriginalManifest
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: Manifest
focal_class.signature: Manifest getOriginalManifest()
focal_class.class_method_signature: JarWriter.extract(Path _todir)
focal_class.full_signature: public Path extract(Path _todir)
focal_class.identifier: extract
focal_class.modifiers: public
focal_class.parameters: (Path _todir)
focal_class.return: Path
focal_class.signature: Path extract(Path _todir)
focal_class.class_method_signature: JarWriter.getSHA1()
focal_class.full_signature: public synchronized String getSHA1()
focal_class.identifier: getSHA1
focal_class.modifiers: public synchronized
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getSHA1()
focal_class.class_method_signature: JarWriter.skipManifestEntry(String _entry)
focal_class.full_signature: public void skipManifestEntry(String _entry)
focal_class.identifier: skipManifestEntry
focal_class.modifiers: public
focal_class.parameters: (String _entry)
focal_class.return: void
focal_class.signature: void skipManifestEntry(String _entry)
focal_class.class_method_signature: JarWriter.addManifestEntry(String _key, String _val)
focal_class.full_signature: public void addManifestEntry(String _key, String _val)
focal_class.identifier: addManifestEntry
focal_class.modifiers: public
focal_class.parameters: (String _key, String _val)
focal_class.return: void
focal_class.signature: void addManifestEntry(String _key, String _val)
focal_class.class_method_signature: JarWriter.hasManifestEntry(String _key)
focal_class.full_signature: public boolean hasManifestEntry(String _key)
focal_class.identifier: hasManifestEntry
focal_class.modifiers: public
focal_class.parameters: (String _key)
focal_class.return: boolean
focal_class.signature: boolean hasManifestEntry(String _key)
focal_class.class_method_signature: JarWriter.isRewrittenByVulas()
focal_class.full_signature: public boolean isRewrittenByVulas()
focal_class.identifier: isRewrittenByVulas
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean isRewrittenByVulas()
focal_class.class_method_signature: JarWriter.setClassifier(String _string)
focal_class.full_signature: public void setClassifier(String _string)
focal_class.identifier: setClassifier
focal_class.modifiers: public
focal_class.parameters: (String _string)
focal_class.return: void
focal_class.signature: void setClassifier(String _string)
focal_class.class_method_signature: JarWriter.register(String _regex, JarEntryWriter _writer)
focal_class.full_signature: public void register(String _regex, JarEntryWriter _writer)
focal_class.identifier: register
focal_class.modifiers: public
focal_class.parameters: (String _regex, JarEntryWriter _writer)
focal_class.return: void
focal_class.signature: void register(String _regex, JarEntryWriter _writer)
focal_class.class_method_signature: JarWriter.createModifiedManifest()
focal_class.full_signature: private Manifest createModifiedManifest()
focal_class.identifier: createModifiedManifest
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.return: Manifest
focal_class.signature: Manifest createModifiedManifest()
focal_class.class_method_signature: JarWriter.getOriginalJarFileName()
focal_class.full_signature: public Path getOriginalJarFileName()
focal_class.identifier: getOriginalJarFileName
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: Path
focal_class.signature: Path getOriginalJarFileName()
focal_class.class_method_signature: JarWriter.getRewriteJarFileName()
focal_class.full_signature: public Path getRewriteJarFileName()
focal_class.identifier: getRewriteJarFileName
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: Path
focal_class.signature: Path getRewriteJarFileName()
focal_class.class_method_signature: JarWriter.addFiles(String _target_dir, Set<Path> _paths, boolean _overwrite)
focal_class.full_signature: public void addFiles(String _target_dir, Set<Path> _paths, boolean _overwrite)
focal_class.identifier: addFiles
focal_class.modifiers: public
focal_class.parameters: (String _target_dir, Set<Path> _paths, boolean _overwrite)
focal_class.return: void
focal_class.signature: void addFiles(String _target_dir, Set<Path> _paths, boolean _overwrite)
focal_class.class_method_signature: JarWriter.addFile(String _target_dir, Path _path, boolean _overwrite)
focal_class.full_signature: public void addFile(String _target_dir, Path _path, boolean _overwrite)
focal_class.identifier: addFile
focal_class.modifiers: public
focal_class.parameters: (String _target_dir, Path _path, boolean _overwrite)
focal_class.return: void
focal_class.signature: void addFile(String _target_dir, Path _path, boolean _overwrite)
focal_class.class_method_signature: JarWriter.hasEntry(String _entry_name)
focal_class.full_signature: public boolean hasEntry(String _entry_name)
focal_class.identifier: hasEntry
focal_class.modifiers: public
focal_class.parameters: (String _entry_name)
focal_class.return: boolean
focal_class.signature: boolean hasEntry(String _entry_name)
focal_class.class_method_signature: JarWriter.getRewrittenJarFile()
focal_class.full_signature: public File getRewrittenJarFile()
focal_class.identifier: getRewrittenJarFile
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: File
focal_class.signature: File getRewrittenJarFile()
focal_class.class_method_signature: JarWriter.rewrite(Path _todir)
focal_class.full_signature: public Path rewrite(Path _todir)
focal_class.identifier: rewrite
focal_class.modifiers: public
focal_class.parameters: (Path _todir)
focal_class.return: Path
focal_class.signature: Path rewrite(Path _todir)
focal_class.class_method_signature: JarWriter.appendToClasspath(
        Set<Path> _classpath, Set<Path> _to_append, boolean _preprocess)
focal_class.full_signature: public static final void appendToClasspath(
        Set<Path> _classpath, Set<Path> _to_append, boolean _preprocess)
focal_class.identifier: appendToClasspath
focal_class.modifiers: public static final
focal_class.parameters: (
        Set<Path> _classpath, Set<Path> _to_append, boolean _preprocess)
focal_class.return: void
focal_class.signature: void appendToClasspath(
        Set<Path> _classpath, Set<Path> _to_append, boolean _preprocess)
focal_class.class_method_signature: JarWriter.appendToClasspath(
        Set<Path> _classpath, Path _to_append, boolean _preprocess)
focal_class.full_signature: public static final Path appendToClasspath(
        Set<Path> _classpath, Path _to_append, boolean _preprocess)
focal_class.identifier: appendToClasspath
focal_class.modifiers: public static final
focal_class.parameters: (
        Set<Path> _classpath, Path _to_append, boolean _preprocess)
focal_class.return: Path
focal_class.signature: Path appendToClasspath(
        Set<Path> _classpath, Path _to_append, boolean _preprocess)
focal_method.body: public Path rewrite(Path _todir) throws JarAnalysisException {
      // Target dir
      Path dir = _todir;
  
      // Callback logic
      Matcher matcher = null;
  
      // Loop all entries of the old JAR
      JarEntry old_entry = null, new_entry = null;
  
      try {
        if (dir == null) dir = java.nio.file.Files.createTempDirectory("rewritten_jar_");
        this.rewrittenFile =
            Paths.get(dir.toString(), this.getRewriteJarFileName().toString()).toFile();
  
        if (this.rewrittenFile.exists()) {
          JarWriter.log.info(
              "The target [" + this.rewrittenFile + "] already exists, skip rewriting");
        } else {
          final FileOutputStream fos = new FileOutputStream(this.rewrittenFile);
          final JarOutputStream jos = new JarOutputStream(fos, this.createModifiedManifest());
          InputStream is = null;
          byte[] bytes = new byte[1024];
          int bytes_read = 0;
  
          final Enumeration<JarEntry> en = this.originalJar.entries();
          String class_name = null;
          JavaId jid = null;
          Set<JavaId> jids = new HashSet<JavaId>();
  
          // Remember all JAR entries written to the new JAR, so that we do not create duplicate
          // entries
          // Example of a duplicate entry: Location.class in xmlbeans-2.6.0.jar
          Set<String> written_jar_entries = new HashSet<String>();
  
          while (en.hasMoreElements()) {
            old_entry = en.nextElement();
  
            // The input stream used for writing the entries
            is = null;
  
            // Check whether we already write an entry with this name
            if (written_jar_entries.contains(old_entry.getName())) continue;
  
            // Ignore the original manifest (we built a new one with certain vulas-specific
            // attributes)
            if (old_entry.getName().equals("META-INF/MANIFEST.MF")) continue;
  
            // Ignore signature related files (->
            // http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Signed_JAR_File)
            if (old_entry.getName().startsWith("META-INF/")
                && (old_entry.getName().toLowerCase().endsWith(".sf")
                    || old_entry.getName().toLowerCase().endsWith(".dsa")
                    || old_entry.getName().toLowerCase().endsWith(".rsa"))) continue;
  
            // Loop registered JarEntryWriters to see if any matches (take the input stream from the
            // first match)
            for (Map.Entry<Pattern, JarEntryWriter> e : this.callbacks.entrySet()) {
              matcher = e.getKey().matcher(old_entry.getName());
              if (matcher.matches()) {
                is = e.getValue().getInputStream(e.getKey().toString(), old_entry);
              }
            }
  
            // If null, take the original file
            if (is == null) is = this.originalJar.getInputStream(old_entry);
  
            // Debug information regarding specific attributes
            if (old_entry.getAttributes() != null)
              JarWriter.log.debug(
                  this.toString() + ": Entry [" + old_entry.getName() + "] has specific attributes");
  
            // Write the entry to the modified JAR
            new_entry = new JarEntry(old_entry.getName());
            jos.putNextEntry(new_entry);
            while ((bytes_read = is.read(bytes)) != -1) jos.write(bytes, 0, bytes_read);
  
            // is.close();
            jos.closeEntry();
  
            // Remember we wrote it
            written_jar_entries.add(new_entry.getName());
          }
  
          // Add additional files
          for (Map.Entry<String, Path> e : this.additionalFiles.entrySet()) {
            if (e.getValue().toFile().exists()) {
              new_entry = new JarEntry(e.getKey());
              jos.putNextEntry(new_entry);
              is = new FileInputStream(e.getValue().toFile());
              while ((bytes_read = is.read(bytes)) != -1) jos.write(bytes, 0, bytes_read);
              is.close();
              jos.closeEntry();
            }
          }
  
          jos.flush();
          jos.close();
          this.originalJar.close();
  
          //
          old_entry = null;
          JarWriter.log.info(
              "[" + this.originalJar.getName() + "] rewritten to [" + this.rewrittenFile + "]");
        }
      } catch (Exception ioe) {
        if (old_entry != null)
          throw new JarAnalysisException(
              "Error while writing JAR entry ["
                  + old_entry.getName()
                  + "] to modified JAR ["
                  + this.rewrittenFile
                  + "]: "
                  + ioe.getMessage(),
              ioe);
        else
          throw new JarAnalysisException(
              "Error while writing modified JAR: " + ioe.getMessage(), ioe);
      }
      return this.rewrittenFile.toPath();
    }
focal_method.class_method_signature: JarWriter.rewrite(Path _todir)
focal_method.full_signature: public Path rewrite(Path _todir)
focal_method.identifier: rewrite
focal_method.invocations: createTempDirectory toFile get toString toString getRewriteJarFileName exists info createModifiedManifest entries hasMoreElements nextElement contains getName equals getName startsWith getName endsWith toLowerCase getName endsWith toLowerCase getName endsWith toLowerCase getName entrySet matcher getKey getName matches getInputStream getValue toString getKey getInputStream getAttributes debug toString getName getName putNextEntry read write closeEntry add getName entrySet exists toFile getValue getKey putNextEntry toFile getValue read write close closeEntry flush close close info getName getName getMessage getMessage toPath
focal_method.modifiers: public
focal_method.parameters: (Path _todir)
focal_method.return: Path
focal_method.signature: Path rewrite(Path _todir)