KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > ImplManipulator


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8
package org.ozoneDB.tools.OPP;
9
10 import java.io.PrintWriter JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import de.fub.bytecode.classfile.*;
14 import de.fub.bytecode.generic.ClassGen;
15 import de.fub.bytecode.generic.ConstantPoolGen;
16 import org.ozoneDB.core.ObjectContainer;
17 import org.ozoneDB.tools.OPP.message.MessageWriter;
18
19 /**
20  * Helper class that is responsible for all operations that are done on the
21  * byte code of class files.
22  *
23  *
24  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
25  * @version $Revision$Date$
26  */

27 class ImplManipulator {
28     protected ClassLoader JavaDoc loader;
29     protected PrintWriter JavaDoc out;
30     protected String JavaDoc outputDir;
31     protected MessageWriter genListener;
32
33     public ImplManipulator(String JavaDoc _outputDir, MessageWriter _genListener, ClassLoader JavaDoc _loader) throws Exception JavaDoc {
34         loader = _loader;
35         outputDir = _outputDir;
36         genListener = _genListener;
37     }
38
39     protected String JavaDoc slashedClassName(String JavaDoc className) {
40         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(className);
41         for (int i = 0; i < sb.length(); i++) {
42             if (sb.charAt(i) == '.') {
43                 sb.setCharAt(i, '/');
44             }
45         }
46         return sb.toString();
47     }
48
49     /**
50
51      * Renames the class and, (if classes != null) checks if the superclass
52
53      * needs to be renamed too.
54
55      * @param classes
56
57      * @param fileName
58
59      * @param newClassName
60
61      * @throws Exception
62
63      */

64     public void changeClassFile(Set JavaDoc classes, String JavaDoc fileName, String JavaDoc newClassName) throws Exception JavaDoc {
65         String JavaDoc newFileName = outputDir + OPPHelper.classFileBasename(newClassName) + ".class";
66         genListener.info(" creating " + newFileName + " from " + fileName + " ...");
67         ClassParser parser = new ClassParser(fileName);
68         JavaClass jcl = parser.parse();
69         String JavaDoc name = jcl.getClassName();
70         genListener.info("class name: " + name);
71         genListener.info("new class name: " + newClassName);
72
73         // im Constantpool den Eintrag mit dem Classennamen ?berschreiben
74
ClassGen cg = new ClassGen(jcl);
75         ConstantPoolGen cpg = new ConstantPoolGen(jcl.getConstantPool());
76
77
78
79         // for (int i=0; i<cpg.getSize(); i++) {
80
// System.out.println (cpg.getConstant (i));
81
// }
82

83         int i = cpg.lookupClass(slashedClassName(name));
84         if (i == -1) {
85             throw new Exception JavaDoc("Unable to lookup class name in class.");
86         }
87
88
89
90         // System.out.println ("ClassIndex: "+i);
91

92         ConstantClass cc = (ConstantClass) cpg.getConstant(i);
93         int x = cc.getNameIndex();
94
95         // System.out.println ("ClassConstant.nameIndex: "+x);
96

97         // System.out.println ("\nAlter Klassenname: "+((ConstantUtf8)cpg.getConstant (x)).getBytes ());
98

99         ((ConstantUtf8) cpg.getConstant(x)).setBytes(slashedClassName(newClassName));
100
101         // System.out.println ("Neuer Klassenname: "+((ConstantUtf8)cpg.getConstant (x)).getBytes ()+"\n");
102

103
104
105         cc.setNameIndex(cpg.addUtf8(slashedClassName(newClassName)));
106
107         // System.out.println ("ClassConstant.nameIndex: (new) "+cc.getNameIndex ());
108

109
110
111         // check super class name and change this too, if needed
112

113         if (classes != null) {
114             genListener.info(" checking super class... ");
115             String JavaDoc sName = jcl.getSuperclassName();
116             // System.out.println ("superclassname: " + name);
117
// change super class name if the <code>classes</code> set contains
118
// the name (so the super class *will* change too) or the super
119
// class is an proxy already (the super class has already be
120
// changed)
121

122             if (classes.contains(sName) || Class.forName("org.ozoneDB.OzoneProxy", true, loader).isAssignableFrom(Class.forName(
123                     sName, true, loader))) {
124                 String JavaDoc newSClassName = sName + ObjectContainer.IMPLNAME_POSTFIX;
125                 genListener.info(sName + " changed to " + newSClassName);
126                 // im Constantpool den Eintrag mit dem Superclassennamen ?berschreiben
127
i = cpg.lookupClass(slashedClassName(sName));
128                 // System.out.println ("ClassIndex: "+i);
129
cc = (ConstantClass) cpg.getConstant(i);
130                 x = cc.getNameIndex();
131                 // System.out.println ("ClassConstant.nameIndex: "+x);
132
// System.out.println ("\nAlter Klassenname: "+((ConstantUtf8)cpg.getConstant (x)).getBytes ());
133
((ConstantUtf8) cpg.getConstant(x)).setBytes(slashedClassName(newSClassName));
134                 // System.out.println ("Neuer Klassenname: "+((ConstantUtf8)cpg.getConstant (x)).getBytes ()+"\n");
135
cc.setNameIndex(cpg.addUtf8(slashedClassName(newSClassName)));
136                 // System.out.println ("ClassConstant.nameIndex: (new) "+cc.getNameIndex ());
137
} else {
138                 genListener.info("nothing changed");
139             }
140         }
141
142
143
144         // System.out.println ("new File: "+newFileName);
145

146         jcl.dump(newFileName);
147     }
148 }
Popular Tags