Created
March 10, 2022 23:09
-
-
Save camsaul/f079d5b8d3c33c13454e4e9e26b66d38 to your computer and use it in GitHub Desktop.
ASM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns asm | |
(:import liquibase.database.core.H2Database | |
liquibase.database.jvm.JdbcConnection | |
[org.objectweb.asm Opcodes ClassWriter Type] | |
[org.objectweb.asm.commons Method GeneratorAdapter])) | |
(defn x ^ClassWriter [] | |
(let [flags 0 | |
classwriter (org.objectweb.asm.ClassWriter. flags)] | |
;; define the Class | |
(.visit | |
classwriter | |
Opcodes/V1_8 | |
Opcodes/ACC_PUBLIC | |
"metabase/db/liquibase/h2/H2Database" | |
nil | |
"liquibase/database/core/H2Database" | |
(into-array String [])) | |
;; define the constructor H2Database() | |
(let [method (Method/getMethod "void <init> ()") | |
generator (GeneratorAdapter. Opcodes/ACC_PUBLIC method nil nil classwriter)] | |
(doto generator | |
.loadThis | |
(.invokeConstructor (Type/getType H2Database) method) | |
.returnValue | |
.endMethod)) | |
;; define boolean mustQuoteObjectName(String objectName, String objectType) implementation | |
(let [method (Method/getMethod "boolean mustQuoteObjectName(")]) | |
;; finally, return the classwriter so we can convert it to bytes | |
classwriter)) | |
(defn y ^bytes [] | |
(.toByteArray (x))) | |
(defn z ^Class [] | |
(.defineClass (classloader/the-classloader) | |
"metabase.db.liquibase.h2.H2Database" | |
(y) | |
nil)) | |
(defn disassemble [^bytes bytecode] | |
(.disassemble (org.eclipse.jdt.internal.core.util.Disassembler.) bytecode "\n" 1)) | |
(defn a [] | |
(println (disassemble (y)))) |
Author
camsaul
commented
Mar 10, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment