From 372e36e9014ff0d0ff478bd63c374c0068564be1 Mon Sep 17 00:00:00 2001 From: Chris Banes Date: Thu, 30 Apr 2015 10:31:25 +0100 Subject: Add wildcard support to stubpackages 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 --- src/com/google/doclava/Stubs.java | 45 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/com/google/doclava/Stubs.java b/src/com/google/doclava/Stubs.java index 44f1277..5ae1dd0 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> packages = new HashMap>(); + final HashSet 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 stubPackages, final HashSet 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 extractWildcards(HashSet stubPackages) { + HashSet wildcards = null; + if (stubPackages != null) { + for (Iterator i = stubPackages.iterator(); i.hasNext();) { + final String pkg = i.next(); + if (pkg.indexOf('*') != -1) { + if (wildcards == null) { + wildcards = new HashSet(); + } + // 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; -- cgit v1.2.3