1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 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 */ 21 package proguard.wtk; 22 23 import com.sun.kvem.environment.Obfuscator; 24 import proguard.*; 25 26 import java.io.*; 27 28 29 /** 30 * ProGuard plug-in for the J2ME Wireless Toolkit. 31 * <p> 32 * In order to integrate this plug-in in the toolkit, you'll have to put the 33 * following lines in the file 34 * {j2mewtk.dir}<code>/wtklib/Linux/ktools.properties</code> or 35 * {j2mewtk.dir}<code>\wtklib\Windows\ktools.properties</code> (whichever is 36 * applicable). 37 * <p> 38 * <pre> 39 * obfuscator.runner.class.name: proguard.wtk.ProGuardObfuscator 40 * obfuscator.runner.classpath: /usr/local/java/proguard1.6/lib/proguard.jar 41 * </pre> 42 * Please make sure the class path is set correctly for your system. 43 * 44 * @author Eric Lafortune 45 */ 46 public class ProGuardObfuscator implements Obfuscator 47 { 48 private static final String DEFAULT_CONFIGURATION = "default.pro"; 49 50 51 // Implementations for Obfuscator. 52 createScriptFile(File jadFile, File projectDir)53 public void createScriptFile(File jadFile, 54 File projectDir) 55 { 56 // We don't really need to create a script file; 57 // we'll just fill out all options in the run method. 58 } 59 60 run(File obfuscatedJarFile, String wtkBinDir, String wtkLibDir, String jarFileName, String projectDirName, String classPath, String emptyAPI)61 public void run(File obfuscatedJarFile, 62 String wtkBinDir, 63 String wtkLibDir, 64 String jarFileName, 65 String projectDirName, 66 String classPath, 67 String emptyAPI) 68 throws IOException 69 { 70 // Create the ProGuard configuration. 71 Configuration configuration = new Configuration(); 72 73 // Parse the default configuration file. 74 ConfigurationParser parser = new ConfigurationParser(this.getClass().getResource(DEFAULT_CONFIGURATION)); 75 76 try 77 { 78 parser.parse(configuration); 79 80 // Fill out the library class path. 81 configuration.libraryJars = classPath(classPath); 82 83 // Fill out the program class path (input and output). 84 configuration.programJars = new ClassPath(); 85 configuration.programJars.add(new ClassPathEntry(new File(jarFileName), false)); 86 configuration.programJars.add(new ClassPathEntry(obfuscatedJarFile, true)); 87 88 // The preverify tool seems to unpack the resulting classes, 89 // so we must not use mixed-case class names on Windows. 90 configuration.useMixedCaseClassNames = 91 !System.getProperty("os.name").regionMatches(true, 0, "windows", 0, 7); 92 93 // Run ProGuard with these options. 94 ProGuard proGuard = new ProGuard(configuration); 95 proGuard.execute(); 96 97 } 98 catch (ParseException ex) 99 { 100 throw new IOException(ex.getMessage()); 101 } 102 finally 103 { 104 parser.close(); 105 } 106 } 107 108 109 /** 110 * Converts the given class path String into a ClassPath object. 111 */ classPath(String classPathString)112 private ClassPath classPath(String classPathString) 113 { 114 ClassPath classPath = new ClassPath(); 115 116 String separator = System.getProperty("path.separator"); 117 118 int index = 0; 119 while (index < classPathString.length()) 120 { 121 // Find the next separator, or the end of the String. 122 int next_index = classPathString.indexOf(separator, index); 123 if (next_index < 0) 124 { 125 next_index = classPathString.length(); 126 } 127 128 // Create and add the found class path entry. 129 ClassPathEntry classPathEntry = 130 new ClassPathEntry(new File(classPathString.substring(index, next_index)), 131 false); 132 133 classPath.add(classPathEntry); 134 135 // Continue after the separator. 136 index = next_index + 1; 137 } 138 139 return classPath; 140 } 141 } 142