OSGi WeavingHook Using Test

140 Views Asked by At

Does anybody actually have any examples of using the OSGi Weaving Hook Service?
I'm doing some test on it .But meet some problem. what I want to do is to modify the bytecode of the class in Bundle. ex: The Bundle A registers the Weaving Hook Service and using the ASM to modify the class in Bundle B.

Bundle A :

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.osgi.framework.hooks.weaving.WeavingHook;
import org.osgi.framework.hooks.weaving.WovenClass;

 @Component
 @Provides
 @Instantiate
 public class TWMethodCallsHook implements WeavingHook {

public void weave(WovenClass wovenClass) {
    // TODO Auto-generated method stub
        if(inInstrumented(wovenClass.getClassName())){
            
            System.out.println("Instrumenting{}");
            System.out.println(wovenClass.getBundleWiring().getBundle().getBundleId());
            final ClassReader cr=new ClassReader(wovenClass.getBytes());
            final ClassWriter cw=new ClassWriter(cr,ClassWriter.COMPUTE_MAXS);
            final ClassVisitor v=new TClassVisitor(cw);
            System.out.println(wovenClass.getBytes());
            cr.accept(v,Opcodes.ASM5);
            byte[]code= cw.toByteArray();
            System.out.println(code.toString());
            wovenClass.setBytes(cw.toByteArray());
        }
}

private boolean inInstrumented(String className) {

    return className.contains("people");
    
}

 }

======================

package per.osgi.weavingHookText.Adapter;

import org.objectweb.asm.ClassVisitor;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class TClassVisitor extends ClassVisitor {

private final ClassVisitor cw;

public TClassVisitor(final ClassVisitor cw) {
    super(Opcodes.ASM5, cw);
    this.cw = cw;
    // TODO Auto-generated constructor stub
}
 @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
     super.visit(version, access, name, signature, name, interfaces);
    }
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    
    if("say".equals(name)){
        
        MethodVisitor mv=cw.visitMethod(access, name, desc, signature, exceptions);
        return new TMethodVisitor(mv);
        
    }
    return null;
}

}

===================================

package per.osgi.weavingHookText.Adapter;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class TMethodVisitor extends MethodVisitor {

public TMethodVisitor(MethodVisitor mv){
    super(Opcodes.ASM5,mv);
}

@Override
public void visitCode(){
    
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn("========start=========");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
    super.visitCode();
}
 }

============================ BUNDLE b

package per.osgi.weavingHookTest.Target;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Target implements BundleActivator {
    //public people a=new people();
public void start(BundleContext context) throws Exception {
    // TODO Auto-generated method stub
    //a.say();
}

public void stop(BundleContext context) throws Exception {
    // TODO Auto-generated method stub
    
}

 }

==========================

package per.osgi.weavingHookTest.Target;

public class people {
public void say(){
    
}
}

================================= I want to insert some code to the Class people.

But Some Error happens. The Bundle B can not start ... I want to know actually what happens...

Actually I don't whether it is possible to insert the code to the another BUNDLE

[The console][1]

[The Error][2]

Instrumenting{}
16
[B@2269ab25
[B@6192f5fb
gogo: BundleException: Error starting module.

Thank you for your help.

When I start the budnle B .SOMEING HAPPENS. [1]: https://i.stack.imgur.com/SRPvH.png [2]: https://i.stack.imgur.com/EnrrD.png

0

There are 0 best solutions below