1 package org.ozoneDB.tools.OPP; 9 10 import java.io.PrintWriter ; 11 import java.util.Set ; 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 27 class ImplManipulator { 28 protected ClassLoader loader; 29 protected PrintWriter out; 30 protected String outputDir; 31 protected MessageWriter genListener; 32 33 public ImplManipulator(String _outputDir, MessageWriter _genListener, ClassLoader _loader) throws Exception { 34 loader = _loader; 35 outputDir = _outputDir; 36 genListener = _genListener; 37 } 38 39 protected String slashedClassName(String className) { 40 StringBuffer sb = new StringBuffer (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 64 public void changeClassFile(Set classes, String fileName, String newClassName) throws Exception { 65 String 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 name = jcl.getClassName(); 70 genListener.info("class name: " + name); 71 genListener.info("new class name: " + newClassName); 72 73 ClassGen cg = new ClassGen(jcl); 75 ConstantPoolGen cpg = new ConstantPoolGen(jcl.getConstantPool()); 76 77 78 79 83 int i = cpg.lookupClass(slashedClassName(name)); 84 if (i == -1) { 85 throw new Exception ("Unable to lookup class name in class."); 86 } 87 88 89 90 92 ConstantClass cc = (ConstantClass) cpg.getConstant(i); 93 int x = cc.getNameIndex(); 94 95 97 99 ((ConstantUtf8) cpg.getConstant(x)).setBytes(slashedClassName(newClassName)); 100 101 103 104 105 cc.setNameIndex(cpg.addUtf8(slashedClassName(newClassName))); 106 107 109 110 111 113 if (classes != null) { 114 genListener.info(" checking super class... "); 115 String sName = jcl.getSuperclassName(); 116 122 if (classes.contains(sName) || Class.forName("org.ozoneDB.OzoneProxy", true, loader).isAssignableFrom(Class.forName( 123 sName, true, loader))) { 124 String newSClassName = sName + ObjectContainer.IMPLNAME_POSTFIX; 125 genListener.info(sName + " changed to " + newSClassName); 126 i = cpg.lookupClass(slashedClassName(sName)); 128 cc = (ConstantClass) cpg.getConstant(i); 130 x = cc.getNameIndex(); 131 ((ConstantUtf8) cpg.getConstant(x)).setBytes(slashedClassName(newSClassName)); 134 cc.setNameIndex(cpg.addUtf8(slashedClassName(newSClassName))); 136 } else { 138 genListener.info("nothing changed"); 139 } 140 } 141 142 143 144 146 jcl.dump(newFileName); 147 } 148 } | Popular Tags |