ASM DEMO
原文:http://caodaoxi.iteye.com/blog/723910
これはASMツールでStudentクラスにpublic Stringタイプのaddress属性を追加します。1属性を追加する必要があるオリジナルクラス:Student.java
これはASMツールでStudentクラスにpublic Stringタイプのaddress属性を追加します。1属性を追加する必要があるオリジナルクラス:Student.java
public class Student {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2,プロパティを追加するアダプター:AddFieldAdapter.javaimport org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
public class AddFieldAdapter extends ClassAdapter {
private int accessModifier;
private String name;
private String desc;
private boolean isFieldPresent;
public AddFieldAdapter(ClassVisitor cv, int accessModifier, String name, String desc) {
super(cv);
this.accessModifier = accessModifier;
this.name = name;
this.desc = desc;
}
@Override
public FieldVisitor visitField(int access, String name, String desc,
String signature, Object value) {
if (name.equals(this.name)) {
isFieldPresent = true;
}
return cv.visitField(access, name, desc, signature, value);
}
@Override
public void visitEnd() {
if (!isFieldPresent) {
FieldVisitor fv = cv.visitField(accessModifier, name, desc, null, null);
if (fv != null) {
fv.visitEnd();
}
}
cv.visitEnd();
}
}
3属性を追加するツールAddField.javaimport java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
public class AddField {
private Class clazz = null;
private ClassReader cr = null;
private ClassWriter cw = null;
private ClassAdapter ca = null;
private File classFile = null;
private final static String CLASS_FILE_SUFFIX = ".class";
public AddField(Class clazz) {
this.clazz = clazz;
}
/**
* public
* @param fieldName
* @param fieldDesc
*/
public void addPublicField(String fieldName, String fieldDesc) {
if(cr == null) {
try {
cr = new ClassReader(clazz.getCanonicalName());
} catch (IOException e) {
e.printStackTrace();
}
cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
}
if(ca == null) {
ca = new AddFieldAdapter(cw, Opcodes.ACC_PUBLIC, fieldName, fieldDesc);
} else {
ca = new AddFieldAdapter(ca, Opcodes.ACC_PUBLIC, fieldName, fieldDesc);
}
}
/**
* .class
*
*/
public void writeByteCode() {
cr.accept(ca, ClassReader.SKIP_DEBUG);
byte[] bys = cw.toByteArray();
OutputStream os = null;
try {
os = new FileOutputStream(getFile());
os.write(bys);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* File
* @return
*/
private File getFile() {
if(classFile == null) {
StringBuffer sb = new StringBuffer();
sb.append(clazz.getResource("/"))
.append(clazz.getCanonicalName().replace(".", File.separator))
.append(CLASS_FILE_SUFFIX);
classFile = new File(sb.substring(6));
}
return classFile;
}
}
4バイトコード処理:PreCompleProcess.javapublic class PreCompileProcess {
public static void main(String[] args) {
// Student
AddField add = new AddField(Student.class);
// address, java.lang.String public
add.addPublicField("address", "Ljava/lang/String;");
// tel, int public
add.addPublicField("tel", "I");
// .class
add.writeByteCode();
}
}
5、テストクラス:Test.javapublic class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.setAge(10);
stu.setName("Tom");
stu.address = "Beijing"; // address
stu.tel = 56; // tel
System.out.println("Name: " + stu.getName());
System.out.println("Age: " + stu.getAge());
System.out.println("Address: " + stu.address);
System.out.println("Tel: " + stu.tel);
}
}
上のコードはASMツールでコンパイルできます。ASMで行けます。http://asm.objectweb.org/ 上からダウンロードします実行順序:javac Student.java javac-cp.lib/asm-all-31.jar ProCompleProcess.java java-cp.lib/asm-all-3.1.jar ProCopileProcess javac Test.java java TestはStudent.javaで修正がない場合、PreCompleProcessは一回だけ実行すればいいです。