Java – OSGi weavinghook example

Is there an example of using OSGi 4.3 woven hook service? What about AspectJ, ASM and javaassist? Does anyone really use OSGi weavinghooks?

OSGi Core 5.0. The example in section 56.2 simply omits the actual weaving and says "the final weaving is left to the reader as an exercise"

My goal is:

>Create a comment (@ myannotation) that I can put on a field (primitive or object). > Create an org osgi. framework. hooks. weaving. Weavinghook write a class with this annotation > use load time weaving to switch any modification of the field with this annotation > fire eventadmin event, the field is modified. > Dynamic update from weavinghook bundle to eventadmin bundle

My main problem is #3

I'm trying to weave using AspectJ weavingadaptor, but because we expect Java. Java in the constructor net. URL [] aspecturl can be jars or directory. It can be found on the file system, not bundled In addition, I don't know how to handle any new classes generated by the compiler by calling back to the acceptaptclass (string name, bytes []) method of the generatedclasshandler

Maybe the weaving adaptor is not the right place to start my weaving? Or maybe I shouldn't use AspectJ?

MyAnnotation. java

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}

MyWeavingHook. java

public class MyWeavingHook implements WeavingHook {

    public class MyWeavingClassloader implements WeavingClassLoader {

        private Bundle b;

        public MyWeavingClassLoader(Bundle b) {
            this.b = b;
        }

        void acceptClass(java.lang.String name,byte[] bytes) {
            //no way to get this back into the woven classes bundle classloader?
        } 

        URL[] getAspectURLs() {
            //how do I get a handle to my aspect library that AspectJ can understand?
        }
    }

    public void weave(WovenClass myclass) {
        Bundle b = Framework.getBundle(MyWeavingHook.class);
        WeavingClassLoader wc = new WeavingClassLoader(b);
        WeavingAdaptor w = new WeavingAdaptor(wc);
        if (shouldweave(myclass))
          myclass.setBytes(w.weave(myClass.getBytes()));
        //should catch exceptions
    }

    private boolean shouldweave(WovenClass myclass) {
        //not sure of the best logic to pick which classes to weave yet
    }
}

MyAspect. aj

privileged aspect MyAspect {
    after() : set(* *) && @annotation(MyAnnotation) {
      //send EventAdmin event
    }
}

MyTestClass. java

public class MyTestClass {
    @MyAnnotation
    private int myField;

    public void doSomething() {
      //do stuff with myField
    }
}

I can use spring AOP, but I hope this can be used for any bundle, not just beans instantiated through spring or blueprint In addition, equinox braiding doesn't seem to use OSGi braiding hook specification, and I don't want to be bound to equinox If other jobs work better, I have no problem

Refer to a similar question: is it possible to do bytecode manipulation when using OSGi?

to update:

The end result is that I just used equinox aspects and installed it into karaf There are 3 bundles, a library and a system attribute I will use it until they are updated to our OSGi weaving, or I write my own OSGi weaving hook to use AspectJ code similar to equinox aspects I don't like the braid indicator needed to make equinox aspects work because it introduces requirements bundling / re exporting or importing packages on AspectJ RT in the package to be braided This dependency should be dynamically added and suggested outside the bundle

Solution

Take a look at proxyweavinghook.com in the proxy module of Apache Aries It directly uses ASM library to modify bytecode to lower level

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>