Econ11 weaving

637 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
637
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Econ11 weaving

  1. 1. <ul>These ARE the classes you are looking for! (Byte Code Weaving for fun and profit) </ul><ul>Tom Ware </ul>
  2. 2. You're going to do WHAT to my classes? <ul><li>Look at them before we load them
  3. 3. Change the class structure </li><ul><li>Add/Remove classes
  4. 4. Implement new interfaces, inherit from new classes
  5. 5. Add/Remove inter-type variables and methods </li></ul><li>Change the class behaviour </li></ul><ul><ul><li>Execute logic before and after a piece of code </li><ul><li>e.g. Execute some code before a method executes </li></ul><li>Replace a piece of code </li><ul><li>e.g. get/set of an instance variable </li></ul></ul></ul>
  6. 6. We are not alone <ul><li>Code Instrumentation </li></ul><ul><ul><li>Emma/EclEmma </li></ul></ul><ul><li>AOP </li></ul><ul><ul><li>AspectJ </li></ul></ul><ul><li>Improve performance </li></ul><ul><ul><li>AspectJ/EclipseLink </li></ul></ul><ul><li>Isolate developer from tedious or library-dependant code </li></ul><ul><ul><li>EclipseLink </li></ul></ul><ul><li>OSGi Class Consistency </li></ul><ul><ul><li>Gemini (in development) </li></ul></ul>
  7. 7. Under the Looking Glass (EclipseLink example )
  8. 8. Java Byte Code as a Second Language cv = cw.visitMethod(ACC_PUBLIC, &quot;setId&quot;, &quot;(Ljava/lang/Integer;)V&quot;, null, null); cv.visitVarInsn(ALOAD, 0); cv.visitVarInsn(ALOAD, 1); cv.visitFieldInsn(PUTFIELD, &quot;org/eclipse/persistence/testing/models/jpa/advanced/PhoneNumber&quot;, &quot;id&quot;, &quot;Ljava/lang/Integer;&quot;); cv.visitInsn(RETURN); cv.visitMaxs(2, 2); public void setId(Integer id) { this . id = id; } Java Code ASM Code
  9. 9. Java Byte Code as a Second Language <ul><li>EclipseLink Committer </li></ul><ul><ul><li>ASMifierClassVisitor </li><ul><li>Input: Class File; Output: ASM code for class file </li></ul><li>Eclipse Plug-in ( http://asm.ow2.org/eclipse/index.html )
  10. 10. Decompiler </li></ul></ul><ul><li>User </li></ul><ul><ul><li>eclipselink.weaving.output.path=<path>
  11. 11. Static weaver </li></ul></ul>
  12. 12. The red pill or the blue pill? (ASM version issues) <ul><li>ASM commonly used
  13. 13. Behaviour varies among versions
  14. 14. Need to get predictable version
  15. 15. Repackaged version available as bundle and in eclipselink.jar </li></ul>Bundle-Vendor : Eclipse.org - EclipseLink Project Bundle-SymbolicName : org.eclipse.persistence.asm Bundle-Name : EclipseLink ASM Export-Package : org.eclipse.persistence.internal.libraries.asm;... ...
  16. 16. What do you mean we got it wrong? <ul><li>Changing user-code in a generic way is complex
  17. 17. Other libraries may weave in incompatible ways
  18. 18. Granular on/off for components of weaving </li></ul><ul><ul><li>eclipselink.weaving = true/false
  19. 19. eclipselink.weaving.changetracking = true/false
  20. 20. eclipselink.weaving.internal = true/false
  21. 21. eclipselink.weaving.lazy = true/false
  22. 22. eclipselink.weaving.eager = false/true </li></ul></ul>public class PhoneNumber implements Serializable, Cloneable, PersistenceWeaved , PersistenceEntity , PersistenceObject , FetchGroupTracker, PersistenceWeavedFetchGroups , PersistenceWeavedLazy , ChangeTracker, PersistenceWeavedChangeTracking
  23. 23. Don't load that class yet! <ul><li>Altering byte codes occurs at class-load time
  24. 24. Different deployment scenarios allow this in different ways </li></ul><ul><ul><li>Application specific: e.g. Java EE </li><ul><li>Rules about when classes are loaded </li></ul><li>Java SE </li><ul><li>-javaagent </li></ul><li>OSGi </li><ul><li>Equinox hooks
  25. 25. OSGi Core Spec version 4.3 </li></ul><li>Preprocessing </li><ul><li>EclipseLink provides and ant task to produce weaved classes </li></ul></ul></ul>
  26. 26. Down the Rabbit Hole (Equinox Example)

×