• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 import org.apache.bcel.classfile.ClassParser;
20 import org.apache.bcel.classfile.Constant;
21 import org.apache.bcel.classfile.ConstantUtf8;
22 import org.apache.bcel.classfile.JavaClass;
23 
24 /**
25  * Patch all Utf8 constants in the given class file <em>file</em>.class
26  * and save the result in _<em>file</em>.class.
27  *
28  * Usage: patch <oldstring> <newstring> files
29  *
30  * @version $Id$
31  */
32 public class patchclass {
33 
main(String[] argv)34     public static void main(String[] argv) throws Exception {
35         String[] file_name = new String[argv.length];
36         int files = 0;
37 
38         if (argv.length < 3) {
39             System.err.println("Usage: patch <oldstring> <newstring> file1.class ...");
40             System.exit(-1);
41         }
42 
43         for (int i = 2; i < argv.length; i++) {
44             file_name[files++] = argv[i];
45         }
46 
47         for (int i = 0; i < files; i++) {
48             ClassParser parser = new ClassParser(file_name[i]);
49             JavaClass java_class = parser.parse();
50 
51             patchIt(argv[0], argv[1], java_class.getConstantPool().getConstantPool());
52 
53             // Dump the changed class to a new file
54             java_class.dump("_" + file_name[i]);
55             System.out.println("Results saved in: _" + file_name[i]);
56         }
57     }
58 
59     /*
60      * Replace all occurences of string "<em>old</em>" with
61      * "<em>replacement</em>" in all Utf8 constants
62      */
patchIt(String old, String replacement, Constant[] constant_pool)63     private static void patchIt(String old, String replacement, Constant[] constant_pool) {
64         ConstantUtf8 c;
65         String str;
66         int index, old_index;
67         StringBuffer buf;
68 
69         // Loop through constant pool
70         for (short i = 0; i < constant_pool.length; i++) {
71             if (constant_pool[i] instanceof ConstantUtf8) { // Utf8 string found
72                 try {
73                     c = (ConstantUtf8) constant_pool[i]; // Get the string
74                     str = c.getBytes();
75 
76                     if ((index = str.indexOf(old)) != -1) { // `old' found in str
77                         buf = new StringBuffer();           // target buffer
78                         old_index = 0;                      // String start offset
79 
80                         // While we have something to replace
81                         while ((index = str.indexOf(old, old_index)) != -1) {
82                             buf.append(str.substring(old_index, index)); // append prefix
83                             buf.append(replacement);                     // append `replacement'
84 
85                             old_index = index + old.length(); // Skip `old'.length chars
86                         }
87 
88                         buf.append(str.substring(old_index)); // append rest of string
89                         str = buf.toString();
90 
91                         // Finally push the new string back to the constant pool
92                         c = new ConstantUtf8(str);
93                         constant_pool[i] = c;
94                     }
95                 } catch (StringIndexOutOfBoundsException e) { // Should not occur
96                     System.err.println(e);
97                 }
98             }
99         }
100     }
101 }
102