< prev index next >

src/jdk.jdeps/share/classes/com/sun/tools/jdeps/JdepsFilter.java

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.tools.jdeps;
  26 
  27 import com.sun.tools.classfile.Dependencies;
  28 import com.sun.tools.classfile.Dependency;
  29 import com.sun.tools.classfile.Dependency.Location;
  30 
  31 import java.util.HashSet;
  32 import java.util.Optional;
  33 import java.util.Set;
  34 import java.util.regex.Pattern;
  35 import java.util.stream.Collectors;
  36 import java.util.stream.Stream;
  37 
  38 /*
  39  * Filter configured based on the input jdeps option
  40  * 1. -p and -regex to match target dependencies
  41  * 2. -filter:package to filter out same-package dependencies
  42  *    This filter is applied when jdeps parses the class files
  43  *    and filtered dependencies are not stored in the Analyzer.
  44  * 3. --require specifies to match target dependence from the given module
  45  *    This gets expanded into package lists to be filtered.
  46  * 4. -filter:archive to filter out same-archive dependencies
  47  *    This filter is applied later in the Analyzer as the
  48  *    containing archive of a target class may not be known until
  49  *    the entire archive
  50  */
  51 public class JdepsFilter implements Dependency.Filter, Analyzer.Filter {
  52 
  53     public static final JdepsFilter DEFAULT_FILTER =
  54         new JdepsFilter.Builder().filter(true, true).build();
  55 
  56     private final Dependency.Filter filter;
  57     private final Pattern filterPattern;
  58     private final boolean filterSamePackage;
  59     private final boolean filterSameArchive;
  60     private final boolean findJDKInternals;
  61     private final Pattern includePattern;
  62     private final Pattern includeSystemModules;
  63 
  64     private final Set<String> requires;
  65 
  66     private JdepsFilter(Dependency.Filter filter,
  67                         Pattern filterPattern,
  68                         boolean filterSamePackage,
  69                         boolean filterSameArchive,
  70                         boolean findJDKInternals,
  71                         Pattern includePattern,
  72                         Pattern includeSystemModules,
  73                         Set<String> requires) {
  74         this.filter = filter;
  75         this.filterPattern = filterPattern;
  76         this.filterSamePackage = filterSamePackage;
  77         this.filterSameArchive = filterSameArchive;
  78         this.findJDKInternals = findJDKInternals;
  79         this.includePattern = includePattern;
  80         this.includeSystemModules = includeSystemModules;
  81         this.requires = requires;
  82     }
  83 
  84     /**
  85      * Tests if the given class matches the pattern given in the -include option
  86      *
  87      * @param cn fully-qualified name
  88      */
  89     public boolean matches(String cn) {
  90         if (includePattern == null)
  91             return true;
  92 
  93         if (includePattern != null)
  94             return includePattern.matcher(cn).matches();
  95 
  96         return false;
  97     }
  98 
  99     /**
 100      * Tests if the given source includes classes specified in -include option
 101      *
 102      * This method can be used to determine if the given source should eagerly
 103      * be processed.
 104      */
 105     public boolean matches(Archive source) {
 106         if (includePattern != null) {
 107             return source.reader().entries().stream()
 108                     .map(name -> name.replace('/', '.'))
 109                     .filter(name -> !name.equals("module-info.class"))
 110                     .anyMatch(this::matches);
 111         }
 112         return hasTargetFilter();
 113     }
 114 
 115     public boolean include(Archive source) {
 116         Module module = source.getModule();
 117         // skip system module by default; or if includeSystemModules is set
 118         // only include the ones matching the pattern
 119         return  !module.isSystem() || (includeSystemModules != null &&
 120             includeSystemModules.matcher(module.name()).matches());
 121     }
 122 
 123     public boolean hasIncludePattern() {
 124         return includePattern != null || includeSystemModules != null;
 125     }
 126 
 127     public boolean hasTargetFilter() {
 128         return filter != null;
 129     }
 130 
 131     public Set<String> requiresFilter() {
 132         return requires;
 133     }
 134 
 135     // ----- Dependency.Filter -----
 136 
 137     @Override
 138     public boolean accepts(Dependency d) {
 139         if (d.getOrigin().equals(d.getTarget()))
 140             return false;
 141 
 142         // filter same package dependency
 143         String pn = d.getTarget().getPackageName();
 144         if (filterSamePackage && d.getOrigin().getPackageName().equals(pn)) {


 180     public boolean isJDKInternalPackage(Module module, String pn) {
 181         if (module.isJDKUnsupported()) {
 182             // its exported APIs are unsupported
 183             return true;
 184         }
 185 
 186         return module.isJDK() && !module.isExported(pn);
 187     }
 188 
 189     @Override
 190     public String toString() {
 191         StringBuilder sb = new StringBuilder();
 192         sb.append("include pattern: ").append(includePattern).append("\n");
 193         sb.append("filter same archive: ").append(filterSameArchive).append("\n");
 194         sb.append("filter same package: ").append(filterSamePackage).append("\n");
 195         sb.append("requires: ").append(requires).append("\n");
 196         return sb.toString();
 197     }
 198 
 199     public static class Builder {
 200         static Pattern SYSTEM_MODULE_PATTERN = Pattern.compile("java\\..*|jdk\\..*|javafx\\..*");
 201         Pattern filterPattern;
 202         Pattern regex;
 203         boolean filterSamePackage;
 204         boolean filterSameArchive;
 205         boolean findJDKInterals;
 206         // source filters
 207         Pattern includePattern;
 208         Pattern includeSystemModules;

 209         Set<String> requires = new HashSet<>();
 210         Set<String> targetPackages = new HashSet<>();
 211 


 212         public Builder packages(Set<String> packageNames) {
 213             this.targetPackages.addAll(packageNames);
 214             return this;
 215         }
 216         public Builder regex(Pattern regex) {
 217             this.regex = regex;
 218             return this;
 219         }
 220         public Builder filter(Pattern regex) {
 221             this.filterPattern = regex;
 222             return this;
 223         }
 224         public Builder filter(boolean samePackage, boolean sameArchive) {
 225             this.filterSamePackage = samePackage;
 226             this.filterSameArchive = sameArchive;
 227             return this;
 228         }
 229         public Builder requires(String name, Set<String> packageNames) {
 230             this.requires.add(name);
 231             this.targetPackages.addAll(packageNames);
 232 
 233             includeIfSystemModule(name);
 234             return this;
 235         }
 236         public Builder findJDKInternals(boolean value) {
 237             this.findJDKInterals = value;
 238             return this;
 239         }
 240         public Builder includePattern(Pattern regex) {
 241             this.includePattern = regex;
 242             return this;
 243         }
 244         public Builder includeSystemModules(Pattern regex) {
 245             this.includeSystemModules = regex;
 246             return this;
 247         }
 248         public Builder includeIfSystemModule(String name) {
 249             if (includeSystemModules == null &&
 250                     SYSTEM_MODULE_PATTERN.matcher(name).matches()) {
 251                 this.includeSystemModules = SYSTEM_MODULE_PATTERN;
 252             }
 253             return this;
 254         }
 255 
 256         public JdepsFilter build() {
 257             Dependency.Filter filter = null;
 258             if (regex != null)
 259                 filter = Dependencies.getRegexFilter(regex);
 260             else if (!targetPackages.isEmpty()) {
 261                 filter = Dependencies.getPackageFilter(targetPackages, false);
 262             }
 263             return new JdepsFilter(filter,
 264                                    filterPattern,
 265                                    filterSamePackage,
 266                                    filterSameArchive,
 267                                    findJDKInterals,
 268                                    includePattern,
 269                                    includeSystemModules,
 270                                    requires);
 271         }
 272 
 273     }
 274 }


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.tools.jdeps;
  26 
  27 import com.sun.tools.classfile.Dependencies;
  28 import com.sun.tools.classfile.Dependency;
  29 import com.sun.tools.classfile.Dependency.Location;
  30 
  31 import java.util.HashSet;

  32 import java.util.Set;
  33 import java.util.regex.Pattern;


  34 
  35 /*
  36  * Filter configured based on the input jdeps option
  37  * 1. -p and -regex to match target dependencies
  38  * 2. -filter:package to filter out same-package dependencies
  39  *    This filter is applied when jdeps parses the class files
  40  *    and filtered dependencies are not stored in the Analyzer.
  41  * 3. --require specifies to match target dependence from the given module
  42  *    This gets expanded into package lists to be filtered.
  43  * 4. -filter:archive to filter out same-archive dependencies
  44  *    This filter is applied later in the Analyzer as the
  45  *    containing archive of a target class may not be known until
  46  *    the entire archive
  47  */
  48 public class JdepsFilter implements Dependency.Filter, Analyzer.Filter {
  49 
  50     public static final JdepsFilter DEFAULT_FILTER =
  51         new JdepsFilter.Builder().filter(true, true).build();
  52 
  53     private final Dependency.Filter filter;
  54     private final Pattern filterPattern;
  55     private final boolean filterSamePackage;
  56     private final boolean filterSameArchive;
  57     private final boolean findJDKInternals;
  58     private final Pattern includePattern;

  59 
  60     private final Set<String> requires;
  61 
  62     private JdepsFilter(Dependency.Filter filter,
  63                         Pattern filterPattern,
  64                         boolean filterSamePackage,
  65                         boolean filterSameArchive,
  66                         boolean findJDKInternals,
  67                         Pattern includePattern,

  68                         Set<String> requires) {
  69         this.filter = filter;
  70         this.filterPattern = filterPattern;
  71         this.filterSamePackage = filterSamePackage;
  72         this.filterSameArchive = filterSameArchive;
  73         this.findJDKInternals = findJDKInternals;
  74         this.includePattern = includePattern;

  75         this.requires = requires;
  76     }
  77 
  78     /**
  79      * Tests if the given class matches the pattern given in the -include option
  80      *
  81      * @param cn fully-qualified name
  82      */
  83     public boolean matches(String cn) {
  84         if (includePattern == null)
  85             return true;
  86 
  87         if (includePattern != null)
  88             return includePattern.matcher(cn).matches();
  89 
  90         return false;
  91     }
  92 
  93     /**
  94      * Tests if the given source includes classes specified in -include option
  95      *
  96      * This method can be used to determine if the given source should eagerly
  97      * be processed.
  98      */
  99     public boolean matches(Archive source) {
 100         if (includePattern != null) {
 101             return source.reader().entries().stream()
 102                     .map(name -> name.replace('/', '.'))
 103                     .filter(name -> !name.equals("module-info.class"))
 104                     .anyMatch(this::matches);
 105         }
 106         return hasTargetFilter();
 107     }
 108 








 109     public boolean hasIncludePattern() {
 110         return includePattern != null;
 111     }
 112 
 113     public boolean hasTargetFilter() {
 114         return filter != null;
 115     }
 116 
 117     public Set<String> requiresFilter() {
 118         return requires;
 119     }
 120 
 121     // ----- Dependency.Filter -----
 122 
 123     @Override
 124     public boolean accepts(Dependency d) {
 125         if (d.getOrigin().equals(d.getTarget()))
 126             return false;
 127 
 128         // filter same package dependency
 129         String pn = d.getTarget().getPackageName();
 130         if (filterSamePackage && d.getOrigin().getPackageName().equals(pn)) {


 166     public boolean isJDKInternalPackage(Module module, String pn) {
 167         if (module.isJDKUnsupported()) {
 168             // its exported APIs are unsupported
 169             return true;
 170         }
 171 
 172         return module.isJDK() && !module.isExported(pn);
 173     }
 174 
 175     @Override
 176     public String toString() {
 177         StringBuilder sb = new StringBuilder();
 178         sb.append("include pattern: ").append(includePattern).append("\n");
 179         sb.append("filter same archive: ").append(filterSameArchive).append("\n");
 180         sb.append("filter same package: ").append(filterSamePackage).append("\n");
 181         sb.append("requires: ").append(requires).append("\n");
 182         return sb.toString();
 183     }
 184 
 185     public static class Builder {

 186         Pattern filterPattern;
 187         Pattern regex;
 188         boolean filterSamePackage;
 189         boolean filterSameArchive;
 190         boolean findJDKInterals;
 191         // source filters
 192         Pattern includePattern;
 193         Pattern includeSystemModules;
 194         Set<Module> roots = new HashSet<>();
 195         Set<String> requires = new HashSet<>();
 196         Set<String> targetPackages = new HashSet<>();
 197 
 198         public Builder() {};
 199 
 200         public Builder packages(Set<String> packageNames) {
 201             this.targetPackages.addAll(packageNames);
 202             return this;
 203         }
 204         public Builder regex(Pattern regex) {
 205             this.regex = regex;
 206             return this;
 207         }
 208         public Builder filter(Pattern regex) {
 209             this.filterPattern = regex;
 210             return this;
 211         }
 212         public Builder filter(boolean samePackage, boolean sameArchive) {
 213             this.filterSamePackage = samePackage;
 214             this.filterSameArchive = sameArchive;
 215             return this;
 216         }
 217         public Builder requires(String name, Set<String> packageNames) {
 218             this.requires.add(name);
 219             this.targetPackages.addAll(packageNames);


 220             return this;
 221         }
 222         public Builder findJDKInternals(boolean value) {
 223             this.findJDKInterals = value;
 224             return this;
 225         }
 226         public Builder includePattern(Pattern regex) {
 227             this.includePattern = regex;
 228             return this;
 229         }











 230 
 231         public JdepsFilter build() {
 232             Dependency.Filter filter = null;
 233             if (regex != null)
 234                 filter = Dependencies.getRegexFilter(regex);
 235             else if (!targetPackages.isEmpty()) {
 236                 filter = Dependencies.getPackageFilter(targetPackages, false);
 237             }
 238             return new JdepsFilter(filter,
 239                                    filterPattern,
 240                                    filterSamePackage,
 241                                    filterSameArchive,
 242                                    findJDKInterals,
 243                                    includePattern,

 244                                    requires);
 245         }
 246 
 247     }
 248 }
< prev index next >