ASM 的话相对复杂一些,参考代码:
下面这个是用 ASM 工具为 Student 类添加一个 public String 类型的 address 属性:
1,需要添加属性的原始类:Student.java
Java code?
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.java
Java code?
import 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.java
Java code?
import 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
}
}
所学知识有限,我也是新手,但是据我所知是不能动态添加字段和方法的。但是我们可以用继承,建立一个子类,用子类来扩展父类的字段,重写父类的方法,也可以添加子类特有的方法。
也可以用has-a的形式,用一个类包涵另一个类的形式,为原来的类增加功能方法和字段。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)