Home | History | Annotate | Download | only in proguard
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2014 Eric Lafortune (eric (at) 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;
     22 
     23 import proguard.classfile.*;
     24 import proguard.classfile.util.*;
     25 import proguard.classfile.visitor.MemberVisitor;
     26 
     27 import java.util.List;
     28 
     29 /**
     30  * This class checks if the user has specified non-existent class members.
     31  *
     32  * @author Eric Lafortune
     33  */
     34 public class ClassMemberChecker
     35 extends      SimplifiedVisitor
     36 implements   MemberVisitor
     37 {
     38     private final ClassPool      programClassPool;
     39     private final WarningPrinter notePrinter;
     40 
     41 
     42     /**
     43      * Creates a new ClassMemberChecker.
     44      */
     45     public ClassMemberChecker(ClassPool      programClassPool,
     46                               WarningPrinter notePrinter)
     47     {
     48         this.programClassPool = programClassPool;
     49         this.notePrinter      = notePrinter;
     50     }
     51 
     52 
     53     /**
     54      * Checks the classes mentioned in the given class specifications, printing
     55      * notes if necessary.
     56      */
     57     public void checkClassSpecifications(List classSpecifications)
     58     {
     59         if (classSpecifications != null)
     60         {
     61             for (int index = 0; index < classSpecifications.size(); index++)
     62             {
     63                 ClassSpecification classSpecification =
     64                     (ClassSpecification)classSpecifications.get(index);
     65 
     66                 String className = classSpecification.className;
     67                 if (className != null             &&
     68                     !containsWildCards(className) &&
     69                     notePrinter.accepts(className))
     70                 {
     71                     Clazz clazz = programClassPool.getClass(className);
     72                     if (clazz != null)
     73                     {
     74                         checkMemberSpecifications(clazz, classSpecification.fieldSpecifications,  true);
     75                         checkMemberSpecifications(clazz, classSpecification.methodSpecifications, false);
     76                     }
     77                 }
     78             }
     79         }
     80     }
     81 
     82 
     83     /**
     84      * Checks the class members mentioned in the given class member
     85      * specifications, printing notes if necessary.
     86      */
     87     private void checkMemberSpecifications(Clazz   clazz,
     88                                            List    memberSpecifications,
     89                                            boolean isField)
     90     {
     91         if (memberSpecifications != null)
     92         {
     93             String className = clazz.getName();
     94 
     95             for (int index = 0; index < memberSpecifications.size(); index++)
     96             {
     97                 MemberSpecification memberSpecification =
     98                     (MemberSpecification)memberSpecifications.get(index);
     99 
    100                 String memberName = memberSpecification.name;
    101                 String descriptor = memberSpecification.descriptor;
    102                 if (memberName != null             &&
    103                     !containsWildCards(memberName) &&
    104                     descriptor != null             &&
    105                     !containsWildCards(descriptor))
    106                 {
    107                     if (isField)
    108                     {
    109                         if (clazz.findField(memberName, descriptor) == null)
    110                         {
    111                             notePrinter.print(className,
    112                                               "Note: the configuration refers to the unknown field '" +
    113                                               ClassUtil.externalFullFieldDescription(0, memberName, descriptor) + "' in class '" +
    114                                               ClassUtil.externalClassName(className) + "'");
    115                         }
    116                     }
    117                     else
    118                     {
    119                         if (clazz.findMethod(memberName, descriptor) == null)
    120                         {
    121                             notePrinter.print(className,
    122                                               "Note: the configuration refers to the unknown method '" +
    123                                               ClassUtil.externalFullMethodDescription(className, 0, memberName, descriptor) + "' in class '" +
    124                                               ClassUtil.externalClassName(className) + "'");
    125                         }
    126                     }
    127                 }
    128             }
    129         }
    130     }
    131 
    132 
    133     private static boolean containsWildCards(String string)
    134     {
    135         return string != null &&
    136             (string.indexOf('!')   >= 0 ||
    137              string.indexOf('*')   >= 0 ||
    138              string.indexOf('?')   >= 0 ||
    139              string.indexOf(',')   >= 0 ||
    140              string.indexOf("///") >= 0);
    141     }
    142 
    143 
    144     // Implementations for MemberVisitor.
    145 
    146     public void visitProgramField(ProgramClass programClass, ProgramField programField)
    147     {
    148         System.out.println("      Maybe you meant the field '" +
    149                            ClassUtil.externalFullFieldDescription(0, programField.getName(programClass), programField.getDescriptor(programClass)) + "'?");
    150     }
    151 
    152 
    153     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
    154     {
    155         System.out.println("      Maybe you meant the method '" +
    156                            ClassUtil.externalFullMethodDescription(programClass.getName(), 0, programMethod.getName(programClass), programMethod.getDescriptor(programClass)) + "'?");
    157     }
    158 }