-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Java] Can't match imported annotations #1508
Comments
This is based of the example by @minusworld, theirs works: Mine doesn't: Only difference is the import statement |
Using full package name doesn't help either: |
Thanks for the great bug report, @nightwatchcyber. Cc @aryx who I suspect has the most context here |
There's 2 bugs here. First, annotations with qualified names are not correctly represented in the generic AST |
This will help semgrep/semgrep#1508 test plan: See NamedAttr below which now contains ["Foo",();"Bar",()] instead of a OA_AnnotOther + /home/pad/pfff/pfff -dump_ast misc_import_resolve.java [(DirectiveStmt (ImportFrom ((), (DottedName [("foo", ())]), ("Bar", ()), None))); (DefStmt ({ name = ("Foo", ()); attrs = []; info = { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }; tparams = [] }, (ClassDef { ckind = (Class, ()); cextends = []; cimplements = []; cmixins = []; cbody = ((), [(FieldStmt (DefStmt ({ name = ("foo", ()); attrs = [(NamedAttr ((), [("Misc", ())], { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }, ((), [], ()))); (NamedAttr ((), [("Foo", ()); ("Bar", ())], { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }, ((), [(ArgKwd (("method", ()), (L (String ("foobar", ()))))) ], ()) )); (KeywordAttr (Public, ()))]; info = { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }; tparams = [] }, (FuncDef { fparams = [(ParamClassic { pname = (Some ("x", ())); ptype = (Some (TyBuiltin ("int", ()))); pdefault = None; pattrs = []; pinfo = { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) } }) ]; frettype = (Some (TyBuiltin ("int", ()))); fbody = (Block ((), [(Return ((), (Some (Id (("x", ()), { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) } ))) )) ], ())) })))) ], ()) }))) ]
This will help semgrep/semgrep#1508 test plan: See NamedAttr below which now contains ["Foo",();"Bar",()] instead of a OA_AnnotOther + /home/pad/pfff/pfff -dump_ast misc_import_resolve.java [(DirectiveStmt (ImportFrom ((), (DottedName [("foo", ())]), ("Bar", ()), None))); (DefStmt ({ name = ("Foo", ()); attrs = []; info = { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }; tparams = [] }, (ClassDef { ckind = (Class, ()); cextends = []; cimplements = []; cmixins = []; cbody = ((), [(FieldStmt (DefStmt ({ name = ("foo", ()); attrs = [(NamedAttr ((), [("Misc", ())], { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }, ((), [], ()))); (NamedAttr ((), [("Foo", ()); ("Bar", ())], { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }, ((), [(ArgKwd (("method", ()), (L (String ("foobar", ()))))) ], ()) )); (KeywordAttr (Public, ()))]; info = { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) }; tparams = [] }, (FuncDef { fparams = [(ParamClassic { pname = (Some ("x", ())); ptype = (Some (TyBuiltin ("int", ()))); pdefault = None; pattrs = []; pinfo = { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) } }) ]; frettype = (Some (TyBuiltin ("int", ()))); fbody = (Block ((), [(Return ((), (Some (Id (("x", ()), { id_resolved = ref (None); id_type = ref (None); id_const_literal = ref (None) } ))) )) ], ())) })))) ], ()) }))) ]
cf. https://semgrep.dev/jxwN/, which should be a working test case even if we don't change the fqdn resolution behavior (the latter seems more like a design decision we should revisit than a clear-cut bug, @aryx ?). |
Here's the AST of the code annotation:
but the pattern annotation is parsed as:
|
Yep, I'm almost done fixing it. |
This gets closer to the annotation type in AST generic. This is necessary for semgrep/semgrep#1508 otherwise we get regressions on python semgrep tests. test plan: make
This gets closer to the annotation type in AST generic. This is necessary for semgrep/semgrep#1508 otherwise we get regressions on python semgrep tests. test plan: make
This should fix #1508 Test plan: test file included
This should fix #1508 Test plan: test file included
Describe the bug
Matching stops working in Java when "import" is used
To Reproduce
https://semgrep.dev/RrPN
Expected behavior
Matching should work even if import statements exist
Screenshots
If applicable, add screenshots to help explain your problem.
Environment
semgrev.dev and MacOS
The text was updated successfully, but these errors were encountered: