Skip to content

Instantly share code, notes, and snippets.

@catharinejm
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save catharinejm/b1228a5ecc58fd66375c to your computer and use it in GitHub Desktop.

Select an option

Save catharinejm/b1228a5ecc58fd66375c to your computer and use it in GitHub Desktop.
(ns asm-test.test-class
(:import [clojure.asm ClassWriter ClassVisitor Opcodes Type]
[clojure.asm.commons GeneratorAdapter Method]
[clojure.lang DynamicClassLoader Compiler RT]))
(def cw (ClassWriter. ClassWriter/COMPUTE_MAXS))
(. cw (visit Opcodes/V1_5 Opcodes/ACC_PUBLIC
"asm_test/test_class/TestClass" nil "java/lang/Object" (make-array String 0)))
(let [m (Method. "doit" Type/VOID_TYPE (into-array Type [(Type/getType String)]))
gen (GeneratorAdapter. (bit-or Opcodes/ACC_PUBLIC Opcodes/ACC_STATIC)
m nil nil cw)]
(.visitCode gen)
(. gen push "clojure.core")
(. gen push "*out*")
(. gen invokeStatic (Type/getType clojure.lang.RT) (. Method getMethod "clojure.lang.Var var(String,String)"))
(. gen invokeVirtual (Type/getType clojure.lang.Var) (. Method getMethod "java.lang.Object get()"))
(. gen dup)
(. gen checkCast (Type/getType java.io.PrintWriter))
(. gen loadArg 0)
(. gen invokeVirtual (Type/getType java.io.PrintWriter) (. Method getMethod "void println(String)"))
(.returnValue gen)
(.endMethod gen))
(.visitEnd cw)
(. @Compiler/LOADER (defineClass "asm_test.test_class.TestClass" (.toByteArray cw) nil))
(import 'asm_test.test_class.TestClass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment