diff options
author | Chris Banes <chrisbanes@google.com> | 2015-04-30 10:31:25 +0100 |
---|---|---|
committer | Chris Banes <chrisbanes@google.com> | 2015-05-04 08:58:09 +0000 |
commit | 34cf9d6e3a6448578438e925384114177de3978a (patch) | |
tree | 25cd22bdf78911e3eb20724548068eb5ae299663 | |
parent | b948ffffae412a12d92b17f1e70d9fee529c0804 (diff) | |
download | doclava-34cf9d6e3a6448578438e925384114177de3978a.tar.gz |
Add wildcard support to stubpackages DO NOT MERGE
Allows things like -stubpackages my.package.* which is
really useful for the support lib API file generation.
The alternative is keeping a list of each library's
package, which is brittle to future changes.
Change-Id: Ibd49cc0348e2767b2c0fe6f14e7253b6d8e04c7b
(cherry picked from commit 372e36e9014ff0d0ff478bd63c374c0068564be1)
-rw-r--r-- | src/com/google/doclava/Stubs.java | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/src/com/google/doclava/Stubs.java b/src/com/google/doclava/Stubs.java index bc5e586..647c921 100644 --- a/src/com/google/doclava/Stubs.java +++ b/src/com/google/doclava/Stubs.java @@ -27,8 +27,10 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.regex.Pattern; public class Stubs { public static void writeStubsAndApi(String stubsDir, String apiFile, String keepListFile, @@ -161,9 +163,10 @@ public class Stubs { // packages contains all the notStrippable classes mapped by their containing packages HashMap<PackageInfo, List<ClassInfo>> packages = new HashMap<PackageInfo, List<ClassInfo>>(); + final HashSet<Pattern> stubPackageWildcards = extractWildcards(stubPackages); for (ClassInfo cl : notStrippable) { if (!cl.isDocOnly()) { - if (stubPackages == null || stubPackages.contains(cl.containingPackage().name())) { + if (shouldWriteStub(cl.containingPackage().name(), stubPackages, stubPackageWildcards)) { // write out the stubs if (stubsDir != null) { writeClassFile(stubsDir, notStrippable, cl); @@ -211,6 +214,46 @@ public class Stubs { } } + private static boolean shouldWriteStub(final String packageName, + final HashSet<String> stubPackages, final HashSet<Pattern> stubPackageWildcards) { + if (stubPackages == null) { + // There aren't any stub packages set, write all stubs + return true; + } + if (stubPackages.contains(packageName)) { + // Stub packages contains package, return true + return true; + } + if (stubPackageWildcards != null) { + // Else, we will iterate through the wildcards to see if there's a match + for (Pattern wildcard : stubPackageWildcards) { + if (wildcard.matcher(packageName).matches()) { + return true; + } + } + } + return false; + } + + private static HashSet<Pattern> extractWildcards(HashSet<String> stubPackages) { + HashSet<Pattern> wildcards = null; + if (stubPackages != null) { + for (Iterator<String> i = stubPackages.iterator(); i.hasNext();) { + final String pkg = i.next(); + if (pkg.indexOf('*') != -1) { + if (wildcards == null) { + wildcards = new HashSet<Pattern>(); + } + // Add the compiled wildcard, replacing * with the regex equivalent + wildcards.add(Pattern.compile(pkg.replace("*", ".*?"))); + // And remove the raw wildcard from the packages + i.remove(); + } + } + } + return wildcards; + } + private static ClassInfo findHiddenClasses(TypeInfo ti) { ClassInfo ci = ti.asClassInfo(); if (ci == null) return null; |