📄 classwriter.java
字号:
* The index of the constant pool item that contains the name of the source
* file from which this class was compiled.
*/
private int sourceFile;
/**
* The SourceDebug attribute of this class.
*/
private ByteVector sourceDebug;
/**
* The constant pool item that contains the name of the enclosing class of
* this class.
*/
private int enclosingMethodOwner;
/**
* The constant pool item that contains the name and descriptor of the
* enclosing method of this class.
*/
private int enclosingMethod;
/**
* The runtime visible annotations of this class.
*/
private AnnotationWriter anns;
/**
* The runtime invisible annotations of this class.
*/
private AnnotationWriter ianns;
/**
* The non standard attributes of this class.
*/
private Attribute attrs;
/**
* The number of entries in the InnerClasses attribute.
*/
private int innerClassesCount;
/**
* The InnerClasses attribute.
*/
private ByteVector innerClasses;
/**
* The fields of this class. These fields are stored in a linked list of
* {@link FieldWriter} objects, linked to each other by their
* {@link FieldWriter#next} field. This field stores the first element of
* this list.
*/
FieldWriter firstField;
/**
* The fields of this class. These fields are stored in a linked list of
* {@link FieldWriter} objects, linked to each other by their
* {@link FieldWriter#next} field. This field stores the last element of
* this list.
*/
FieldWriter lastField;
/**
* The methods of this class. These methods are stored in a linked list of
* {@link MethodWriter} objects, linked to each other by their
* {@link MethodWriter#next} field. This field stores the first element of
* this list.
*/
MethodWriter firstMethod;
/**
* The methods of this class. These methods are stored in a linked list of
* {@link MethodWriter} objects, linked to each other by their
* {@link MethodWriter#next} field. This field stores the last element of
* this list.
*/
MethodWriter lastMethod;
/**
* <tt>true</tt> if the maximum stack size and number of local variables
* must be automatically computed.
*/
private boolean computeMaxs;
/**
* <tt>true</tt> if the stack map frames must be recomputed from scratch.
*/
private boolean computeFrames;
/**
* <tt>true</tt> if the stack map tables of this class are invalid. The
* {@link MethodWriter#resizeInstructions} method cannot transform existing
* stack map tables, and so produces potentially invalid classes when it is
* executed. In this case the class is reread and rewritten with the
* {@link #COMPUTE_FRAMES} option (the resizeInstructions method can resize
* stack map tables when this option is used).
*/
boolean invalidFrames;
// ------------------------------------------------------------------------
// Static initializer
// ------------------------------------------------------------------------
/**
* Computes the instruction types of JVM opcodes.
*/
static {
int i;
byte[] b = new byte[220];
String s = "AAAAAAAAAAAAAAAABCKLLDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
+ "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+ "AAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAIIIIIIIIIIIIIIIIDNOAA"
+ "AAAAGGGGGGGHAFBFAAFFAAQPIIJJIIIIIIIIIIIIIIIIII";
for (i = 0; i < b.length; ++i) {
b[i] = (byte) (s.charAt(i) - 'A');
}
TYPE = b;
// code to generate the above string
//
// // SBYTE_INSN instructions
// b[Constants.NEWARRAY] = SBYTE_INSN;
// b[Constants.BIPUSH] = SBYTE_INSN;
//
// // SHORT_INSN instructions
// b[Constants.SIPUSH] = SHORT_INSN;
//
// // (IMPL)VAR_INSN instructions
// b[Constants.RET] = VAR_INSN;
// for (i = Constants.ILOAD; i <= Constants.ALOAD; ++i) {
// b[i] = VAR_INSN;
// }
// for (i = Constants.ISTORE; i <= Constants.ASTORE; ++i) {
// b[i] = VAR_INSN;
// }
// for (i = 26; i <= 45; ++i) { // ILOAD_0 to ALOAD_3
// b[i] = IMPLVAR_INSN;
// }
// for (i = 59; i <= 78; ++i) { // ISTORE_0 to ASTORE_3
// b[i] = IMPLVAR_INSN;
// }
//
// // TYPE_INSN instructions
// b[Constants.NEW] = TYPE_INSN;
// b[Constants.ANEWARRAY] = TYPE_INSN;
// b[Constants.CHECKCAST] = TYPE_INSN;
// b[Constants.INSTANCEOF] = TYPE_INSN;
//
// // (Set)FIELDORMETH_INSN instructions
// for (i = Constants.GETSTATIC; i <= Constants.INVOKESTATIC; ++i) {
// b[i] = FIELDORMETH_INSN;
// }
// b[Constants.INVOKEINTERFACE] = ITFMETH_INSN;
//
// // LABEL(W)_INSN instructions
// for (i = Constants.IFEQ; i <= Constants.JSR; ++i) {
// b[i] = LABEL_INSN;
// }
// b[Constants.IFNULL] = LABEL_INSN;
// b[Constants.IFNONNULL] = LABEL_INSN;
// b[200] = LABELW_INSN; // GOTO_W
// b[201] = LABELW_INSN; // JSR_W
// // temporary opcodes used internally by ASM - see Label and
// MethodWriter
// for (i = 202; i < 220; ++i) {
// b[i] = LABEL_INSN;
// }
//
// // LDC(_W) instructions
// b[Constants.LDC] = LDC_INSN;
// b[19] = LDCW_INSN; // LDC_W
// b[20] = LDCW_INSN; // LDC2_W
//
// // special instructions
// b[Constants.IINC] = IINC_INSN;
// b[Constants.TABLESWITCH] = TABL_INSN;
// b[Constants.LOOKUPSWITCH] = LOOK_INSN;
// b[Constants.MULTIANEWARRAY] = MANA_INSN;
// b[196] = WIDE_INSN; // WIDE
//
// for (i = 0; i < b.length; ++i) {
// System.err.print((char)('A' + b[i]));
// }
// System.err.println();
}
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
/**
* Constructs a new {@link ClassWriter} object.
*
* @param flags option flags that can be used to modify the default behavior
* of this class. See {@link #COMPUTE_MAXS}, {@link #COMPUTE_FRAMES}.
*/
public ClassWriter(final int flags) {
index = 1;
pool = new ByteVector();
items = new Item[256];
threshold = (int) (0.75d * items.length);
key = new Item();
key2 = new Item();
key3 = new Item();
this.computeMaxs = (flags & COMPUTE_MAXS) != 0;
this.computeFrames = (flags & COMPUTE_FRAMES) != 0;
}
/**
* Constructs a new {@link ClassWriter} object and enables optimizations for
* "mostly add" bytecode transformations. These optimizations are the
* following:
*
* <ul> <li>The constant pool from the original class is copied as is in
* the new class, which saves time. New constant pool entries will be added
* at the end if necessary, but unused constant pool entries <i>won't be
* removed</i>.</li> <li>Methods that are not transformed are copied as
* is in the new class, directly from the original class bytecode (i.e.
* without emitting visit events for all the method instructions), which
* saves a <i>lot</i> of time. Untransformed methods are detected by the
* fact that the {@link ClassReader} receives {@link MethodVisitor} objects
* that come from a {@link ClassWriter} (and not from a custom
* {@link ClassAdapter} or any other {@link ClassVisitor} instance).</li>
* </ul>
*
* @param classReader the {@link ClassReader} used to read the original
* class. It will be used to copy the entire constant pool from the
* original class and also to copy other fragments of original
* bytecode where applicable.
* @param flags option flags that can be used to modify the default behavior
* of this class. See {@link #COMPUTE_MAXS}, {@link #COMPUTE_FRAMES}.
*/
public ClassWriter(final ClassReader classReader, final int flags) {
this(flags);
classReader.copyPool(this);
this.cr = classReader;
}
// ------------------------------------------------------------------------
// Implementation of the ClassVisitor interface
// ------------------------------------------------------------------------
public void visit(
final int version,
final int access,
final String name,
final String signature,
final String superName,
final String[] interfaces)
{
this.version = version;
this.access = access;
this.name = newClass(name);
thisName = name;
if (signature != null) {
this.signature = newUTF8(signature);
}
this.superName = superName == null ? 0 : newClass(superName);
if (interfaces != null && interfaces.length > 0) {
interfaceCount = interfaces.length;
this.interfaces = new int[interfaceCount];
for (int i = 0; i < interfaceCount; ++i) {
this.interfaces[i] = newClass(interfaces[i]);
}
}
}
public void visitSource(final String file, final String debug) {
if (file != null) {
sourceFile = newUTF8(file);
}
if (debug != null) {
sourceDebug = new ByteVector().putUTF8(debug);
}
}
public void visitOuterClass(
final String owner,
final String name,
final String desc)
{
enclosingMethodOwner = newClass(owner);
if (name != null && desc != null) {
enclosingMethod = newNameType(name, desc);
}
}
public AnnotationVisitor visitAnnotation(
final String desc,
final boolean visible)
{
ByteVector bv = new ByteVector();
// write type, and reserve space for values count
bv.putShort(newUTF8(desc)).putShort(0);
AnnotationWriter aw = new AnnotationWriter(this, true, bv, bv, 2);
if (visible) {
aw.next = anns;
anns = aw;
} else {
aw.next = ianns;
ianns = aw;
}
return aw;
}
public void visitAttribute(final Attribute attr) {
attr.next = attrs;
attrs = attr;
}
public void visitInnerClass(
final String name,
final String outerName,
final String innerName,
final int access)
{
if (innerClasses == null) {
innerClasses = new ByteVector();
}
++innerClassesCount;
innerClasses.putShort(name == null ? 0 : newClass(name));
innerClasses.putShort(outerName == null ? 0 : newClass(outerName));
innerClasses.putShort(innerName == null ? 0 : newUTF8(innerName));
innerClasses.putShort(access);
}
public FieldVisitor visitField(
final int access,
final String name,
final String desc,
final String signature,
final Object value)
{
return new FieldWriter(this, access, name, desc, signature, value);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -