Repository navigation
[Java] how to extract all field name which type is String ?
#2195
|
hi. I try to use ast-grep to parse some Java code. But it fails me. @Component
class ABC extends Object{
@Resource
private final String with_anno;
private final String with_multi_mod;
public String simple;
}I want to extract all field name which type is rule:
pattern:
selector: field_declaration
context: class A{ String $F;}but it match nothing. it requires a precise rule:
pattern:
selector: field_declaration
context: class A{ private String $F;}this rule will match so my question is: how to extract all field name which type is |
Answered by
HerringtonDarkholme
Aug 24, 2025
Replies: 2 comments 3 replies
|
You can use kind + has |
2 replies
Answer selected by
hxzhao527
|
second question: why this rule not work? rule:
pattern:
selector: field_declaration
context: class A{ $MOD String $F;} |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


You can use kind + has
https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmEiLCJxdWVyeSI6ImAkVEFHYCIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoic21hcnQiLCJzZWxlY3RvciI6IiIsImNvbmZpZyI6InJ1bGU6XG4gIGtpbmQ6IGZpZWxkX2RlY2xhcmF0aW9uXG4gIGhhczpcbiAgICBmaWVsZDogdHlwZVxuICAgIHJlZ2V4OiBeU3RyaW5nJCIsInNvdXJjZSI6IkBDb21wb25lbnRcbmNsYXNzIEFCQyBleHRlbmRzIE9iamVjdHtcbiAgICBAUmVzb3VyY2VcbiAgICBwcml2YXRlIGZpbmFsIFN0cmluZyB3aXRoX2Fubm87XG5cbiAgICBwcml2YXRlIGZpbmFsIFN0cmluZyB3aXRoX211bHRpX21vZDtcblxuICAgIHB1YmxpYyBTdHJpbmcgc2ltcGxlO1xufSJ9