the craft of meta programming on jvm

Download The craft of meta programming on JVM

If you can't read please download the document

Upload: igor-khotin

Post on 04-Aug-2015

212 views

Category:

Software


2 download

TRANSCRIPT

1. The Craft of Metaprogramming on JVM Igor Khotin E-mail: [email protected] 2. Background 15+ years in the IT industry 10+ years with Java Flexible design promoter After deploymentDeploying... 3. Meta Tricks Code 4. MetaData 5. MetaCode 6. Translators Domain-Specific Languages Code Instrumentation Preprocessors 7. Translators Domain-Specific Languages Code Instrumentation Preprocessors 8. LISP (defmacro setq-reversible (e1 e2 d) (case d (:normal (list 'setq e1 e2)) (:backward (list 'setq e2 e1)) (t (error ...)))) 9. bash `backquotes` 10. JavaScript eval() 11. Java Motherf***er! Do you code it? 12. ClassLoader 13. Changing the bytecode 14. Just a hack 15. Code that live by itself 16. Backbone for many products 17. Boilerplate 18. Meta Tricks Code 19. Annotations Providing metadata on code since Java 1.5 20. Reflection Know your structure 21. Scannotation Reflections Scan and metadata 22. and now the bytecode time!!! 23. Compile-time Load-time Run-time Time? 24. Java Agent Ability to modify bytecode Not affecting the source Transformation handlers Special app startup process Since Java 1.5 25. Java Debugging Interface Limited ability to reload classes 26. ASM Event-based visitors API Object-based model API Fast Anything is possible 27. Byte Code Engineering Library Object-model API Good for static code analysis used by findbugs BCEL 28. Java Assistanct JBoss subproject High-level bytecode manipulation Includes simple Java compiler JAssist 29. Java Assistanct JBoss subproject High-level bytecode manipulation Includes simple Java compiler Simple Slower than ASM Limited capabilities compared to ASM JAssist 30. Conceptually high-level Manages cross-cutting concerns Uses ASM for bytecode-level manipulation AspectJ 31. Introduce Interface (or Superclass) Change visibility Add new method Add new field Replace method body Merge two classes into one Class Transformations 32. Insert code before/after Replace method call Replace field access Inline method Method Transformations 33. @Before @After @Around Aspects 34. Proxy and Lazy 35. Meta Tricks Code 36. @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Command { String value(); } Command 37. @Command("hi") public class Hi { public void exec() { System.out.println(this.getClass().getName()); System.out.println("Hello annotations!"); } } Hi 38. Reflections reflections = new Reflections("meta"); Set> annotated = reflections .getTypesAnnotatedWith(Command.class); for(Class c: annotated) { Command ca = (Command)c.getAnnotation( Command.class); System.out.println("[" + c.getSimpleName() + "] -> " + ca.value()); } Reflections 39. Class commandClass = map.get(cmd); Object command = commandClass.newInstance(); Method exec = command.getClass().getMethod(exec); exec.invoke(command); Reflection 40. public Class enhance(Class target) { ClassPool cp = new ClassPool(true); CtClass ctSuper = cp.get(target.getName()); CtClass ctClass = cp.makeClass("meta.InfectedClass"); ctClass.setSuperclass(ctSuper); CtMethod execMethod = CtNewMethod.make( "public void exec() {System.out.print("[ENHANCED] ");super.exec();}", ctClass); ctClass.addMethod(execMethod); ctClass.writeFile(); return ctClass.toClass(); } JAssist 41. Eval.me( '2 + 2' ) Groovy eval() 42. def list = ['Java', 'Groovy', 'Scala'] LanguageList() { def mc = new ExpandoMetaClass(LanguageList, false, true) mc.initialize() this.metaClass = mc } def methodMissing(String name, args) { // Intercept method that starts with find. if (name.startsWith("find")) { def result = list.find { it == name[4..-1] } // Add new method to class with metaClass. this.metaClass."$name" = {-> result + "[cache]" } result } else { throw new MissingMethodException( name, this.class, args) } } Groovy missing method 43. Questions? Igor Khotin E-mail: [email protected] Blog: www.ikhotin.com Twitter: chaostarter