OSGi WeavingHook使用测试

时间:2017-03-09 06:23:43

标签: java apache osgi apache-felix ipojo

有没有人真的有使用OSGi编织钩服务的任何例子? 我正在做一些测试。但是遇到了一些问题。我想要做的是修改Bundle中类的字节码。例如:Bundle A注册Weaving Hook Service并使用ASM修改Bundle B中的类。

捆绑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(){

}
}

================================= 我想向Class人员插入一些代码。

但是发生了一些错误。 Bundle B无法启动...... 我想知道究竟会发生什么......

其实我不知道是否可以将代码插入另一个BUNDLE

The console

The Error

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

感谢您的帮助。

当我开始使用B时。发生了什么事。

0 个答案:

没有答案