1b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/* 2b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ProGuard -- shrinking, optimization, obfuscation, and preverification 3b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * of Java bytecode. 4b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 52270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu) 6b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 7b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is free software; you can redistribute it and/or modify it 8b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * under the terms of the GNU General Public License as published by the Free 9b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Software Foundation; either version 2 of the License, or (at your option) 10b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * any later version. 11b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 12b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is distributed in the hope that it will be useful, but WITHOUT 13b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * more details. 16b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 17b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * You should have received a copy of the GNU General Public License along 18b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * with this program; if not, write to the Free Software Foundation, Inc., 19b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 21b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopackage proguard.classfile.editor; 22b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 232270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstromimport proguard.classfile.ProgramClass; 242270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstromimport proguard.util.ArrayUtil; 25b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 26b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/** 27b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This class can add and delete interfaces to and from classes. References to 28b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * the constant pool must be filled out beforehand. 29b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 30b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @author Eric Lafortune 31b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 32b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopublic class InterfacesEditor 33b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato{ 34b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato private final ProgramClass targetClass; 35b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 36b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 37b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato /** 38b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Creates a new InterfacesEditor that will edit interfaces in the given 39b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * target class. 40b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 41b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato public InterfacesEditor(ProgramClass targetClass) 42b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 43b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato this.targetClass = targetClass; 44b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 45b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 46b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 47b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato /** 48b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Adds the specified interface to the target class, if it isn't present yet. 49b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 50b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato public void addInterface(int interfaceConstantIndex) 51b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 52b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato // Is the interface not yet present? 53b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato if (findInterfaceIndex(interfaceConstantIndex) < 0) 54b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 55b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato // Append the interface. 562270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom targetClass.u2interfaces = 572270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom ArrayUtil.add(targetClass.u2interfaces, 582270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom targetClass.u2interfacesCount++, 592270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom interfaceConstantIndex); 60b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 61b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 62b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 63b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 64b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato /** 65b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Deletes the given interface from the target class, if it is present. 66b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 67b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato public void deleteInterface(int interfaceConstantIndex) 68b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 69b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato // Is the interface already present? 70b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato int interfaceIndex = findInterfaceIndex(interfaceConstantIndex); 71b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato if (interfaceIndex >= 0) 72b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 73b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato int interfacesCount = --targetClass.u2interfacesCount; 74b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato int[] interfaces = targetClass.u2interfaces; 75b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 76b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato // Shift the other interfaces in the array. 77b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato for (int index = interfaceIndex; index < interfacesCount; index++) 78b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 79b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato interfaces[index] = interfaces[index + 1]; 80b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 81b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 82b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato // Clear the remaining entry in the array. 83b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato interfaces[interfacesCount] = 0; 84b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 85b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 86b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 87b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 88b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato // Small utility methods. 89b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 90b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato /** 91b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Finds the index of the specified interface in the list of interfaces of 92b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * the target class. 93b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */ 94b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato private int findInterfaceIndex(int interfaceConstantIndex) 95b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 96b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato int interfacesCount = targetClass.u2interfacesCount; 97b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato int[] interfaces = targetClass.u2interfaces; 98b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 99b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato for (int index = 0; index < interfacesCount; index++) 100b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato { 101b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato if (interfaces[index] == interfaceConstantIndex) 1022270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom { 1032270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom return index; 1042270795fbe0b277bfd49f40950ecaa78583175ccBrian Carlstrom } 105b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 106b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato 107b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato return -1; 108b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato } 109b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato}