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 */
21package proguard;
22
23import proguard.classfile.*;
24import proguard.classfile.util.*;
25import proguard.classfile.visitor.*;
26import proguard.optimize.*;
27
28import java.util.List;
29
30/**
31 * This class checks whether some keep rules only keep library classes, no
32 * program classes. That is strange, because library classes never need to
33 * be kept explicitly.
34 *
35 * @author Eric Lafortune
36 */
37public class LibraryKeepChecker
38implements   ClassVisitor
39{
40    private final ClassPool      programClassPool;
41    private final ClassPool      libraryClassPool;
42    private final WarningPrinter notePrinter;
43
44    // Some fields acting as parameters for the class visitor.
45    private String keepName;
46
47
48    /**
49     * Creates a new DescriptorKeepChecker.
50     */
51    public LibraryKeepChecker(ClassPool      programClassPool,
52                              ClassPool      libraryClassPool,
53                              WarningPrinter notePrinter)
54    {
55        this.programClassPool = programClassPool;
56        this.libraryClassPool = libraryClassPool;
57        this.notePrinter      = notePrinter;
58    }
59
60
61    /**
62     * Checks the classes mentioned in the given keep specifications, printing
63     * notes if necessary.
64     */
65    public void checkClassSpecifications(List keepSpecifications)
66    {
67        if (keepSpecifications != null)
68        {
69            // Go over all individual keep specifications.
70            for (int index = 0; index < keepSpecifications.size(); index++)
71            {
72                KeepClassSpecification keepClassSpecification =
73                    (KeepClassSpecification)keepSpecifications.get(index);
74
75                // Is the keep specification more specific than a general
76                // wildcard?
77                keepName = keepClassSpecification.className;
78                if (keepName != null)
79                {
80                    // Doesn't the specification match any program classes?
81                    ClassCounter programClassCounter = new ClassCounter();
82                    programClassPool.accept(
83                        ClassSpecificationVisitorFactory.createClassPoolVisitor(keepClassSpecification,
84                                                                                programClassCounter,
85                                                                                null));
86                    if (programClassCounter.getCount() == 0)
87                    {
88                        // Print out notes about any matched library classes.
89                        libraryClassPool.accept(
90                            ClassSpecificationVisitorFactory.createClassPoolVisitor(keepClassSpecification,
91                                                                                    this,
92                                                                                    null));
93                    }
94                }
95            }
96        }
97    }
98
99
100    // Implementations for ClassVisitor.
101
102    public void visitProgramClass(ProgramClass programClass) {}
103
104
105    public void visitLibraryClass(LibraryClass libraryClass)
106    {
107        String className = libraryClass.getName();
108        notePrinter.print(className,
109                          "Note: the configuration explicitly specifies '" +
110                          ClassUtil.externalClassName(keepName) +
111                          "' to keep library class '" +
112                          ClassUtil.externalClassName(className) +
113                          "'");
114    }
115}
116