下面列出了org.objectweb.asm.ClassVisitor#visitField ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Void visitVariable(VariableElement e, ClassVisitor classVisitor) {
if (e.getModifiers().contains(Modifier.PRIVATE)) {
return null;
}
FieldVisitor fieldVisitor =
classVisitor.visitField(
accessFlagsUtils.getAccessFlags(e),
e.getSimpleName().toString(),
descriptorFactory.getDescriptor(e),
signatureFactory.getSignature(e),
e.getConstantValue());
visitAnnotations(e, fieldVisitor::visitAnnotation);
fieldVisitor.visitEnd();
return null;
}
@Override
public void write(ClassVisitor file) {
FieldVisitor fieldVisitor = file.visitField(modifiers, fieldDescriptor.getName(), fieldDescriptor.getType(), signature, null);
for(AnnotationCreatorImpl annotation : annotations) {
AnnotationVisitor av = fieldVisitor.visitAnnotation(DescriptorUtils.extToInt(annotation.getAnnotationType()), annotation.getRetentionPolicy() == RetentionPolicy.RUNTIME);
for(Map.Entry<String, Object> e : annotation.getValues().entrySet()) {
AnnotationUtils.visitAnnotationValue(av, e.getKey(), e.getValue());
}
av.visitEnd();
}
fieldVisitor.visitEnd();
}
public void accept(ClassVisitor visitor)
{
String[] ints = interfaces.getInterfaces().stream().map(i -> i.getName()).toArray(String[]::new);
visitor.visit(version, access, name.getName(), null, super_class.getName(), ints);
visitor.visitSource(source, null);
for (Annotation annotation : annotations.getAnnotations())
{
AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true);
annotation.accept(av);
}
for (Field field : fields)
{
FieldVisitor fv = visitor.visitField(field.getAccessFlags(), field.getName(), field.getType().toString(), null, field.getValue());
field.accept(fv);
}
for (Method method : methods)
{
String[] exceptions = method.getExceptions().getExceptions().stream().map(cl -> cl.getName()).toArray(String[]::new);
if (exceptions.length == 0)
{
exceptions = null;
}
MethodVisitor mv = visitor.visitMethod(method.getAccessFlags(), method.getName(), method.getDescriptor().toString(), null, exceptions);
method.accept(mv);
}
visitor.visitEnd();
}
static void addIsServlet3(ClassVisitor cw) {
FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, "$$isServlet3", "Z", null, null);
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "$$isServlet3", "()Z", null, null);
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/NoSuchMethodException");
mv.visitLabel(l0);
mv.visitLineNumber(446, l0);
mv.visitLdcInsn(Type.getType("Ljavax/servlet/ServletRequest;"));
mv.visitLdcInsn("startAsync");
mv.visitInsn(ICONST_0);
mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getMethod",
"(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
mv.visitInsn(POP);
mv.visitLabel(l1);
mv.visitLineNumber(447, l1);
mv.visitInsn(ICONST_1);
mv.visitInsn(IRETURN);
mv.visitLabel(l2);
mv.visitLineNumber(448, l2);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/NoSuchMethodException" });
mv.visitVarInsn(ASTORE, 0);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitLineNumber(449, l3);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitLocalVariable("e", "Ljava/lang/NoSuchMethodException;", null, l3, l4, 0);
mv.visitMaxs(3, 1);
mv.visitEnd();
}
public static void main(String[] args) throws Exception {
ClassWriter cw = new ClassWriter(0);
TraceClassVisitor tracer =
new TraceClassVisitor(cw, new PrintWriter(System.out));
ClassVisitor cv = tracer;
String name = "Nerd";
String generics = null;
String superName = "java/lang/Object";
String[] interfaces = null;
int access = ACC_PUBLIC + ACC_INTERFACE;
int version = V1_5;
cv.visit(version, access, name, generics, superName, interfaces);
int fieldAccess = ACC_PUBLIC + ACC_FINAL + ACC_STATIC;
String shortDescriptor = Type.SHORT_TYPE.getDescriptor();
FieldVisitor hair = cv.visitField(fieldAccess, "hair", shortDescriptor,
null, new Integer(0));
hair.visitEnd();
MethodVisitor playVideoGame =
cv.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "playVideoGame",
"()V", null, null);
// no code to define, just finish it up
playVideoGame.visitEnd();
cv.visitEnd(); // prints if using tracer
byte[] b = cw.toByteArray();
// can define or write to file:
// defineClass(name, b, 0, b.length)
// from findClass() in subclass of ClassLoader
FileOutputStream fos = new FileOutputStream("Nerd.class");
fos.write(b);
fos.close();
}
public static void main(String[] args) throws Exception {
ClassWriter cw = new ClassWriter(0);
TraceClassVisitor tracer =
new TraceClassVisitor(cw, new PrintWriter(System.out));
ClassVisitor cv = tracer;
String name = "Nerd";
String generics = null;
String superName = "java/lang/Object";
String[] interfaces = null;
int access = ACC_PUBLIC + ACC_INTERFACE;
int version = V1_5;
cv.visit(version, access, name, generics, superName, interfaces);
int fieldAccess = ACC_PUBLIC + ACC_FINAL + ACC_STATIC;
String shortDescriptor = Type.SHORT_TYPE.getDescriptor();
FieldVisitor hair = cv.visitField(fieldAccess, "hair", shortDescriptor,
null, new Integer(0));
hair.visitEnd();
MethodVisitor playVideoGame =
cv.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "playVideoGame",
"()V", null, null);
// no code to define, just finish it up
playVideoGame.visitEnd();
cv.visitEnd(); // prints if using tracer
byte[] b = cw.toByteArray();
// can define or write to file:
// defineClass(name, b, 0, b.length)
// from findClass() in subclass of ClassLoader
FileOutputStream fos = new FileOutputStream("Nerd.class");
fos.write(b);
fos.close();
}
private static void implementInstanceFields(ClassVisitor classVisitor) {
classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL,
"waitStrategy",
Type.getDescriptor(WaitStrategy.class),
null,
null);
}
public FieldVisitor visitField(ClassVisitor visitor, int access, Object value) {
return visitor.visitField(access, s_name, s_desc, null, value);
}
public void add(ClassVisitor cv) {
_log.info("Adding field " + _fn.name);
cv.visitField(_fn.access, _fn.name, _fn.desc, _fn.signature, _fn.value);
//_fn.accept(cv);
}
public void generate(ClassVisitor cw) {
FieldVisitor fv = cw.visitField(modifiers, name, type.getJVMType().getDescriptor(), null, null);
generateAnnotations(fv);
fv.visitEnd();
}
public FieldVisitor visitField(ClassVisitor visitor, int access, Object value) {
return visitor.visitField(access, s_name, s_desc, null, value);
}