From 4a538894e38e25ea2674070ef80353315de713bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=B1=89=E5=90=9B?= Date: Mon, 13 Dec 2021 10:39:49 +0800 Subject: [PATCH] change publish info 2 --- app/build.gradle | 2 +- build.gradle | 3 +- hannoplugin/gradle.properties | 2 +- .../java/com/hanking/asmdemo/AsmDemo.java | 176 ++++++++++++++++++ hannotation/build.gradle | 3 +- hannotation/gradle.properties | 2 +- 6 files changed, 183 insertions(+), 5 deletions(-) create mode 100644 hannoplugin/src/main/java/com/hanking/asmdemo/AsmDemo.java diff --git a/app/build.gradle b/app/build.gradle index 513b763..220fd8d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-parcelize' -//apply plugin: 'com.hanking.hanno' +apply plugin: 'com.hanking.hanno' hannoExtension{ enable=true openLog=true diff --git a/build.gradle b/build.gradle index 01da694..edc6661 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,8 @@ buildscript { dependencies { classpath "com.android.tools.build:gradle:4.0.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" -// classpath 'com.hanking.hanno:hannoPlugin:1.0.0' + classpath "com.github.dcendents:android-maven-gradle-plugin:2.0" + classpath 'com.hanking.hanno:hannoPlugin:1.0.0' } } diff --git a/hannoplugin/gradle.properties b/hannoplugin/gradle.properties index 4533009..6c744ed 100644 --- a/hannoplugin/gradle.properties +++ b/hannoplugin/gradle.properties @@ -4,5 +4,5 @@ ARTIFACT_VERSION = 1.0.0 LOCK_ARTIFACT_NAME = true #崩溃自动分配组件维护人 AUTHOR_NAME=huhanjun -AUTHOR_EMAIL=huhanjun@bytedance.com +AUTHOR_EMAIL=huhanjun diff --git a/hannoplugin/src/main/java/com/hanking/asmdemo/AsmDemo.java b/hannoplugin/src/main/java/com/hanking/asmdemo/AsmDemo.java new file mode 100644 index 0000000..3fe4ff6 --- /dev/null +++ b/hannoplugin/src/main/java/com/hanking/asmdemo/AsmDemo.java @@ -0,0 +1,176 @@ +package com.hanking.asmdemo; + +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.commons.AdviceAdapter; +import org.objectweb.asm.commons.Remapper; +import org.objectweb.asm.commons.RemappingMethodAdapter; +import org.objectweb.asm.commons.SimpleRemapper; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.MethodNode; + +import java.util.Iterator; +import java.util.Map; + + +/** + * _ _ _ + * | | | | (_) + * | |__ __ _ _ __ | | ___ _ __ __ _ + * | '_ \ / _` | '_ \| |/ / | '_ \ / _` | + * | | | | (_| | | | | | | | | | (_| | + * |_| |_|\__,_|_| |_|_|\_\_|_| |_|\__, | + * ********************************* __/ | + * ******************************** |___/ + * create time 2021/12/12 12:24 下午 + * create by 胡汉君 + */ +public class AsmDemo { + + public class FieldAdder extends ClassVisitor { + private final FieldNode fn; + + public FieldAdder(ClassVisitor cv, FieldNode fn) { + super(Opcodes.ASM5, cv); + this.fn = fn; + } + + public void visitEnd() { + fn.accept(cv); + super.visitEnd(); + } + } + + public class MethodAdder extends ClassVisitor { + private int mAccess; + private String mName; + private String mDesc; + private String mSignature; + private String[] mExceptions; + + public MethodAdder(ClassVisitor cv, int mthAccess, String mthName, String mthDesc, String mthSignature, String[] mthExceptions) { + super(Opcodes.ASM5, cv); + this.mAccess = mthAccess; + this.mName = mthName; + this.mDesc = mthDesc; + this.mSignature = mthSignature; + this.mExceptions = mthExceptions; + } + + public void visitEnd() { + MethodVisitor mv = cv.visitMethod(mAccess, mName, mDesc, mSignature, mExceptions); + // create method body mv.visitMaxs(0, 0); mv.visitEnd(); super.visitEnd(); + } + } + + public class MethodReplacer extends ClassVisitor { + private String mname; + private String mdesc; + private String cname; + + public MethodReplacer(ClassVisitor cv, String mname, String mdesc) { + super(Opcodes.ASM5, cv); + this.mname = mname; + this.mdesc = mdesc; + } + + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + this.cname = name; + cv.visit(version, access, name, signature, superName, interfaces); + } + + public MethodVisitor visitMethod(int access, + String name, String desc, + String signature, String[] exceptions) { + String newName = name; + if (name.equals(mname) && desc.equals(mdesc)) { + newName = "orig$" + name; + generateNewBody(access, desc, signature, exceptions, name, newName); + } + return super.visitMethod(access, newName, desc, signature, exceptions); + } + + private void generateNewBody(int access, + String desc, String signature, + String[] exceptions, + String name, String newName) { + MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); + // ... + mv.visitCode(); // call original metod mv.visitVarInsn(Opcodes.ALOAD, 0); // this mv.visitMethodInsn(access, cname, newName, desc); + // ... mv.visitEnd(); + } + } + + public class MergeAdapter extends ClassVisitor { + private ClassNode cn; + private String cname; + + public MergeAdapter(ClassVisitor cv, + ClassNode cn) { + super(Opcodes.ASM5, cv); + this.cn = cn; + } + + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + super.visit(version, access, name, signature, superName, interfaces); + this.cname = name; + } + + public void visitEnd() { + for (Iterator it = cn.fields.iterator(); it.hasNext(); ) { + ((FieldNode) it.next()).accept(this); + } + for (Iterator it = cn.methods.iterator(); it.hasNext(); ) { + MethodNode mn = (MethodNode) it.next(); + String[] exceptions = new String[mn.exceptions.size()]; + mn.exceptions.toArray(exceptions); + MethodVisitor mv = cv.visitMethod(mn.access, mn.name, mn.desc, mn.signature, exceptions); + mn.instructions.resetLabels(); + mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new SimpleRemapper(cname, cn.name))); + } + super.visitEnd(); + } + } + + class EnteringAdapter extends AdviceAdapter { + private String name; + private Label timeVarStart = new Label(); + private Label timeVarEnd = new Label(); + + + /** + * Constructs a new {@link AdviceAdapter}. + * + * @param api the ASM API version implemented by this visitor. Must be one of {@link + * Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}. + * @param methodVisitor the method visitor to which this adapter delegates calls. + * @param access the method's access flags (see {@link Opcodes}). + * @param name the method's name. + * @param descriptor the method's descriptor (see {@link Type Type}). + */ + protected EnteringAdapter(int api, MethodVisitor methodVisitor, int access, String name, String descriptor) { + super(api, methodVisitor, access, name, descriptor); + this.name = name; + } + + protected void onMethodEnter() { + visitLabel(timeVarStart); + int timeVar = newLocal(Type.getType("J")); + visitLocalVariable("timeVar", "J", null, timeVarStart, timeVarEnd, timeVar); + super.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava//io // PrintStream;"); + super.visitLdcInsn("Entering " + name); + super.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", + "(Ljava/lang/String;)V"); + } + + public void visitMaxs(int stack, int locals) { + visitLabel(timeVarEnd); + super.visitMaxs(stack, locals); + } + } +} + diff --git a/hannotation/build.gradle b/hannotation/build.gradle index 6ee2692..7f12c4a 100644 --- a/hannotation/build.gradle +++ b/hannotation/build.gradle @@ -1,7 +1,8 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' - +apply plugin: 'com.github.dcendents.android-maven' +group = 'com.github.hankinghu' android { compileSdkVersion 29 buildToolsVersion "29.0.3" diff --git a/hannotation/gradle.properties b/hannotation/gradle.properties index d1d92b0..026481f 100644 --- a/hannotation/gradle.properties +++ b/hannotation/gradle.properties @@ -4,5 +4,5 @@ ARTIFACT_VERSION = 1.0.0 LOCK_ARTIFACT_NAME = true #崩溃自动分配组件维护人 AUTHOR_NAME=huhanjun -AUTHOR_EMAIL=huhanjun@bytedance.com +AUTHOR_EMAIL=huhanjun