Home | History | Annotate | Download | only in peephole
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2009 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.optimize.peephole;
     22 
     23 import proguard.classfile.*;
     24 import proguard.classfile.attribute.*;
     25 import proguard.classfile.attribute.visitor.*;
     26 import proguard.classfile.constant.ClassConstant;
     27 import proguard.classfile.constant.visitor.ConstantVisitor;
     28 import proguard.classfile.util.SimplifiedVisitor;
     29 import proguard.classfile.visitor.ClassVisitor;
     30 
     31 /**
     32  * This ClassVisitor removes InnerClasses and EnclosingMethod attributes in
     33  * classes that are retargeted or that refer to classes that are retargeted.
     34  *
     35  * @see ClassMerger
     36  * @author Eric Lafortune
     37  */
     38 public class RetargetedInnerClassAttributeRemover
     39 extends      SimplifiedVisitor
     40 implements   ClassVisitor,
     41              AttributeVisitor,
     42              InnerClassesInfoVisitor,
     43              ConstantVisitor
     44 {
     45     private boolean retargeted;
     46 
     47 
     48     // Implementations for ClassVisitor.
     49 
     50     public void visitProgramClass(ProgramClass programClass)
     51     {
     52         int         attributesCount = programClass.u2attributesCount;
     53         Attribute[] attributes      = programClass.attributes;
     54 
     55         int newAtributesCount = 0;
     56 
     57         // Copy over all non-retargeted attributes.
     58         for (int index = 0; index < attributesCount; index++)
     59         {
     60             Attribute attribute = attributes[index];
     61 
     62             // Check if it's an InnerClasses or EnclosingMethod attribute in
     63             // a retargeted class or referring to a retargeted class.
     64             retargeted = false;
     65             attribute.accept(programClass, this);
     66             if (!retargeted)
     67             {
     68                 attributes[newAtributesCount++] = attribute;
     69             }
     70         }
     71 
     72         // Clean up any remaining array elements.
     73         for (int index = newAtributesCount; index < attributesCount; index++)
     74         {
     75             attributes[index] = null;
     76         }
     77 
     78         // Update the number of attribuets.
     79         programClass.u2attributesCount = newAtributesCount;
     80     }
     81 
     82 
     83     // Implementations for AttributeVisitor.
     84 
     85     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
     86 
     87 
     88     public void visitInnerClassesAttribute(Clazz clazz, InnerClassesAttribute innerClassesAttribute)
     89     {
     90         // Check whether the class itself is retargeted.
     91         checkTarget(clazz);
     92 
     93         // Check whether the referenced classes are retargeted.
     94         innerClassesAttribute.innerClassEntriesAccept(clazz, this);
     95     }
     96 
     97 
     98     public void visitEnclosingMethodAttribute(Clazz clazz, EnclosingMethodAttribute enclosingMethodAttribute)
     99     {
    100         // Check whether the class itself is retargeted.
    101         checkTarget(clazz);
    102 
    103         // Check whether the referenced class is retargeted.
    104         checkTarget(enclosingMethodAttribute.referencedClass);
    105     }
    106 
    107 
    108     // Implementations for InnerClassesInfoVisitor.
    109 
    110     public void visitInnerClassesInfo(Clazz clazz, InnerClassesInfo innerClassesInfo)
    111     {
    112         // Check whether the inner class or the outer class are retargeted.
    113         innerClassesInfo.innerClassConstantAccept(clazz, this);
    114         innerClassesInfo.outerClassConstantAccept(clazz, this);
    115     }
    116 
    117 
    118     // Implementations for ConstantVisitor.
    119 
    120     public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
    121     {
    122         // Check whether the referenced class is retargeted.
    123         checkTarget(classConstant.referencedClass);
    124     }
    125 
    126 
    127     // Small utility methods.
    128 
    129     /**
    130      * Sets the global return value to true if the given class is retargeted.
    131      */
    132     private void checkTarget(Clazz clazz)
    133     {
    134         if (clazz != null &&
    135             ClassMerger.getTargetClass(clazz) != null)
    136         {
    137             retargeted = true;
    138         }
    139     }
    140 }