1 24 package org.objectweb.speedo.tools; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 31 import org.objectweb.asm.Attribute; 32 import org.objectweb.asm.ClassAdapter; 33 import org.objectweb.asm.ClassReader; 34 import org.objectweb.asm.ClassVisitor; 35 import org.objectweb.asm.ClassWriter; 36 import org.objectweb.asm.Constants; 37 import org.objectweb.asm.attrs.Attributes; 38 39 45 public class ClassFieldModifier extends ClassAdapter { 46 47 private String className; 48 49 52 private String fieldName; 53 54 57 private String fieldValue; 58 59 62 private boolean fieldFound; 63 64 private boolean classModified = false; 65 66 70 public ClassFieldModifier(final ClassVisitor cv, 71 final String fieldName, 72 final String fieldValue) { 73 super(cv); 74 this.fieldName = fieldName; 75 this.fieldValue = fieldValue; 76 fieldFound = false; 77 } 78 79 public void visit(final int version, final int access, 80 final String name, 81 final String superName, 82 final String [] interfaces, 83 final String sourceFile) { 84 className = name; 85 super.visit(version, access, name, superName, interfaces, sourceFile); 86 } 87 public void visitField(final int access, 88 final String name, 89 final String desc, 90 final Object value, 91 final Attribute attrs) { 92 Object val = value; 93 if (name.equals(fieldName)) { 94 fieldFound = true; 95 if (value == null || !value.equals(fieldValue)) { 96 val = fieldValue; 97 classModified = true; 98 System.out.println("Set the field '" + fieldName 99 + "' of the class '" + className 100 + "' to '" + val 101 + "' (old value: '" + value + "')."); 102 } 103 } 104 super.visitField(access, name, desc, val, attrs); 105 } 106 107 public void visitEnd() { 108 if (!fieldFound) { 109 classModified = true; 110 super.visitField(Constants.ACC_PUBLIC 111 | Constants.ACC_STATIC 112 | Constants.ACC_FINAL, 113 fieldName, 114 "Ljava/lang/String;", 115 fieldValue, 116 null); 117 System.out.println("Add the field '" + fieldName 118 + "' to the class '" + className 119 + "' with the value '" + fieldValue + "'."); 120 } 121 super.visitEnd(); 122 } 123 124 public boolean isClassModified() { 125 return classModified; 126 } 127 128 public static void main(String [] args) throws IOException { 129 if (args == null || args.length < 3) { 130 usage(); 131 return; 132 } 133 String classFileName = args[0]; 134 String fieldName = args[1]; 135 String fieldValue = args[2]; 136 ClassReader cr; 137 File f = new File (classFileName); 138 FileInputStream fis = new FileInputStream (f); 139 cr = new ClassReader(fis); 140 fis.close(); 141 ClassWriter cw = new ClassWriter(true); 142 ClassFieldModifier cfm = new ClassFieldModifier(cw, fieldName, fieldValue); 143 cr.accept(cfm, Attributes.getDefaultAttributes(), false); 144 if (cfm.isClassModified()) { 145 FileOutputStream fos = new FileOutputStream (f); 146 fos.write(cw.toByteArray()); 147 fos.close(); 148 } 149 } 150 151 public final static void usage() { 152 System.out.println("Usage: <class file> <field name> <field value>"); 153 } 154 } 155 | Popular Tags |