1 package com.android.tools.metalava 2 3 import com.android.tools.metalava.model.PackageItem 4 import java.io.File 5 6 /** 7 * 8 * We see a number of different styles: 9 * - exact match (foo) 10 * - prefix match (foo*, probably not intentional) 11 * - subpackage match (foo.*) 12 * - package and subpackage match (foo:foo.*) 13 * 14 * Real examples: 15 * args: "-stubpackages com.android.test.power ", 16 * args: "-stubpackages android.car* ", 17 * args: "-stubpackages com.android.ahat:com.android.ahat.*", 18 * 19 * Note that doclava does *not* include subpackages by default: -stubpackage foo 20 * will match only foo, not foo.bar. Note also that "foo.*" will not match "foo", 21 * so doclava required you to supply both: "foo:foo.*". 22 * 23 * In metalava we've changed that: it's not likely that you want to 24 * match any subpackage of foo but not foo itself, so foo.* is taken 25 * to mean "foo" and "foo.*". 26 */ 27 class PackageFilter( 28 private val exactPackages: MutableSet<String> = mutableSetOf(), 29 private val packagePrefixes: MutableList<String> = mutableListOf() 30 ) { 31 init { 32 // Wildcards should have been removed by option <lambda>null33 assert(packagePrefixes.none { it.contains("*") }) 34 } 35 matchesnull36 fun matches(qualifiedName: String): Boolean { 37 if (exactPackages.contains(qualifiedName)) { 38 return true 39 } 40 41 if (packagePrefixes.isNotEmpty()) { 42 for (prefix in packagePrefixes) { 43 if (qualifiedName.startsWith(prefix)) { 44 return true 45 } 46 } 47 } 48 49 return false 50 } 51 addPackagesnull52 fun addPackages(path: String) { 53 for (pkg in path.split(File.pathSeparatorChar)) { 54 val index = pkg.indexOf('*') 55 if (index != -1) { 56 if (index < pkg.length - 1) { 57 throw DriverException(stderr = "Wildcards in stub packages must be at the end of the package: $pkg)") 58 } 59 val prefix = pkg.removeSuffix("*") 60 if (prefix.endsWith(".")) { 61 // In doclava, "foo.*" does not match "foo", but we want to do that. 62 exactPackages.add(prefix.substring(0, prefix.length - 1)) 63 } 64 packagePrefixes += prefix 65 } else { 66 exactPackages.add(pkg) 67 } 68 } 69 } 70 matchesnull71 fun matches(packageItem: PackageItem): Boolean { 72 return matches(packageItem.qualifiedName()) 73 } 74 }