Skip to content

Commit 12f89cb

Browse files
committed
Add importing-shaded-package.ql
1 parent a2bd29a commit 12f89cb

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Finds import statements which import a package containing apparently classes
3+
* shaded / repackaged from another artifact. Such shaded classes are often
4+
* part of the implementation details and should not be used directly. Instead
5+
* the official classes should be used.
6+
*
7+
* For example:
8+
* ```java
9+
* import org.example.shaded.com.google.common.base.Strings;
10+
* // should instead import the official class:
11+
* import com.google.common.base.Strings;
12+
* ```
13+
*
14+
* @kind problem
15+
*/
16+
17+
// Related to own `using-class-from-internal-package.ql` query
18+
19+
import java
20+
21+
Package getPackage(Import importStmt) {
22+
result =
23+
[
24+
importStmt.(ImportOnDemandFromPackage).getPackageHoldingImport(),
25+
importStmt.(ImportOnDemandFromType).getTypeHoldingImport().getPackage(),
26+
importStmt.(ImportStaticOnDemand).getTypeHoldingImport().getPackage(),
27+
importStmt.(ImportStaticTypeMember).getTypeHoldingImport().getPackage(),
28+
importStmt.(ImportType).getImportedType().getPackage(),
29+
]
30+
}
31+
32+
// Only consider import statements; ignore if fully qualified name is used within class
33+
from Import importStmt, Package package, string shadedPackagePrefix
34+
where
35+
package = getPackage(importStmt) and
36+
exists(string packageName, int index |
37+
packageName = package.getName() and
38+
exists(packageName.regexpFind("(^|\\.)(shaded|repackaged)($|\\.)", 0, index)) and
39+
shadedPackagePrefix = packageName.prefix(index)
40+
) and
41+
// And shaded classes are not somehow part of the sources
42+
not package.fromSource() and
43+
// And package is not shaded by the project itself, i.e. shaded package has same prefix
44+
// as other classes of project
45+
not exists(Package p | p.getName() = shadedPackagePrefix and p.fromSource())
46+
select importStmt, "Imports shaded package"

0 commit comments

Comments
 (0)