commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kohs...@apache.org
Subject svn commit: r359265 - in /jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow: ant/AntRewriteTask.java utils/CompositeTransformer.java utils/RewritingUtils.java
Date Tue, 27 Dec 2005 19:38:10 GMT
Author: kohsuke
Date: Tue Dec 27 11:37:58 2005
New Revision: 359265

URL: http://svn.apache.org/viewcvs?rev=359265&view=rev
Log:
added CompositeTransformer for composing multiple transformataions into one,
and simplified RewritingUtils

Added:
    jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/CompositeTransformer.java
  (with props)
Modified:
    jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/ant/AntRewriteTask.java
    jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/RewritingUtils.java

Modified: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/ant/AntRewriteTask.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/ant/AntRewriteTask.java?rev=359265&r1=359264&r2=359265&view=diff
==============================================================================
--- jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/ant/AntRewriteTask.java
(original)
+++ jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/ant/AntRewriteTask.java
Tue Dec 27 11:37:58 2005
@@ -102,23 +102,19 @@
                     log("Rewriting " + source + " to " + destination, Project.MSG_VERBOSE);
                     System.out.println("Rewriting " + source);
 
-                    RewritingUtils.rewriteClassFile(
-                            source,
-                            new ResourceTransformer[] { transformer },
-                            destination
-                            );                    
+                    RewritingUtils.rewriteClassFile( source, transformer, destination );
                 }
-                
+
                 if (fileName.endsWith(".jar")
                     || fileName.endsWith(".ear")
                     || fileName.endsWith(".zip")
                     || fileName.endsWith(".war")) {
 
                     log("Rewriting " + source + " to " + destination, Project.MSG_VERBOSE);
-                    
+
                     RewritingUtils.rewriteJar(
                             new JarInputStream(new FileInputStream(source)),
-                            new ResourceTransformer[] { transformer },
+                            transformer,
                             new JarOutputStream(new FileOutputStream(destination))
                             );
                     

Added: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/CompositeTransformer.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/CompositeTransformer.java?rev=359265&view=auto
==============================================================================
--- jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/CompositeTransformer.java
(added)
+++ jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/CompositeTransformer.java
Tue Dec 27 11:37:58 2005
@@ -0,0 +1,24 @@
+package org.apache.commons.javaflow.utils;
+
+import org.apache.commons.javaflow.bytecode.transformation.ResourceTransformer;
+
+/**
+ * {@link ResourceTransformer} whose transformation
+ * is defined in terms of multiple {@link ResourceTransformer}s.
+ *
+ * @author Kohsuke Kawaguchi
+ */
+public class CompositeTransformer implements ResourceTransformer {
+    private final ResourceTransformer[] transformers;
+
+    public CompositeTransformer(ResourceTransformer[] transformers) {
+        this.transformers = transformers;
+    }
+
+    public byte[] transform(byte[] image) {
+        for (int i = 0; i < transformers.length; i++) {
+            image = transformers[i].transform(image);
+        }
+        return image;
+    }
+}

Propchange: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/CompositeTransformer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/RewritingUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/RewritingUtils.java?rev=359265&r1=359264&r2=359265&view=diff
==============================================================================
--- jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/RewritingUtils.java
(original)
+++ jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/utils/RewritingUtils.java
Tue Dec 27 11:37:58 2005
@@ -50,71 +50,65 @@
      */
     public static void rewriteClassFile(
             final File pInput,
-            final ResourceTransformer[] pTransformers,
+            final ResourceTransformer transformer,
             final File pOutput
             ) throws IOException {
-        
+
         final byte[] original = FileUtils.readFileToByteArray(pInput);
-        byte[] transformed = original;
-        for (int i = 0; i < pTransformers.length; i++) {
-            transformed = pTransformers[i].transform(original);            
-        }
+        byte[] transformed = transformer.transform(original);
         final FileOutputStream os = new FileOutputStream(pOutput);
         os.write(transformed);
         os.close();
     }
-    
+
     public static boolean rewriteJar(
             final JarInputStream pInput,
-            final ResourceTransformer[] pTransformers,
+            final ResourceTransformer transformer,
             final JarOutputStream pOutput
             ) throws IOException {
-        return rewriteJar(pInput, pTransformers, pOutput, MATCH_ALL);
+        return rewriteJar(pInput, transformer, pOutput, MATCH_ALL);
     }
 
     public static boolean rewriteJar(
             final JarInputStream pInput,
-            final ResourceTransformer[] pTransformers,
+            final ResourceTransformer transformer,
             final JarOutputStream pOutput,
             final Matcher pMatcher
             ) throws IOException {
 
         boolean changed = false;
-        
+
         while(true) {
             final JarEntry entry = pInput.getNextJarEntry();
-            
+
             if (entry == null) {
                 break;
             }
-            
+
             if (entry.isDirectory()) {
                 pOutput.putNextEntry(new JarEntry(entry));
                 continue;
             }
-            
+
             final String name = entry.getName();
-            
+
             pOutput.putNextEntry(new JarEntry(name));
 
             if (name.endsWith(".class")) {
                 if (pMatcher.isMatching(name)) {
-                    
+
                     if (log.isDebugEnabled()) {
                         log.debug("transforming " + name);
                     }
-                    
+
                     final byte[] original = IOUtils.toByteArray(pInput);
-                    
-                    byte[] transformed = original;
-                    for (int i = 0; i < pTransformers.length; i++) {
-                        transformed = pTransformers[i].transform(original);            
-                    }
+
+                    byte[] transformed = transformer.transform(original);
 
                     IOUtils.copy(new ByteArrayInputStream(transformed), pOutput);
-    
+
                     changed |= transformed.length != original.length;
-                    
+
                     continue;
                 }
             } else if (name.endsWith(".jar")
@@ -124,20 +118,20 @@
 
                 changed |= rewriteJar(
                         new JarInputStream(pInput),
-                        pTransformers,
+                        transformer,
                         new JarOutputStream(pOutput),
                         pMatcher
                         );
-                
+
                 continue;
             }
-            
+
             final byte[] original = IOUtils.toByteArray(pInput);
             IOUtils.copy(new ByteArrayInputStream(original), pOutput);
-            
+
             log.debug("copied " + name + "(" + original.length + ")");
         }
-        
+
         pInput.close();
         pOutput.close();
 
@@ -146,15 +140,15 @@
 
     public static void main(final String[] args) throws FileNotFoundException, IOException
{
         System.out.println("rewriting");
-        
+
         ResourceTransformer transformer = new BcelClassTransformer();
-        
+
         for (int i=0; i<args.length; i+=2) {
             System.out.println("rewriting " + args[i]);
 
             RewritingUtils.rewriteJar(
                     new JarInputStream(new FileInputStream(args[i])),
-                    new ResourceTransformer[] { new BcelClassTransformer() },
+                    transformer,
                     new JarOutputStream(new FileOutputStream(args[i+1]))
                     );
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message