KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > wtk > ProGuardObfuscator


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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.*;
24 import proguard.*;
25 import proguard.classfile.*;
26
27 import java.io.*;
28 import java.util.*;
29
30
31 /**
32  * ProGuard plug-in for the J2ME Wireless Toolkit.
33  * <p>
34  * In order to integrate this plug-in in the toolkit, you'll have to put the
35  * following lines in the file
36  * {j2mewtk.dir}<code>/wtklib/Linux/ktools.properties</code> or
37  * {j2mewtk.dir}<code>\wtklib\Windows\ktools.properties</code> (whichever is
38  * applicable).
39  * <p>
40  * <pre>
41  * obfuscator.runner.class.name: proguard.wtk.ProGuardObfuscator
42  * obfuscator.runner.classpath: /usr/local/java/proguard1.6/lib/proguard.jar
43  * </pre>
44  * Please make sure the class path is set correctly for your system.
45  *
46  * @author Eric Lafortune
47  */

48 public class ProGuardObfuscator implements Obfuscator
49 {
50     private static final String JavaDoc DEFAULT_CONFIGURATION = "default.pro";
51
52
53     // Implementations for Obfuscator.
54

55     public void createScriptFile(File jadFile,
56                                  File projectDir)
57     {
58         // We don't really need to create a script file;
59
// we'll just fill out all options in the run method.
60
}
61
62
63     public void run(File obfuscatedJarFile,
64                     String JavaDoc wtkBinDir,
65                     String JavaDoc wtkLibDir,
66                     String JavaDoc jarFileName,
67                     String JavaDoc projectDirName,
68                     String JavaDoc classPath,
69                     String JavaDoc emptyAPI)
70     throws IOException
71     {
72         // Create the ProGuard configuration.
73
Configuration configuration = new Configuration();
74
75         // Parse the default configuration file.
76
ConfigurationParser parser = new ConfigurationParser(this.getClass().getResource(DEFAULT_CONFIGURATION));
77
78         try
79         {
80             parser.parse(configuration);
81
82             // Fill out the library class path.
83
configuration.libraryJars = classPath(classPath);
84
85             // Fill out the program class path (input and output).
86
configuration.programJars = new ClassPath();
87             configuration.programJars.add(new ClassPathEntry(new File(jarFileName), false));
88             configuration.programJars.add(new ClassPathEntry(obfuscatedJarFile, true));
89
90             // The preverify tool seems to unpack the resulting classes,
91
// so we must not use mixed-case class names on Windows.
92
configuration.useMixedCaseClassNames =
93                 !System.getProperty("os.name").regionMatches(true, 0, "windows", 0, 7);
94
95             // Run ProGuard with these options.
96
ProGuard proGuard = new ProGuard(configuration);
97             proGuard.execute();
98
99         }
100         catch (ParseException ex)
101         {
102             throw new IOException(ex.getMessage());
103         }
104         finally
105         {
106             parser.close();
107         }
108     }
109
110
111     /**
112      * Converts the given class path String into a ClassPath object.
113      */

114     private ClassPath classPath(String JavaDoc classPathString)
115     {
116         ClassPath classPath = new ClassPath();
117
118         String JavaDoc separator = System.getProperty("path.separator");
119
120         int index = 0;
121         while (index < classPathString.length())
122         {
123             // Find the next separator, or the end of the String.
124
int next_index = classPathString.indexOf(separator, index);
125             if (next_index < 0)
126             {
127                 next_index = classPathString.length();
128             }
129
130             // Create and add the found class path entry.
131
ClassPathEntry classPathEntry =
132                 new ClassPathEntry(new File(classPathString.substring(index, next_index)),
133                                    false);
134
135             classPath.add(classPathEntry);
136
137             // Continue after the separator.
138
index = next_index + 1;
139         }
140
141         return classPath;
142     }
143 }
144
Popular Tags