当前位置:网站首页>[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
2022-07-06 00:35:00 【Programmer community】
List of articles
- One 、 stay MyASTTransformation#visit Method to intercept
- 1、 obtain ClassNode Node collection
- 2、 Find the specified ClassNode node
- 3、 Get specified ClassNode Node under MethodNode Node collection
- 4、 Find the specified MethodNode node
One 、 stay MyASTTransformation#visit Method to intercept
stay ASTTransformation Interface implementation class
void visit(ASTNode[] nodes, SourceUnit source) In the method , among ASTNode[] nodes Parameter is AST Syntax tree root node array , Each array element is a ModuleNode Corresponding to one Groovy Script ;
SourceUnit source Is the source unit , You can get the source file through this object ;
source.AST It's a single ModuleNode object , Corresponding to one Groovy Script ;
1、 obtain ClassNode Node collection
source.AST.classes It's just one. Groovy The array of class nodes defined in the script ; This is ModuleNode Medium ClassNode Class nodes are encapsulated in List<ClassNode> classes = new LinkedList<ClassNode>(); Among members ;
2、 Find the specified ClassNode node
Use
source.AST.classes.find {
// The search name is Student Class // it yes ClassNode node it.name == "Student" }Code , You can find the name “Student” Of ClassNode node , That is to say Student Class corresponding node ;
A collection of find The prototype of the method is as follows , What you get is a collection element object ; This method returns the first set element in the set that matches the closure condition ;
/** * Find the first value that matches the closure condition . Example : * <pre class="groovyTestCase">def list = [1,2,3] * assert 2 == list.find { it {@code >} 1 } * </pre> * * @param self a Collection * @param closure a closure condition * @return the first Object found, in the order of the collections iterator, or null if no element matches * @since 1.0 */ public static <T> T find(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure); for (T value : self) {
if (bcw.call(value)) {
return value; } } return null; }3、 Get specified ClassNode Node under MethodNode Node collection
Further , If you get ClassNode Node is not empty , Get MethodNode Node collection , Use ?.methods Code acquisition ,
source.AST.classes.find {
// The search name is Student Class // it yes ClassNode node it.name == "Student" }?.methodsClassNode Of getMethods The prototype of the method is as follows :
public class ClassNode extends AnnotatedNode implements Opcodes {
/** * @return Methods related to this {@code ClassNode} */ public List<MethodNode> getMethods() {
if (redirect != null) return redirect.getMethods(); lazyClassInit(); return methodsList; }}4、 Find the specified MethodNode node
lookup List<MethodNode> Collection , The name is “hello” The node of , That is to find Student Class hello Method corresponding to MethodNode node ;
source.AST.classes.find {
// The search name is Student Class // it yes ClassNode node it.name == "Student" }?.methods?.find {
// lookup Student The name under the class is hello Methods // it yes MethodNode node it.name == "hello" }边栏推荐
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- MDK debug时设置数据实时更新
- Knowledge about the memory size occupied by the structure
- 如何利用Flutter框架开发运行小程序
- Model analysis of establishment time and holding time
- LeetCode 斐波那契序列
- LeetCode 6005. The minimum operand to make an array an alternating array
- CTF daily question day44 rot
- Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
- Folding and sinking sand -- weekly record of ETF
猜你喜欢

Leetcode:20220213 week race (less bugs, top 10% 555)

2022-02-13 work record -- PHP parsing rich text

FFT learning notes (I think it is detailed)

如何制作自己的机器人

【DesignMode】装饰者模式(Decorator pattern)

剖面测量之提取剖面数据

Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike

Ffmpeg learning - core module
![Atcoder beginer contest 258 [competition record]](/img/e4/1d34410f79851a7a81dd8f4a0b54bf.gif)
Atcoder beginer contest 258 [competition record]

Problems and solutions of converting date into specified string in date class
随机推荐
Arduino六足机器人
SQLServer连接数据库读取中文乱码问题解决
MySQL functions
AtCoder Beginner Contest 258【比赛记录】
notepad++正則錶達式替換字符串
看抖音直播Beyond演唱会有感
MySql——CRUD
2022.7.5-----leetcode. seven hundred and twenty-nine
MYSQL GROUP_ The concat function realizes the content merging of the same ID
Go learning - dependency injection
Spark SQL null value, Nan judgment and processing
Spark SQL空值Null,NaN判断和处理
About the slmgr command
Folding and sinking sand -- weekly record of ETF
Atcoder beginer contest 258 [competition record]
Extension and application of timestamp
How to make your own robot
Spark-SQL UDF函数
数据分析思维分析方法和业务知识——分析方法(二)
MySql——CRUD