📄 asmcontenthandler.java.old
字号:
* Map of the active {@link Label Label} instances for current method.
*/
protected Map labels;
/**
* Constructs a new {@link ASMContentHandler ASMContentHandler} object.
*
* @param os output stream to write generated class.
* @param computeMax <tt>true</tt> if the maximum stack size and the maximum
* number of local variables must be automatically computed.
* This value is passed to {@link ClassWriter ClassWriter}
* instance.
*/
public ASMContentHandler (OutputStream os, boolean computeMax) {
this.os = os;
this.computeMax = computeMax;
}
/**
* Process notification of the start of an XML element being reached.
*
* @param ns - The Namespace URI, or the empty string if the element has no
* Namespace URI or if Namespace processing is not being performed.
* @param localName - The local name (without prefix), or the empty string if Namespace
* processing is not being performed.
* @param qName - The qualified name (with prefix), or the empty string if
* qualified names are not available.
* @param list - The attributes attached to the element. If there are no
* attributes, it shall be an empty Attributes object.
* @exception SAXException if a parsing error is to be reported
*/
public final void startElement (String ns, String localName, String qName,
Attributes list) throws SAXException {
// the actual element name is either in localName or qName, depending
// on whether the parser is namespace aware
String name = localName;
if (name == null || name.length() < 1) {
name = qName;
}
start(MAP[hashcode(name)], list);
}
/**
* Process notification of the end of an XML element being reached.
*
* @param ns - The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace processing is not
* being performed.
* @param localName - The local name (without prefix), or the empty
* string if Namespace processing is not being performed.
* @param qName - The qualified XML 1.0 name (with prefix), or the
* empty string if qualified names are not available.
*
* @exception SAXException if a parsing error is to be reported
*/
public final void endElement (String ns, String localName, String qName)
throws SAXException {
// the actual element name is either in localName or qName, depending
// on whether the parser is namespace aware
String name = localName;
if (name == null || name.length() < 1) {
name = qName;
}
end(MAP[hashcode(name)]);
}
/**
* Process notification of the end of a document and write generated bytecode
* into output stream.
*
* @exception SAXException if parsing or writing error is to be reported.
*/
public final void endDocument () throws SAXException {
try {
os.write(cw.toByteArray());
} catch (IOException ex) {
throw new SAXException(ex.toString(), ex);
}
}
private void start (int element, Attributes attrs) {
Map vals;
int access;
String owner, name, desc, signature, value;
boolean visible;
Label start, end;
int opcode = element & 0xFF;
switch (element >> 8) {
case CLASS:
int major = Integer.parseInt(attrs.getValue("major"));
int minor = Integer.parseInt(attrs.getValue("minor"));
cw = new ClassWriter(computeMax);
vals = new HashMap();
vals.put("version", new Integer(minor << 16 | major));
vals.put("access", attrs.getValue("access"));
vals.put("name", attrs.getValue("name"));
vals.put("parent", attrs.getValue("parent"));
vals.put("source", attrs.getValue("source"));
vals.put("signature", attrs.getValue("signature"));
vals.put("interfaces", new ArrayList());
push(vals);
break;
case INTERFACE:
((List)((Map)peek()).get("interfaces")).add(attrs.getValue("name"));
break;
case SOURCE:
String file = attrs.getValue("file");
String debug = attrs.getValue("debug");
cw.visitSource(file, debug);
break;
case INNERCLASS:
access = getAccess(attrs.getValue("access"));
name = attrs.getValue("name");
String outerName = attrs.getValue("outerName");
String innerName = attrs.getValue("innerName");
cw.visitInnerClass(name, outerName, innerName, access);
break;
case OUTERCLASS:
owner = attrs.getValue("owner");
name = attrs.getValue("name");
desc = attrs.getValue("desc");
cw.visitOuterClass(owner, name, desc);
break;
case ANNOTATION:
desc = attrs.getValue("desc");
visible = Boolean.valueOf(attrs.getValue("visible")).booleanValue();
Object v = peek();
if (v instanceof ClassVisitor) {
push(((ClassVisitor)v).visitAnnotation(desc, visible));
} else if (v instanceof FieldVisitor) {
push(((FieldVisitor)v).visitAnnotation(desc, visible));
} else if (v instanceof MethodVisitor) {
push(((MethodVisitor)v).visitAnnotation(desc, visible));
}
break;
case ANNOTATION_DEFAULT:
push(((MethodVisitor)peek()).visitAnnotationDefault());
break;
case PARAMETER_ANNOTATION:
int parameter = Integer.parseInt(attrs.getValue("parameter"));
desc = attrs.getValue("desc");
visible = Boolean.valueOf(attrs.getValue("visible")).booleanValue();
push(((MethodVisitor)peek()).visitParameterAnnotation(parameter, desc, visible));
break;
case ANNOTATION_VALUE:
name = attrs.getValue("name");
desc = attrs.getValue("desc");
value = attrs.getValue("value");
((AnnotationVisitor)peek()).visit(name, getValue(desc, value));
break;
case ANNOTATION_VALUE_ENUM:
name = attrs.getValue("name");
desc = attrs.getValue("desc");
value = attrs.getValue("value");
((AnnotationVisitor)peek()).visitEnum(name, desc, value);
break;
case ANNOTATION_VALUE_ANNOTATION:
name = attrs.getValue("name");
desc = attrs.getValue("desc");
push(((AnnotationVisitor)peek()).visitAnnotation(name, desc));
break;
case ANNOTATION_VALUE_ARRAY:
name = attrs.getValue("name");
push(((AnnotationVisitor)peek()).visitArray(name));
break;
case FIELD:
access = getAccess(attrs.getValue("access"));
name = attrs.getValue("name");
signature = attrs.getValue("signature");
desc = attrs.getValue("desc");
Object val = getValue(desc, attrs.getValue("value"));
push(cw.visitField(access, name, desc, signature, val));
break;
case METHOD:
labels = new HashMap();
vals = new HashMap();
vals.put("access", attrs.getValue("access"));
vals.put("name", attrs.getValue("name"));
vals.put("desc", attrs.getValue("desc"));
vals.put("signature", attrs.getValue("signature"));
vals.put("exceptions", new ArrayList());
push(vals);
break;
case EXCEPTION:
((List)((Map)peek()).get("exceptions")).add(attrs.getValue("name"));
break;
case TRYCATCH:
start = getLabel(attrs.getValue("start"));
end = getLabel(attrs.getValue("end"));
Label handler = getLabel(attrs.getValue("handler"));
String type = attrs.getValue("type");
getMethodVisitor().visitTryCatchBlock(start, end, handler, type);
break;
case MAX:
int maxStack = Integer.parseInt(attrs.getValue("maxStack"));
int maxLocals = Integer.parseInt(attrs.getValue("maxLocals"));
getMethodVisitor().visitMaxs(maxStack, maxLocals);
break;
case LOCALVAR:
name = attrs.getValue("name");
desc = attrs.getValue("desc");
signature = attrs.getValue("signature");
start = getLabel(attrs.getValue("start"));
end = getLabel(attrs.getValue("end"));
int var = Integer.parseInt(attrs.getValue("var"));
getMethodVisitor().visitLocalVariable(name, desc, signature, start, end, var);
break;
case LINENUMBER:
int line = Integer.parseInt(attrs.getValue("line"));
start = getLabel(attrs.getValue("start"));
getMethodVisitor().visitLineNumber(line, start);
break;
case INSN:
getMethodVisitor().visitInsn(opcode);
break;
case INT_INSN:
getMethodVisitor().visitIntInsn(opcode, Integer.parseInt(attrs.getValue("value")));
break;
case VAR_INSN:
getMethodVisitor().visitVarInsn(opcode, Integer.parseInt(attrs.getValue("var")));
break;
case TYPE_INSN:
getMethodVisitor().visitTypeInsn(opcode, attrs.getValue("desc"));
break;
case FIELD_INSN:
getMethodVisitor().visitFieldInsn(opcode, attrs.getValue("owner"), attrs.getValue("name"), attrs.getValue("desc"));
break;
case METHOD_INSN:
getMethodVisitor().visitMethodInsn(opcode, attrs.getValue("owner"), attrs.getValue("name"), attrs.getValue("desc"));
break;
case JUMP_INSN:
getMethodVisitor().visitJumpInsn(opcode, getLabel(attrs.getValue("label")));
break;
case LABEL:
getMethodVisitor().visitLabel(getLabel(attrs.getValue("name")));
break;
case TABLELABEL:
vals = (Map)peek();
((List)vals.get("labels")).add(getLabel(attrs.getValue("name")));
if (attrs.getValue("key") != null) {
((List)vals.get("keys")).add(attrs.getValue("key"));
}
break;
case LDC_INSN:
getMethodVisitor().visitLdcInsn(getValue(attrs.getValue("desc"), attrs.getValue("cst")));
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -