Coverage report

  %line %branch
net.sf.infrared.tools.weaving.Aj2System
0% 
0% 

 1  
 /*
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  *
 17  
  *
 18  
  * Original Author:  binil.thomas (Tavant Technologies)
 19  
  * Contributor(s):   -;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.tools.weaving;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.net.URL;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 import java.io.File;
 29  
 import java.util.ArrayList;
 30  
 import net.sf.infrared.tools.util.JarHelper;
 31  
 import org.apache.commons.io.FileUtils;
 32  
 import org.aspectj.tools.ajc.Main;
 33  
 
 34  
 /**
 35  
  *
 36  
  * @author binil.thomas
 37  
  */
 38  0
 public class Aj2System extends AbstractAspectSystem {
 39  0
     private boolean loggedOnce = false;
 40  
     
 41  
     public void instrumentJars(File[] jars) {
 42  0
         if (! loggedOnce) {
 43  0
             getLogger().verbose("[Aj2] Setup: " + 
 44  
                     "\n\t AspectPath = " + getAspectPathAsString() +
 45  
                     "\n\t ClassPath  = " + getClassPathAsString());
 46  0
             loggedOnce = true;
 47  
         }
 48  0
         for (int i = 0; i < jars.length; i++) {
 49  0
             instrumentJar(jars[i]);
 50  
         }
 51  0
     }
 52  
     
 53  
     public void instrumentJar(File jar) {
 54  0
         File temp = null;
 55  
         try {
 56  0
             temp = createTempCopy(jar);
 57  0
         } catch (IOException ex) {
 58  0
             throw new RuntimeException("Failed to copy jar file " + jar.getAbsolutePath()
 59  
             + " to temp file for instrumentation", ex);
 60  0
         }
 61  
 // @TODO not sure if the original will be overwritten; if not we need to delete the original first
 62  
 //        try {
 63  
 //            FileUtils.forceDelete(jar);
 64  
 //            System.out.println("Deleted original file " + jar.getAbsolutePath());
 65  
 //        } catch (IOException ex) {
 66  
 //            throw new RuntimeException("Failed to over-write jar file " + jar.getAbsolutePath(), ex);
 67  
 //        }
 68  
         
 69  0
         String[] args = new String[8];
 70  0
         args[0] = "-injars";
 71  0
         args[1] = temp.getAbsolutePath();
 72  0
         args[2] = "-outjar";
 73  0
         args[3] = jar.getAbsolutePath();
 74  0
         args[4] = "-aspectpath";
 75  0
         args[5] = getAspectPathAsString();
 76  0
         args[6] = "-cp";
 77  0
         args[7] = getClassPathAsString();
 78  
         
 79  0
         List fails = new ArrayList();
 80  0
         List errors = new ArrayList();
 81  0
         List warnings = new ArrayList();
 82  0
         List infos = new ArrayList();
 83  0
         Main.bareMain(args, false, fails, errors, warnings, infos);
 84  
         
 85  0
         if (errors.size() != 0) {
 86  0
             String msg = "\n";
 87  0
             for (Iterator it = errors.iterator(); it.hasNext();) {
 88  0
                 String elem = it.next().toString();
 89  0
                 msg += elem + "\n";
 90  
             }
 91  0
             throw new RuntimeException("Error instrumenting jar file " +
 92  
                     jar.getAbsolutePath() + " " + msg);
 93  
         }
 94  0
         getLogger().verbose("[Aj2] Instrumented jar " + jar.getAbsolutePath());
 95  0
     }
 96  
     
 97  
     public void instrumentClasses(File[] classes, String[] packages) { 
 98  0
         if (! loggedOnce) {
 99  0
             getLogger().verbose("[Aj2] Setup: " + 
 100  
                     "\n\t AspectPath = " + getAspectPathAsString() +
 101  
                     "\n\t ClassPath  = " + getClassPathAsString());
 102  0
             loggedOnce = true;
 103  
         }
 104  0
         for (int i = 0; i < classes.length; i++) {
 105  0
             instrumentClass(classes[i], packages[i]);
 106  
         }
 107  0
     }
 108  
     
 109  
     public void instrumentClass(File classFile, String pkg) {
 110  
         File tempClassDir;
 111  
         try {
 112  0
         	tempClassDir = copyClassToTempDir(classFile, pkg);
 113  0
         } catch (IOException ex) {
 114  0
             throw new RuntimeException("Failed to create working copy of class " +
 115  
                     classFile.getAbsoluteFile() + " in package " + pkg);
 116  0
         }
 117  
         
 118  0
         File outputDir = null;
 119  
         try {
 120  0
             outputDir = createTempDir();
 121  0
         } catch (IOException ex) {
 122  0
             throw new RuntimeException("Failed to create directory to hold the aspect " +
 123  
                     "class temporarily", ex);
 124  0
         }
 125  
         
 126  0
         String[] args = new String[9];
 127  0
         args[0] = "-inpath";
 128  0
         args[1] = tempClassDir.getAbsolutePath();
 129  0
         args[2] = "-d";
 130  0
         args[3] = outputDir.getAbsolutePath();
 131  0
         args[4] = "-aspectpath";
 132  0
         args[5] = getAspectPathAsString();
 133  0
         args[6] = "-cp";
 134  0
         args[7] = getClassPathAsString();
 135  0
         args[8] = "-verbose";
 136  
 
 137  0
         List fails = new ArrayList();
 138  0
         List errors = new ArrayList();
 139  0
         List warnings = new ArrayList();
 140  0
         List infos = new ArrayList();
 141  0
         Main.bareMain(args, false, fails, errors, warnings, infos);
 142  
         
 143  0
         if (errors.size() != 0) {
 144  0
             String msg = "\n";
 145  0
             for (Iterator it = errors.iterator(); it.hasNext();) {
 146  0
                 String elem = it.next().toString();
 147  0
                 msg += elem + "\n";
 148  
             }
 149  0
             throw new RuntimeException("Error instrumenting class file " +
 150  
                     classFile.getAbsolutePath() + " " + msg);
 151  
         }
 152  
         
 153  0
         File aspectedClassFile = 
 154  
                 new File(outputDir, convertPackageToPath(pkg) + File.separator + classFile.getName());
 155  
         try {
 156  0
             FileUtils.copyFile(aspectedClassFile, classFile);
 157  0
         } catch (IOException ex) {
 158  0
             throw new RuntimeException("Failed to copy aspected class" +
 159  
                     aspectedClassFile.getAbsolutePath() + " over the original class " +
 160  
                     classFile.getAbsolutePath());
 161  0
         }
 162  0
         getLogger().verbose("[Aj2] Instrumented class " + classFile.getAbsolutePath());
 163  0
     }
 164  
     
 165  
     public void addAspectPath(File[] aspects) {
 166  0
         for (int i = 0; i < aspects.length; i++) {
 167  0
             if (aspects[i].isFile()) {
 168  0
                 continue;
 169  
             }
 170  
             try {
 171  0
                 File dir = aspects[i];
 172  0
                 aspects[i] = createTempArchive(dir);
 173  0
                 getLogger().verbose("[Aj2] Archived " + dir.getAbsolutePath() + " to " + 
 174  
                         aspects[i] + " temporarily to pass into AspectJ as aspectpath");
 175  0
             } catch (IOException ex) {
 176  0
                 throw new RuntimeException("Failed to create temporary archive of directory " +
 177  
                         aspects[i].getAbsolutePath() + " in aspect path.", ex);
 178  0
             }
 179  
         }
 180  0
         super.addAspectPath(aspects);
 181  0
     }
 182  
     
 183  
     String getClassPathAsString() {
 184  0
         String cp = super.getClassPathAsString();
 185  0
         URL u = getClass().getClassLoader().getResource("aspectjrt.jar");
 186  0
         cp += new File(u.getFile()).getAbsolutePath();
 187  0
         return cp;
 188  
     }
 189  
     
 190  
     File createTempArchive(File dir) throws IOException {
 191  0
         File temp = createTempFile(dir.getName() + "-archive", ".jar");
 192  0
         new JarHelper().jarDir(dir, temp);
 193  0
         temp.deleteOnExit();
 194  0
         return temp;
 195  
     }
 196  
     
 197  
     File createTempCopy(File orig) throws IOException {
 198  0
         File temp = createTempFile(orig.getName(), ".tmp");
 199  0
         FileUtils.copyFile(orig, temp);
 200  0
         temp.deleteOnExit();
 201  0
         return temp;
 202  
     }
 203  
     
 204  
     File copyClassToTempDir(File clazz, String pkg) throws IOException {        
 205  0
     	String p = convertPackageToPath(pkg);
 206  
         
 207  
     	// Need to check if the logic is correct in case of files without a package.
 208  0
     	int i = (p.length() == 0) ? clazz.getAbsolutePath().lastIndexOf(
 209  
 				File.separator) + 1 : clazz.getAbsolutePath().lastIndexOf(p);
 210  
                 
 211  0
         if (i == -1) {
 212  0
             throw new RuntimeException("Failed to figure out the parent directory of class " +
 213  
                     clazz.getAbsolutePath() + " in package " + pkg);
 214  
         }
 215  0
         File tempDir = createTempDir();
 216  0
         File copy = new File(tempDir, clazz.getAbsolutePath().substring(i));                
 217  0
         FileUtils.forceMkdir(copy.getParentFile());
 218  0
         FileUtils.copyFile(clazz, copy);
 219  0
         copy.deleteOnExit();
 220  0
         return tempDir;
 221  
     }
 222  
     
 223  
     File createTempDir() throws IOException {
 224  0
     	File tempDir = null;
 225  
         
 226  0
         for (int i = 0; true; i++) {
 227  0
             tempDir = new File(System.getProperty("java.io.tmpdir"), "infrared-temp-" + i);
 228  0
             if (! tempDir.exists()) {
 229  0
                 break;
 230  
             } 
 231  
         }    
 232  0
         FileUtils.forceMkdir(tempDir);
 233  0
         FileUtils.forceDeleteOnExit(tempDir);
 234  0
         return tempDir;
 235  
     }
 236  
     
 237  
     File createTempFile(String name, String extn) throws IOException {
 238  0
         File tempFile = null;
 239  
         
 240  0
         for (int i = 0; true; i++) {
 241  0
             tempFile = new File(System.getProperty("java.io.tmpdir"), name + "-" + i + extn);
 242  0
             if (! tempFile.exists()) {
 243  0
                 break;
 244  
             }
 245  
         }
 246  0
         FileUtils.forceDeleteOnExit(tempFile);
 247  0
         return tempFile;
 248  
     }
 249  
     
 250  
     String convertPackageToPath(String pkg) {
 251  0
         String p = pkg;
 252  0
         if ("\\".equals(File.separator)) {
 253  0
             p = pkg.replaceAll("\\.", "\\\\");
 254  
         } else {
 255  0
             p = pkg.replaceAll("\\.", File.separator);
 256  
         }
 257  0
         return p;
 258  
     }
 259  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.