Search in sources :

Example 1 with TCCLSetterVisitor

use of org.apache.aries.spifly.weaver.TCCLSetterVisitor in project aries by apache.

the class ClientWeavingHook method weave.

@Override
public void weave(WovenClass wovenClass) {
    Bundle consumerBundle = wovenClass.getBundleWiring().getBundle();
    Set<WeavingData> wd = activator.getWeavingData(consumerBundle);
    if (wd != null) {
        activator.log(Level.FINE, "Weaving class " + wovenClass.getClassName());
        ClassReader cr = new ClassReader(wovenClass.getBytes());
        ClassWriter cw = new OSGiFriendlyClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES, wovenClass.getBundleWiring().getClassLoader());
        TCCLSetterVisitor tsv = new TCCLSetterVisitor(cw, wovenClass.getClassName(), wd);
        cr.accept(tsv, ClassReader.SKIP_FRAMES);
        if (tsv.isWoven()) {
            wovenClass.setBytes(cw.toByteArray());
            if (tsv.additionalImportRequired())
                wovenClass.getDynamicImports().add(addedImport);
            if (activator.isLogEnabled(Level.FINEST)) {
                StringWriter stringWriter = new StringWriter();
                ClassReader reader = new ClassReader(wovenClass.getBytes());
                ClassVisitor tracer = new TraceClassVisitor(new PrintWriter(stringWriter));
                ClassVisitor checker = new CheckClassAdapter(tracer, true);
                reader.accept(checker, 0);
                activator.log(Level.FINEST, "Woven class bytecode: \n" + stringWriter.toString());
            }
        }
    }
}
Also used : WeavingData(org.apache.aries.spifly.WeavingData) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) Bundle(org.osgi.framework.Bundle) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) TCCLSetterVisitor(org.apache.aries.spifly.weaver.TCCLSetterVisitor) PrintWriter(java.io.PrintWriter)

Example 2 with TCCLSetterVisitor

use of org.apache.aries.spifly.weaver.TCCLSetterVisitor in project aries by apache.

the class Main method weaveDir.

private static void weaveDir(File dir, String consumerHeaderKey, String consumerHeaderValue, String bundleClassPath) throws Exception {
    Set<WeavingData> wd = ConsumerHeaderProcessor.processHeader(consumerHeaderKey, consumerHeaderValue);
    URLClassLoader cl = new URLClassLoader(new URL[] { dir.toURI().toURL() }, Main.class.getClassLoader());
    String dirName = dir.getAbsolutePath();
    DirTree dt = new DirTree(dir);
    for (File f : dt.getFiles()) {
        if (!f.getName().endsWith(".class"))
            continue;
        String className = f.getAbsolutePath().substring(dirName.length());
        if (className.startsWith(File.separator))
            className = className.substring(1);
        className = className.substring(0, className.length() - ".class".length());
        className = className.replace(File.separator, ".");
        InputStream is = new FileInputStream(f);
        byte[] b;
        try {
            ClassReader cr = new ClassReader(is);
            ClassWriter cw = new StaticToolClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES, cl);
            TCCLSetterVisitor cv = new TCCLSetterVisitor(cw, className, wd);
            cr.accept(cv, ClassReader.SKIP_FRAMES);
            if (cv.isWoven()) {
                b = cw.toByteArray();
            } else {
                // if not woven, store the original bytes
                b = Streams.suck(new FileInputStream(f));
            }
        } finally {
            is.close();
        }
        OutputStream os = new FileOutputStream(f);
        try {
            os.write(b);
        } finally {
            os.close();
        }
    }
    if (bundleClassPath != null) {
        for (String entry : bundleClassPath.split(",")) {
            File jarFile = new File(dir, entry.trim());
            if (jarFile.isFile()) {
                weaveBCPJar(jarFile, consumerHeaderKey, consumerHeaderValue);
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) ClassWriter(org.objectweb.asm.ClassWriter) TCCLSetterVisitor(org.apache.aries.spifly.weaver.TCCLSetterVisitor) WeavingData(org.apache.aries.spifly.WeavingData) URLClassLoader(java.net.URLClassLoader) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File)

Aggregations

WeavingData (org.apache.aries.spifly.WeavingData)2 TCCLSetterVisitor (org.apache.aries.spifly.weaver.TCCLSetterVisitor)2 ClassReader (org.objectweb.asm.ClassReader)2 ClassWriter (org.objectweb.asm.ClassWriter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 URLClassLoader (java.net.URLClassLoader)1 JarInputStream (java.util.jar.JarInputStream)1 JarOutputStream (java.util.jar.JarOutputStream)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)1 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)1 Bundle (org.osgi.framework.Bundle)1