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.classfile.util; 22 23import proguard.classfile.*; 24import proguard.classfile.constant.ClassConstant; 25import proguard.classfile.constant.visitor.ConstantVisitor; 26import proguard.classfile.visitor.ClassVisitor; 27 28/** 29 * This ClassVisitor initializes the superclass hierarchy of all classes that 30 * it visits. 31 * <p> 32 * Visited library classes get direct references to their superclasses and 33 * interfaces, replacing the superclass names and interface names. The direct 34 * references are equivalent to the names, but they are more efficient to work 35 * with. 36 * <p> 37 * This visitor optionally prints warnings if some superclasses can't be found 38 * or if they are in the program class pool. 39 * 40 * @author Eric Lafortune 41 */ 42public class ClassSuperHierarchyInitializer 43extends SimplifiedVisitor 44implements ClassVisitor, 45 ConstantVisitor 46{ 47 private final ClassPool programClassPool; 48 private final ClassPool libraryClassPool; 49 private final WarningPrinter missingWarningPrinter; 50 private final WarningPrinter dependencyWarningPrinter; 51 52 53 /** 54 * Creates a new ClassSuperHierarchyInitializer that initializes the super 55 * hierarchy of all visited class files, optionally printing warnings if 56 * some classes can't be found or if they are in the program class pool. 57 */ 58 public ClassSuperHierarchyInitializer(ClassPool programClassPool, 59 ClassPool libraryClassPool, 60 WarningPrinter missingWarningPrinter, 61 WarningPrinter dependencyWarningPrinter) 62 { 63 this.programClassPool = programClassPool; 64 this.libraryClassPool = libraryClassPool; 65 this.missingWarningPrinter = missingWarningPrinter; 66 this.dependencyWarningPrinter = dependencyWarningPrinter; 67 } 68 69 70 // Implementations for ClassVisitor. 71 72 public void visitProgramClass(ProgramClass programClass) 73 { 74 // Link to the super class. 75 programClass.superClassConstantAccept(this); 76 77 // Link to the interfaces. 78 programClass.interfaceConstantsAccept(this); 79 } 80 81 82 public void visitLibraryClass(LibraryClass libraryClass) 83 { 84 String className = libraryClass.getName(); 85 86 // Link to the super class. 87 String superClassName = libraryClass.superClassName; 88 if (superClassName != null) 89 { 90 // Keep a reference to the superclass. 91 libraryClass.superClass = findClass(className, superClassName); 92 } 93 94 // Link to the interfaces. 95 if (libraryClass.interfaceNames != null) 96 { 97 String[] interfaceNames = libraryClass.interfaceNames; 98 Clazz[] interfaceClasses = new Clazz[interfaceNames.length]; 99 100 for (int index = 0; index < interfaceNames.length; index++) 101 { 102 // Keep a reference to the interface class. 103 interfaceClasses[index] = 104 findClass(className, interfaceNames[index]); 105 } 106 107 libraryClass.interfaceClasses = interfaceClasses; 108 } 109 } 110 111 112 // Implementations for ConstantVisitor. 113 114 public void visitClassConstant(Clazz clazz, ClassConstant classConstant) 115 { 116 classConstant.referencedClass = 117 findClass(clazz.getName(), classConstant.getName(clazz)); 118 } 119 120 121 // Small utility methods. 122 123 /** 124 * Returns the class with the given name, either for the program class pool 125 * or from the library class pool, or <code>null</code> if it can't be found. 126 */ 127 private Clazz findClass(String referencingClassName, String name) 128 { 129 // First look for the class in the program class pool. 130 Clazz clazz = programClassPool.getClass(name); 131 132 // Otherwise look for the class in the library class pool. 133 if (clazz == null) 134 { 135 clazz = libraryClassPool.getClass(name); 136 137 if (clazz == null && 138 missingWarningPrinter != null) 139 { 140 // We didn't find the superclass or interface. Print a warning. 141 missingWarningPrinter.print(referencingClassName, 142 name, 143 "Warning: " + 144 ClassUtil.externalClassName(referencingClassName) + 145 ": can't find superclass or interface " + 146 ClassUtil.externalClassName(name)); 147 } 148 } 149 else if (dependencyWarningPrinter != null) 150 { 151 // The superclass or interface was found in the program class pool. 152 // Print a warning. 153 dependencyWarningPrinter.print(referencingClassName, 154 name, 155 "Warning: library class " + 156 ClassUtil.externalClassName(referencingClassName) + 157 " extends or implements program class " + 158 ClassUtil.externalClassName(name)); 159 } 160 161 return clazz; 162 } 163} 164