当前位置:网站首页>[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" }边栏推荐
- AtCoder Beginner Contest 254【VP记录】
- The relationship between FPGA internal hardware structure and code
- NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
- 孤勇者
- What is information security? What is included? What is the difference with network security?
- Atcoder beginer contest 258 [competition record]
- LeetCode 6004. Get operands of 0
- LeetCode 8. String conversion integer (ATOI)
- Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
- [QT] QT uses qjson to generate JSON files and save them
猜你喜欢

图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
![[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)](/img/39/9d189a18f3f75110b400506e274391.png)
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)

【DesignMode】组合模式(composite mode)

LeetCode 1189. Maximum number of "balloons"

Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code

多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)

数据分析思维分析方法和业务知识——分析方法(三)

uniapp开发,打包成H5部署到服务器

猿桌派第三季开播在即,打开出海浪潮下的开发者新视野

Model analysis of establishment time and holding time
随机推荐
Room cannot create an SQLite connection to verify the queries
Browser reflow and redraw
Leetcode:20220213 week race (less bugs, top 10% 555)
Spark DF增加一列
Comment faire votre propre robot
Key structure of ffmpeg - avframe
[QT] QT uses qjson to generate JSON files and save them
Spark SQL null value, Nan judgment and processing
How spark gets columns in dataframe --column, $, column, apply
JS can really prohibit constant modification this time!
NLP generation model 2017: Why are those in transformer
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
常用API类及异常体系
孤勇者
Yolov5, pychar, Anaconda environment installation
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
Atcoder beginer contest 254 [VP record]
Knowledge about the memory size occupied by the structure
uniapp开发,打包成H5部署到服务器