View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.jdeprscan.consumers;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import org.codehaus.plexus.util.cli.CommandLineUtils;
30  import org.codehaus.plexus.util.cli.StreamConsumer;
31  
32  /**
33   * Consumes output of jdeprscan tool
34   *
35   * @author Robert Scholte
36   * @since 3.0.0
37   */
38  public class JDeprScanConsumer extends CommandLineUtils.StringStreamConsumer implements StreamConsumer {
39  
40      private final Map<String, Set<String>> deprecatedClasses = new HashMap<>();
41  
42      private final Map<String, Set<String>> deprecatedMethods = new HashMap<>();
43  
44      public static final Pattern DEPRECATED_CLASS = Pattern.compile("^class (\\S+) uses deprecated class (\\S+)");
45  
46      public static final Pattern DEPRECATED_METHOD = Pattern.compile("^class (\\S+) uses deprecated method (\\S+)");
47  
48      public Map<String, Set<String>> getDeprecatedClasses() {
49          return Collections.unmodifiableMap(deprecatedClasses);
50      }
51  
52      public Map<String, Set<String>> getDeprecatedMethods() {
53          return Collections.unmodifiableMap(deprecatedMethods);
54      }
55  
56      @Override
57      public void consumeLine(String line) {
58          super.consumeLine(line);
59  
60          Matcher deprecatedClassMatcher = DEPRECATED_CLASS.matcher(line);
61          matcherCollector(deprecatedClassMatcher, deprecatedClasses);
62  
63          Matcher deprecatedMethodMatcher = DEPRECATED_METHOD.matcher(line);
64          matcherCollector(deprecatedMethodMatcher, deprecatedMethods);
65      }
66  
67      private void matcherCollector(Matcher matcher, Map<String, Set<String>> deprecatedMethods) {
68          if (!matcher.find()) {
69              return;
70          }
71  
72          Set<String> dm = deprecatedMethods.computeIfAbsent(matcher.group(1), k -> new HashSet<>());
73          dm.add(matcher.group(2));
74      }
75  }