|
| 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