Home | History | Annotate | Download | only in editor
      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.classfile.editor;
     22 
     23 import proguard.classfile.ProgramClass;
     24 import proguard.util.ArrayUtil;
     25 
     26 /**
     27  * This class can add and delete interfaces to and from classes. References to
     28  * the constant pool must be filled out beforehand.
     29  *
     30  * @author Eric Lafortune
     31  */
     32 public class InterfacesEditor
     33 {
     34     private final ProgramClass targetClass;
     35 
     36 
     37     /**
     38      * Creates a new InterfacesEditor that will edit interfaces in the given
     39      * target class.
     40      */
     41     public InterfacesEditor(ProgramClass targetClass)
     42     {
     43         this.targetClass = targetClass;
     44     }
     45 
     46 
     47     /**
     48      * Adds the specified interface to the target class, if it isn't present yet.
     49      */
     50     public void addInterface(int interfaceConstantIndex)
     51     {
     52         // Is the interface not yet present?
     53         if (findInterfaceIndex(interfaceConstantIndex) < 0)
     54         {
     55             // Append the interface.
     56             targetClass.u2interfaces =
     57                 ArrayUtil.add(targetClass.u2interfaces,
     58                               targetClass.u2interfacesCount++,
     59                               interfaceConstantIndex);
     60         }
     61     }
     62 
     63 
     64     /**
     65      * Deletes the given interface from the target class, if it is present.
     66      */
     67     public void deleteInterface(int interfaceConstantIndex)
     68     {
     69         // Is the interface already present?
     70         int interfaceIndex = findInterfaceIndex(interfaceConstantIndex);
     71         if (interfaceIndex >= 0)
     72         {
     73             int   interfacesCount = --targetClass.u2interfacesCount;
     74             int[] interfaces      = targetClass.u2interfaces;
     75 
     76             // Shift the other interfaces in the array.
     77             for (int index = interfaceIndex; index < interfacesCount; index++)
     78             {
     79                 interfaces[index] = interfaces[index + 1];
     80             }
     81 
     82             // Clear the remaining entry in the array.
     83             interfaces[interfacesCount] = 0;
     84         }
     85     }
     86 
     87 
     88     // Small utility methods.
     89 
     90     /**
     91      * Finds the index of the specified interface in the list of interfaces of
     92      * the target class.
     93      */
     94     private int findInterfaceIndex(int interfaceConstantIndex)
     95     {
     96         int   interfacesCount = targetClass.u2interfacesCount;
     97         int[] interfaces      = targetClass.u2interfaces;
     98 
     99         for (int index = 0; index < interfacesCount; index++)
    100         {
    101             if (interfaces[index] == interfaceConstantIndex)
    102             {
    103                 return index;
    104             }
    105         }
    106 
    107         return -1;
    108     }
    109 }