From c43f4a7cde41d04095feb027f33f3ab4da7248bd Mon Sep 17 00:00:00 2001
From: chibash For example,
+ * ClassFile
represents a Java .class
file, which
* consists of a constant pool, methods, fields, and attributes.
- *
+ *
+ *
+ * This code generates a class file
+ * ClassFile cf = new ClassFile(false, "test.Foo", null);
+ * cf.setInterfaces(new String[] { "java.lang.Cloneable" });
+ *
+ * FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I");
+ * f.setAccessFlags(AccessFlag.PUBLIC);
+ * cf.addField(f);
+ *
+ * cf.write(new DataOutputStream(new FileOutputStream("Foo.class")));
+ *
Foo.class
for the following class:
+ *
+ *
+ * package test;
+ * class Foo implements Cloneable {
+ * public int width;
+ * }
+ *
Cosnt_MethodHandle
+ * CONSTANT_MethodHandle
*/
public static final int CONST_MethodHandle = MethodHandleInfo.tag;
+ /**
+ * CONSTANT_MethodHandle
+ */
+ public static final int CONST_MethodType = MethodTypeInfo.tag;
+
+ /**
+ * CONSTANT_MethodHandle
+ */
+ public static final int CONST_InvokeDynamic = InvokeDynamicInfo.tag;
+
/**
* Represents the class using this constant pool table.
*/
diff --git a/src/main/javassist/bytecode/FieldInfo.java b/src/main/javassist/bytecode/FieldInfo.java
index 485df3dd..cbba0a85 100644
--- a/src/main/javassist/bytecode/FieldInfo.java
+++ b/src/main/javassist/bytecode/FieldInfo.java
@@ -25,6 +25,15 @@ import java.util.ArrayList;
/**
* field_info
structure.
*
+ * The following code adds a public field width
+ * of int
type:
+ *
+ * * @see javassist.CtField#getFieldInfo() */ public final class FieldInfo { diff --git a/src/main/javassist/bytecode/MethodInfo.java b/src/main/javassist/bytecode/MethodInfo.java index df436dda..4b8c1c4d 100644 --- a/src/main/javassist/bytecode/MethodInfo.java +++ b/src/main/javassist/bytecode/MethodInfo.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; + import javassist.ClassPool; import javassist.bytecode.stackmap.MapMaker; @@ -30,9 +31,25 @@ import javassist.bytecode.stackmap.MapMaker; * *+ * ClassFile cf = ... + * FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I"); + * f.setAccessFlags(AccessFlag.PUBLIC); + * cf.addField(f); + *
The bytecode sequence of the method is represented
* by a CodeAttribute
object.
+ *
+ *
The following code adds the default constructor to a class:
+ * of int
type:
+ *
* * @see #getCodeAttribute() * @see CodeAttribute + * @see Bytecode * @see javassist.CtMethod#getMethodInfo() * @see javassist.CtConstructor#getMethodInfo() */ -- cgit v1.2.3+ * ClassFile cf = ... + * Bytecode code = new Bytecode(cf.getConstPool()); + * code.addAload(0); + * code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V"); + * code.addReturn(null); + * code.setMaxLocals(1); + * + * MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V"); + * minfo.setCodeAttribute(code.toCodeAttribute()); + * cf.addMethod(minfo); + *