Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Voxxed Days Vilnius 2015 - Having fun with Javassist

1.099 Aufrufe

Veröffentlicht am

A little bit about Javassist

Veröffentlicht in: Technologie

Voxxed Days Vilnius 2015 - Having fun with Javassist

  1. 1. Having fun with Javassist
  2. 2. whoami Anton Arhipov @antonarhipov
  3. 3. @Entity
 @Table(name  =  "owners")
 public  class  Owner  extends  Person  {
        @Column(name  =  "address")
        @NotEmpty
        private  String  address;
 
        @Column(name  =  "city")
        @NotEmpty
        private  String  city;
 
        @Column(name  =  "telephone")
        @NotEmpty
        @Digits(fraction  =  0,  integer  =  10)
        private  String  telephone;
 
        @OneToMany(cascade  =  CascadeType.ALL,                                  mappedBy  =  "owner")
        private  Set<Pet>  pets;  
  4. 4. public  class  JavassistLazyInitializer                  extends  BasicLazyInitializer                  implements  MethodHandler  {   final  JavassistLazyInitializer  instance              =  new  JavassistLazyInitializer(…);   
 ProxyFactory  factory  =  new  ProxyFactory();
 factory.setSuperclass(interfaces.length  ==  1?persistentClass:null);
 factory.setInterfaces(interfaces);
 factory.setFilter(FINALIZE_FILTER);   
 Class  cl  =  factory.createClass();
 final  HibernateProxy  proxy  =  (HibernateProxy)  cl.newInstance();
 ((ProxyObject)proxy).setHandler(instance);
 instance.constructed  =  true;
 return  proxy;  
  5. 5. public  class  JavassistLazyInitializer                  extends  BasicLazyInitializer                  implements  MethodHandler  {   final  JavassistLazyInitializer  instance              =  new  JavassistLazyInitializer(…);   
 ProxyFactory  factory  =  new  ProxyFactory();
 factory.setSuperclass(interfaces.length  ==  1?persistentClass:null);
 factory.setInterfaces(interfaces);
 factory.setFilter(FINALIZE_FILTER);   
 Class  cl  =  factory.createClass();
 final  HibernateProxy  proxy  =  (HibernateProxy)  cl.newInstance();
 ((ProxyObject)proxy).setHandler(instance);
 instance.constructed  =  true;
 return  proxy;   Generates proxy!
  6. 6. The main use case for bytecode generation in Java framewoks is to generate proxies
  7. 7. Agenda Javassist -javaagent basics … and little bit about о JRebel
  8. 8. Javassist www.javassist.org
  9. 9. CtPool CtClass CtClass CtClass CtClass CtField CtMethod CtConst CtMethod insertBefore insertAfter instrument Almost like Java Reflection API :)
  10. 10. https://github.com/antonarhipov/jpoint Ex1, Ex2, Ex3
  11. 11. Java Agent
  12. 12. Java Agent import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentation inst) throws Exception { inst.addTransformer(new ClassFileTransformer { // here be code }); } } META-INF/MANIFEST.MF Premain-Class: Agent $> java –javaagent:agent.jar application.Main
  13. 13. ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); // here we can do all the things to ‘ct’ return ct.toBytecode(); } }
  14. 14. ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); // here we can do all the things to ‘ct’ return ct.toBytecode(); } }
  15. 15. ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); // here we can do all the things to ‘ct’ return ct.toBytecode(); } }
  16. 16. ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); // here we can do all the things to ‘ct’ return ct.toBytecode(); } }
  17. 17. https://github.com/zeroturnaround/callspy
  18. 18. Javassist in
  19. 19. JRebel core Spring plugin Hibernate plugin EJB plugin
  20. 20. JRebel core Spring plugin Hibernate plugin EJB plugin Reloads classes
  21. 21. JRebel core Spring plugin Hibernate plugin EJB pluginNotifies plugins
  22. 22. JRebel core Spring plugin Hibernate plugin EJB plugin Refresh configurations
  23. 23. JRebel core Spring plugin Hibernate plugin EJB plugin Javassist lives here
  24. 24. JRebel core Spring plugin Hibernate plugin EJB plugin Spring Hibernate OpenEJB
  25. 25. https://github.com/antonarhipov/jpoint HowItWorks
  26. 26. Your task Javassist
  27. 27. @antonarhipov anton@zeroturnaround.com https://speakerdeck.com/antonarhipov http://www.slideshare.net/arhan

×