Comments (3)
Hi @gauthier-roebroeck-mox,
KlassDeclaration
has a Member called annotations
. When you have the func: KlassDeclaration
you can write func.annotations
to get a list of all annotations. You can then compare the identifier
to check if the given name of the annotation is KafkaListener. When this is the case, you can write
anno.arguments.find { argument ->
argument.identifier.identifierName() == "topics"
}
(import kotlinx.ast.common.klass.identifierName)
Reading the value is not so easy because currently only parsing of top level stuff is implemented. When you have the argument, you can use children
to read the value. in this case, there is only one children with descrption "collectionLiteral" . this has again six children:
0 = {DefaultAstTerminal@4161} DefaultAstTerminal(description=LSQUARE, text=[, channel=AstChannel(id=0, name=DEFAULT_TOKEN_CHANNEL), attachments=AstAttachments(attachments={kotlinx.ast.common.ast.AstAttachmentAstInfo@4944de48= 65 [203..204] [5:18..5:19]}))
1 = {DefaultAstNode@4162} DefaultAstNode(description=expression, children=[DefaultAstNode(description=disjunction, children=[DefaultAstNode(description=conjunction, children=[DefaultAstNode(description=equality, children=[DefaultAstNode(description=comparison, children=[DefaultAstNode(description=genericCallLikeComparison, children=[DefaultAstNode(description=infixOperation, children=[DefaultAstNode(description=elvisExpression, children=[DefaultAstNode(description=infixFunctionCall, children=[DefaultAstNode(description=rangeExpression, children=[DefaultAstNode(description=additiveExpression, children=[DefaultAstNode(description=multiplicativeExpression, children=[DefaultAstNode(description=asExpression, children=[DefaultAstNode(description=prefixUnaryExpression, children=[DefaultAstNode(description=postfixUnaryExpression, children=[DefaultAstNode(description=primaryExpression, children=[DefaultAstNode(description=stringLiteral, children=[DefaultAstNode(description=lineStringLiteral, children=[DefaultAstTerminal
2 = {DefaultAstTerminal@4163} DefaultAstTerminal(description=COMMA, text=,, channel=AstChannel(id=0, name=DEFAULT_TOKEN_CHANNEL), attachments=AstAttachments(attachments={kotlinx.ast.common.ast.AstAttachmentAstInfo@4944de48= 69 [225..226] [5:40..5:41]}))
3 = {DefaultAstTerminal@4164} DefaultAstTerminal(description=Inside_WS, text= , channel=AstChannel(id=1, name=HIDDEN), attachments=AstAttachments(attachments={kotlinx.ast.common.ast.AstAttachmentAstInfo@4944de48= 70 [226..227] [5:41..5:42]}))
4 = {DefaultAstNode@4165} DefaultAstNode(description=expression, children=[DefaultAstNode(description=disjunction, children=[DefaultAstNode(description=conjunction, children=[DefaultAstNode(description=equality, children=[DefaultAstNode(description=comparison, children=[DefaultAstNode(description=genericCallLikeComparison, children=[DefaultAstNode(description=infixOperation, children=[DefaultAstNode(description=elvisExpression, children=[DefaultAstNode(description=infixFunctionCall, children=[DefaultAstNode(description=rangeExpression, children=[DefaultAstNode(description=additiveExpression, children=[DefaultAstNode(description=multiplicativeExpression, children=[DefaultAstNode(description=asExpression, children=[DefaultAstNode(description=prefixUnaryExpression, children=[DefaultAstNode(description=postfixUnaryExpression, children=[DefaultAstNode(description=primaryExpression, children=[DefaultAstNode(description=stringLiteral, children=[DefaultAstNode(description=lineStringLiteral, children=[DefaultAstTerminal
5 = {DefaultAstTerminal@4166} DefaultAstTerminal(description=RSQUARE, text=], channel=AstChannel(id=0, name=DEFAULT_TOKEN_CHANNEL), attachments=AstAttachments(attachments={kotlinx.ast.common.ast.AstAttachmentAstInfo@4944de48= 77 [268..269] [5:83..5:84]}))
you can try to call summary again on all children to get an easier to use ast.
please let me know if you have any questions.
from ast.
The best I could come up with is this, not very pretty:
func.annotations
.mapNotNull { annotation -> annotation.arguments.firstOrNull { it.identifier?.identifier == "topics" } }
.mapNotNull { it.expressions.firstOrNull() }
.flatMap { it.flatten("lineStringContent") }
.flatMap { it.children }
.filter { it.description == "LineStrText" }
.filterIsInstance<DefaultAstTerminal>()
.map { it.text }
Is it possible to use something in kotlinx.ast.common.filter
to make it better ? I couldn't find any doc on those TreeFilter
.
from ast.
Hi @gauthier-roebroeck-mox,
yes, sadly I have only little time to work on this library and I prefer to add new functionality, so there is almost no documentation. TreeFilter is mainly used as an internal API by TreeMapper. This is something like map/flatMap on a AST-Structure. If you are interested in an example:
This Code will convert the annotation from your example, parsed into this ast:
into this summary:
// this call will add a new defintion to the TreeMapper
.convert(
// filter byDescription will select all AST Nodes (and also terminal symbols) with the given description
filter = byDescription("annotation")
// select only AstNodes (a node with possible child nodes), no AstTerminal (a leaf node containing text)
) { node: AstNode ->
// use the same TreeMapper to convert all children of this node
recursiveFlatten(node).flatMap { result ->
// continue the tree mapping,
astContinue(
// replace the node `node` with this KlassAnnotation
KlassAnnotation(
// result the summary of all children nodes,
// filter out all KlassIdentifer
identifier = result.filterIsInstance<KlassIdentifier>(),
// ...and all KlassDeclarations
arguments = result.filterIsInstance<KlassDeclaration>()
)
)
}
}
I hope this helps, feel free to ask me any questions. Sadly, I have no time to add functionality to work in a better way with the ast nodes. In my private project (for which I'm developing this library here) I'm doing code generation (using https://github.com/square/kotlinpoet for writing generated code) and kotlinx.ast for parsing the source. The first step after kotlinx.ast is filtering the annotations I'm interested in (and the annotated subjects), I'm storing this information in a set of data classes. This is very similar to the code you provided here. In the last step, I'm converting this data class into generated code using kotlinpoet.
from ast.
Related Issues (20)
- Add a grammar for Kotlin Script
- org.antlr.v4.kotlinruntime.atn.ATNConfig object has a large number of instance counts, OOM
- Need "kts" scripts support
- How to Visit Each Node of an AST? HOT 2
- KotlinGrammarAntlrKotlinParser very slow for files with json-strings HOT 4
- Consider editing repository setings to remove "Packages" section HOT 2
- I am really confused, which dependency should be used? HOT 5
- End-of-line comments not available in summary HOT 2
- Property delegate not available in summary HOT 2
- Argument with unary prefix is incomplete in summary AST HOT 2
- Best way to add new keywords like in the Spock test HOT 4
- Can I get the specified method code block HOT 3
- Record syntax error messages
- Why column is called row?
- Unsupported ast node functionType when function type used as generic type
- Publish somewhere different than jitpack.io HOT 10
- Is it possible to insert an element? HOT 2
- Add support for @JvmSuppressWildcards/ Annotations for types. HOT 4
- How can I get the specified method code block!Looking forward to answer
- How do I rely on this project on Maven? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ast.