1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 package proguard.classfile.util;
22 
23 import proguard.util.*;
24 
25 import java.io.PrintStream;
26 import java.util.List;
27 
28 /**
29  * This class prints out and counts warnings.
30  *
31  * @author Eric Lafortune
32  */
33 public class WarningPrinter
34 {
35     private final PrintStream   printStream;
36     private final StringMatcher classFilter;
37     private int                 warningCount;
38 
39 
40     /**
41      * Creates a new WarningPrinter that prints to the System.err print stream.
42      */
WarningPrinter()43     public WarningPrinter()
44     {
45         this(System.err);
46     }
47 
48 
49     /**
50      * Creates a new WarningPrinter that prints to the given print stream.
51      */
WarningPrinter(PrintStream printStream)52     public WarningPrinter(PrintStream printStream)
53     {
54         this.printStream = printStream;
55         this.classFilter = null;
56     }
57 
58 
59     /**
60      * Creates a new WarningPrinter that prints to the given print stream,
61      * except if the names of any involved classes matches the given filter.
62      */
WarningPrinter(PrintStream printStream, List classFilter)63     public WarningPrinter(PrintStream printStream, List classFilter)
64     {
65         this.printStream = printStream;
66         this.classFilter = classFilter == null ? null :
67             new ListParser(new ClassNameParser()).parse(classFilter);
68     }
69 
70 
71     /**
72      * Prints out the given warning and increments the warning count, if
73      * the given class name passes the class name filter.
74      */
print(String className, String warning)75     public void print(String className, String warning)
76     {
77         if (accepts(className))
78         {
79             print(warning);
80         }
81     }
82 
83 
84     /**
85      * Returns whether the given class name passes the class name filter.
86      */
accepts(String className)87     public boolean accepts(String className)
88     {
89         return classFilter == null ||
90             !classFilter.matches(className);
91     }
92 
93 
94     /**
95      * Prints out the given warning and increments the warning count, if
96      * the given class names pass the class name filter.
97      */
print(String className1, String className2, String warning)98     public void print(String className1, String className2, String warning)
99     {
100         if (accepts(className1, className2))
101         {
102             print(warning);
103         }
104     }
105 
106 
107     /**
108      * Returns whether the given class names pass the class name filter.
109      */
accepts(String className1, String className2)110     public boolean accepts(String className1, String className2)
111     {
112         return classFilter == null ||
113             !(classFilter.matches(className1) ||
114               classFilter.matches(className2));
115     }
116 
117 
118     /**
119      * Prints out the given warning and increments the warning count.
120      */
print(String warning)121     private void print(String warning)
122     {
123         printStream.println(warning);
124 
125         warningCount++;
126     }
127 
128 
129     /**
130      * Returns the number of warnings printed so far.
131      */
getWarningCount()132     public int getWarningCount()
133     {
134         return warningCount;
135     }
136 }
137