1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2013 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 System.getProperties()); 76 77 try 78 { 79 parser.parse(configuration); 80 81 // Fill out the library class path. 82 configuration.libraryJars = classPath(classPath); 83 84 // Fill out the program class path (input and output). 85 configuration.programJars = new ClassPath(); 86 configuration.programJars.add(new ClassPathEntry(new File(jarFileName), false)); 87 configuration.programJars.add(new ClassPathEntry(obfuscatedJarFile, true)); 88 89 // The preverify tool seems to unpack the resulting classes, 90 // so we must not use mixed-case class names on Windows. 91 configuration.useMixedCaseClassNames = 92 !System.getProperty("os.name").regionMatches(true, 0, "windows", 0, 7); 93 94 // Run ProGuard with these options. 95 ProGuard proGuard = new ProGuard(configuration); 96 proGuard.execute(); 97 98 } 99 catch (ParseException ex) 100 { 101 throw new IOException(ex.getMessage()); 102 } 103 finally 104 { 105 parser.close(); 106 } 107 } 108 109 110 /** 111 * Converts the given class path String into a ClassPath object. 112 */ classPath(String classPathString)113 private ClassPath classPath(String classPathString) 114 { 115 ClassPath classPath = new ClassPath(); 116 117 String separator = System.getProperty("path.separator"); 118 119 int index = 0; 120 while (index < classPathString.length()) 121 { 122 // Find the next separator, or the end of the String. 123 int next_index = classPathString.indexOf(separator, index); 124 if (next_index < 0) 125 { 126 next_index = classPathString.length(); 127 } 128 129 // Create and add the found class path entry. 130 ClassPathEntry classPathEntry = 131 new ClassPathEntry(new File(classPathString.substring(index, next_index)), 132 false); 133 134 classPath.add(classPathEntry); 135 136 // Continue after the separator. 137 index = next_index + 1; 138 } 139 140 return classPath; 141 } 142 } 143