View Javadoc
1   package org.apache.maven.plugins.jdeprscan.consumers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
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
39      extends CommandLineUtils.StringStreamConsumer
40      implements StreamConsumer
41  {
42  
43      private Map<String, Set<String>> deprecatedClasses = new HashMap<>();
44  
45      private Map<String, Set<String>> deprecatedMethods = new HashMap<>();
46  
47      public static final Pattern DEPRECATED_CLASS = Pattern.compile( "^class (\\S+) uses deprecated class (\\S+)" );
48  
49      public static final Pattern DEPRECATED_METHOD = Pattern.compile( "^class (\\S+) uses deprecated method (\\S+)" );
50  
51      public Map<String, Set<String>> getDeprecatedClasses()
52      {
53          return deprecatedClasses;
54      }
55      
56      public Map<String, Set<String>> getDeprecatedMethods()
57      {
58          return deprecatedMethods;
59      }
60      
61      @Override
62      public void consumeLine( String line )
63      {
64          super.consumeLine( line );
65  
66          Matcher matcher;
67          
68          matcher = DEPRECATED_CLASS.matcher( line );
69          if ( matcher.find() )
70          {
71              Set<String> dc = deprecatedClasses.get( matcher.group( 1 ) );
72              if ( dc == null )
73              {
74                  dc = new HashSet<>();
75                  deprecatedClasses.put( matcher.group( 1 ), dc );
76              }
77              dc.add( matcher.group( 2 ) );
78              return;
79          }
80          
81          matcher = DEPRECATED_METHOD.matcher( line );
82          if ( matcher.find() )
83          {
84              Set<String> dm = deprecatedMethods.get( matcher.group( 1 ) );
85              if ( dm == null )
86              {
87                  dm = new HashSet<>();
88                  deprecatedMethods.put( matcher.group( 1 ), dm );
89              }
90              dm.add( matcher.group( 2 ) );
91              return;
92          }
93      }
94  }