There isn’t a built‐in way to “only” visit top-level identifiers (i.e. those not part of a qualifier) via the simple name visitor. In practice, the lint/UAST visitors will give you every simple name (e.g. for each piece of a qualified reference like
println(com.somepackage.SomeObject)
it will call your visitor on com, somepackage, and SomeObject).
So the common workaround is to filter out those names that are actually part of a qualified reference. For example, if you’re implementing a UAST visitor you can:
-
Check the Parent Node:
When you get a USimpleNameReferenceExpression, inspect its parent. Many of the time if the reference is part of a qualified reference (i.e. an expression with a receiver), then its parent will be a UQualifiedReferenceExpression. In that case, you can decide to skip (or further inspect) the node. For instance:
override fun visitSimpleNameReferenceExpression(node: UElement): Boolean {
if (node is USimpleNameReferenceExpression) {
// If the parent is a UQualifiedReferenceExpression and this node is not the "selector"
// then it is part of a package or qualifier.
val parent = node.uastParent
if (parent is UQualifiedReferenceExpression) {
// Depending on your use-case, you might want to only process if this node is the selector.
if (parent.selector != node) {
return super.visitSimpleNameReferenceExpression(node)
}
}
// Now you know that node is either unqualified or it is the selector of a qualified reference.
// In some cases you may want to process only if it is completely unqualified.
// For instance, to only act when the name has no receiver you might do:
if (node.getQualifier() == null) {
// Process your unqualified name (e.g. SomeObject)
}
}
return super.visitSimpleNameReferenceExpression(node)
}
Note that depending on the context the API you use may have helper methods to get the receiver or qualifier. In UAST there isn’t a getQualifier() directly on the simple name reference in all cases, but you can often deduce it from the parent being a qualified expression.
-
Check the Entire Expression:
In some cases you might prefer to visit only UQualifiedReferenceExpression nodes, or even override the visitReferenceExpression method (if available) so you have the whole expression available. Then you can check if the expression has a receiver; if it does, you might decide not to process it for your specific purpose.
In short, while Android Lint (or more generally the UAST visitor infrastructure) doesn’t expose an option to “skip package qualifiers” out of the box, you can easily filter the nodes by checking the structure of the UAST (typically via the parent or the qualified reference itself) to only act on those identifiers that are not part of a package qualifier.
So if you have:
println(SomeObject)
the node for SomeObject will have no receiver or qualifier, and your filter (such as checking that node.getQualifier() == null or that the node’s parent isn’t a composite qualified reference with it as the receiver) will let you know that you can safely process it.
Hope that helps clarify your options!