KeepClassMemberChecker.java revision 8a6199f0c36a778f22394364347a301b0b28e94b
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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 */
21package proguard;
22
23import proguard.classfile.util.*;
24import proguard.classfile.visitor.ClassVisitor;
25
26import java.util.List;
27
28/**
29 * This class checks if the user has forgotten to specify class members in
30 * some keep options in the configuration.
31 *
32 * @author Eric Lafortune
33 */
34public class KeepClassMemberChecker
35extends      SimplifiedVisitor
36implements   ClassVisitor
37{
38    private final WarningPrinter notePrinter;
39
40
41    /**
42     * Creates a new KeepClassMemberChecker.
43     */
44    public KeepClassMemberChecker(WarningPrinter notePrinter)
45    {
46        this.notePrinter = notePrinter;
47    }
48
49
50    /**
51     * Checks if the given class specifications try to keep class members
52     * without actually specifying any, printing notes if necessary. Returns
53     * the number of notes printed.
54     */
55    public void checkClassSpecifications(List keepClassSpecifications)
56    {
57        if (keepClassSpecifications != null)
58        {
59            for (int index = 0; index < keepClassSpecifications.size(); index++)
60            {
61                KeepClassSpecification keepClassSpecification =
62                    (KeepClassSpecification)keepClassSpecifications.get(index);
63
64                if (!keepClassSpecification.markClasses                      &&
65                    (keepClassSpecification.fieldSpecifications  == null ||
66                     keepClassSpecification.fieldSpecifications.size() == 0) &&
67                    (keepClassSpecification.methodSpecifications == null ||
68                     keepClassSpecification.methodSpecifications.size() == 0))
69                {
70                    String className = keepClassSpecification.className;
71                    if (className == null)
72                    {
73                        className = keepClassSpecification.extendsClassName;
74                    }
75
76                    if (notePrinter.accepts(className))
77                    {
78                        notePrinter.print(className,
79                                          "Note: the configuration doesn't specify which class members to keep for class '" +
80                                          (className == null ?
81                                              ConfigurationConstants.ANY_CLASS_KEYWORD :
82                                              ClassUtil.externalClassName(className)) + "'");
83                    }
84                }
85            }
86        }
87    }
88}
89