-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dengshiwei
committed
Sep 22, 2020
1 parent
dc77d86
commit e503aee
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
11 changes: 11 additions & 0 deletions
11
asm_example/src/main/java/com/andoter/asm_example/part10/树 API 向后兼容
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
### 10.2 规则 | ||
首先,如果使用树 API 编写一个类生成器,那就不需要遵循什么规则(和核心 API 一样)。可以用任意构造器创建 ClassNode 和其他元素,可以使用这些类的任意方法。 | ||
另一方面,如果要用树 API 编写类分析器或类适配器,也就是说,如果使用 ClassNode 或其他直接或间接地通过 ClassReader.accept()填充的类似类,或者如果重写这些类中的 | ||
一个,则必须遵循下面给出的规则。 | ||
|
||
#### 规则 1 | ||
要用 ASM 版本 X 的树 API 编写类分析器或适配器,则使用以这一确切版本为参数的构造器创建 ClassNode(而不是使用没有参数的默认构造器)。 | ||
|
||
#### 规则 2 | ||
要用 ASM 版本 X 的树 API 编写一个类分析器或适配器,使用别人创建的 ClassNode,在以任何方式使用这个 ClassNode 之前,都要以这个确切版本号为参数,调用 | ||
它的 check()方法。 |
27 changes: 27 additions & 0 deletions
27
asm_example/src/main/java/com/andoter/asm_example/part8/CyclomaticComplexity.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.andoter.asm_example.part8 | ||
|
||
import org.objectweb.asm.tree.MethodNode | ||
import org.objectweb.asm.tree.analysis.* | ||
import java.util.* | ||
|
||
|
||
class CyclomaticComplexity { | ||
|
||
fun getCyclomaticComplexity(owner: String, mn: MethodNode) { | ||
val analyzer = object :Analyzer<BasicValue>(BasicInterpreter()) { | ||
override fun newFrame(numLocals: Int, numStack: Int): Frame<BasicValue> { | ||
return Node<BasicValue>(numLocals, numStack) | ||
} | ||
|
||
override fun newControlFlowEdge(insnIndex: Int, successorIndex: Int) { | ||
val node = frames[insnIndex] as Node<BasicValue> | ||
node.successors.plus(frames[successorIndex] as Node<BasicValue>) | ||
super.newControlFlowEdge(insnIndex, successorIndex) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class Node<V : Value>(numLocals: Int, numStack: Int) : Frame<V>(numLocals, numStack) { | ||
var successors: Set<Node<V>> = mutableSetOf() | ||
} |
39 changes: 39 additions & 0 deletions
39
asm_example/src/main/java/com/andoter/asm_example/part9/AnnotationNodeAdapter.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.andoter.asm_example.part9 | ||
|
||
import com.andoter.asm_example.utils.ADLog | ||
import org.objectweb.asm.AnnotationVisitor | ||
import org.objectweb.asm.ClassReader | ||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.Opcodes | ||
import org.objectweb.asm.tree.AnnotationNode | ||
|
||
/** | ||
* 在树 API 中提供了对应的 AnnotationNode 类用于处理注解,该类继承 AnnotationVisitor | ||
*/ | ||
|
||
@Test("Adapter") | ||
class AnnotationNodeAdapter { | ||
|
||
} | ||
|
||
annotation class Test(val value: String) | ||
|
||
fun main() { | ||
val classReader = ClassReader("com.andoter.asm_example.part9.AnnotationNodeAdapter") | ||
classReader.accept(object :ClassVisitor(Opcodes.ASM7){ | ||
override fun visitAnnotation(descriptor: String?, visible: Boolean): AnnotationVisitor { | ||
return object :AnnotationNode(Opcodes.ASM6, descriptor) { | ||
override fun visit(name: String?, value: Any?) { | ||
super.visit(name, value) | ||
ADLog.info("visit, name = $name, value = $value") | ||
} | ||
|
||
override fun visitEnd() { | ||
ADLog.info("visitEnd") | ||
super.visitEnd() | ||
} | ||
} | ||
} | ||
}, ClassReader.SKIP_DEBUG) | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
asm_example/src/main/java/com/andoter/asm_example/part9/SignatureVisitorAdapter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.andoter.asm_example.part9; | ||
|
||
/** | ||
* 在树 API 中没有提供与 SignatureVisitor 对应的 SignatureNode | ||
*/ | ||
class SignatureVisitorAdapter { | ||
} |