📄 obfuscator.java
字号:
/* * ProGuard -- shrinking, optimization, obfuscation, and preverification * of Java bytecode. * * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.obfuscate;import proguard.*;import proguard.classfile.*;import proguard.classfile.constant.visitor.AllConstantVisitor;import proguard.classfile.editor.*;import proguard.classfile.util.*;import proguard.classfile.visitor.*;import java.io.*;import java.util.*;/** * This class can perform obfuscation of class pools according to a given * specification. * * @author Eric Lafortune */public class Obfuscator{ private Configuration configuration; /** * Creates a new Obfuscator. */ public Obfuscator(Configuration configuration) { this.configuration = configuration; } /** * Performs obfuscation of the given program class pool. */ public void execute(ClassPool programClassPool, ClassPool libraryClassPool) throws IOException { // Check if we have at least some keep commands. if (configuration.keep == null && configuration.applyMapping == null && configuration.printMapping == null) { throw new IOException("You have to specify '-keep' options for the obfuscation step."); } // Clean up any old visitor info. programClassPool.classesAccept(new ClassCleaner()); libraryClassPool.classesAccept(new ClassCleaner()); // If the class member names have to correspond globally, // link all class members in all classes, otherwise // link all non-private methods in all class hierarchies. ClassVisitor memberInfoLinker = configuration.useUniqueClassMemberNames ? (ClassVisitor)new AllMemberVisitor(new MethodLinker()) : (ClassVisitor)new BottomClassFilter(new MethodLinker()); programClassPool.classesAccept(memberInfoLinker); libraryClassPool.classesAccept(memberInfoLinker); // Create a visitor for marking the seeds. NameMarker nameMarker = new NameMarker(); ClassPoolVisitor classPoolvisitor = ClassSpecificationVisitorFactory.createClassPoolVisitor(configuration.keep, nameMarker, nameMarker, false, false, true); // Mark the seeds. programClassPool.accept(classPoolvisitor); libraryClassPool.accept(classPoolvisitor); // All library classes and library class members keep their names. libraryClassPool.classesAccept(nameMarker); libraryClassPool.classesAccept(new AllMemberVisitor(nameMarker)); // Apply the mapping, if one has been specified. The mapping can // override the names of library classes and of library class members. if (configuration.applyMapping != null) { WarningPrinter warningPrinter = configuration.warn ? new WarningPrinter(System.err) : null; MappingReader reader = new MappingReader(configuration.applyMapping); MappingProcessor keeper = new MultiMappingProcessor(new MappingProcessor[] { new MappingKeeper(programClassPool, warningPrinter), new MappingKeeper(libraryClassPool, null), }); reader.pump(keeper); if (configuration.warn) { // Print out a summary of the warnings if necessary. int mappingWarningCount = warningPrinter.getWarningCount(); if (mappingWarningCount > 0) { System.err.println("Warning: there were " + mappingWarningCount + " kept classes and class members that were remapped anyway."); System.err.println(" You should adapt your configuration or edit the mapping file."); if (!configuration.ignoreWarnings) { System.err.println(" If you are sure this remapping won't hurt,"); System.err.println(" you could try your luck using the '-ignorewarnings' option."); throw new IOException("Please correct the above warnings first."); } } } } // Mark attributes that have to be kept. AttributeUsageMarker attributeUsageMarker = new AttributeUsageMarker(); if (configuration.keepAttributes != null) { if (!configuration.keepAttributes.isEmpty()) { attributeUsageMarker.setKeepAttributes(configuration.keepAttributes); } else { attributeUsageMarker.setKeepAllAttributes(); } } programClassPool.classesAccept(attributeUsageMarker); // Remove the attributes that can be discarded. programClassPool.classesAccept(new AttributeShrinker()); // Come up with new names for all classes. programClassPool.classesAccept(new ClassObfuscator(programClassPool, configuration.useMixedCaseClassNames, configuration.flattenPackageHierarchy, configuration.repackageClasses)); // Come up with new names for all class members. NameFactory nameFactory = new SimpleNameFactory(); if (configuration.obfuscationDictionary != null) { nameFactory = new DictionaryNameFactory(configuration.obfuscationDictionary, nameFactory); } WarningPrinter warningPrinter = configuration.warn ? new WarningPrinter(System.err) : null; // Maintain a map of names to avoid [descriptor - new name - old name]. Map descriptorMap = new HashMap(); // Do the class member names have to be globally unique? if (configuration.useUniqueClassMemberNames) { // Collect all member names in all classes. programClassPool.classesAccept( new AllMemberVisitor( new MemberNameCollector(configuration.overloadAggressively, descriptorMap))); // Assign new names to all members in all classes. programClassPool.classesAccept( new AllMemberVisitor( new MemberObfuscator(configuration.overloadAggressively, nameFactory, descriptorMap))); } else { // Come up with new names for all non-private class members. programClassPool.classesAccept( new MultiClassVisitor(new ClassVisitor[] { // Collect all private member names in this class and down // the hierarchy. new ClassHierarchyTraveler(true, false, false, true, new AllMemberVisitor( new MemberAccessFilter(ClassConstants.INTERNAL_ACC_PRIVATE, 0, new MemberNameCollector(configuration.overloadAggressively, descriptorMap)))), // Collect all non-private member names anywhere in the hierarchy. new ClassHierarchyTraveler(true, true, true, true, new AllMemberVisitor(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -