📄 annotationsattribute.java
字号:
int annotationArray(int pos, int num) throws Exception { for (int i = 0; i < num; ++i) pos = annotation(pos); return pos; } final int annotation(int pos) throws Exception { int type = ByteArray.readU16bit(info, pos); int numPairs = ByteArray.readU16bit(info, pos + 2); return annotation(pos + 4, type, numPairs); } int annotation(int pos, int type, int numPairs) throws Exception { for (int j = 0; j < numPairs; ++j) pos = memberValuePair(pos); return pos; } final int memberValuePair(int pos) throws Exception { int nameIndex = ByteArray.readU16bit(info, pos); return memberValuePair(pos + 2, nameIndex); } int memberValuePair(int pos, int nameIndex) throws Exception { return memberValue(pos); } final int memberValue(int pos) throws Exception { int tag = info[pos] & 0xff; if (tag == 'e') { int typeNameIndex = ByteArray.readU16bit(info, pos + 1); int constNameIndex = ByteArray.readU16bit(info, pos + 3); enumMemberValue(typeNameIndex, constNameIndex); return pos + 5; } else if (tag == 'c') { int index = ByteArray.readU16bit(info, pos + 1); classMemberValue(index); return pos + 3; } else if (tag == '@') return annotationMemberValue(pos + 1); else if (tag == '[') { int num = ByteArray.readU16bit(info, pos + 1); return arrayMemberValue(pos + 3, num); } else { // primitive types or String. int index = ByteArray.readU16bit(info, pos + 1); constValueMember(tag, index); return pos + 3; } } void constValueMember(int tag, int index) throws Exception {} void enumMemberValue(int typeNameIndex, int constNameIndex) throws Exception { } void classMemberValue(int index) throws Exception {} int annotationMemberValue(int pos) throws Exception { return annotation(pos); } int arrayMemberValue(int pos, int num) throws Exception { for (int i = 0; i < num; ++i) { pos = memberValue(pos); } return pos; } } static class Copier extends Walker { ByteArrayOutputStream output; AnnotationsWriter writer; ConstPool srcPool, destPool; Map classnames; /** * Constructs a copier. This copier renames some class names * into the new names specified by <code>map</code> when it copies * an annotation attribute. * * @param info the source attribute. * @param src the constant pool of the source class. * @param dest the constant pool of the destination class. * @param map pairs of replaced and substituted class names. * It can be null. */ Copier(byte[] info, ConstPool src, ConstPool dest, Map map) { super(info); output = new ByteArrayOutputStream(); writer = new AnnotationsWriter(output, dest); srcPool = src; destPool = dest; classnames = map; } byte[] close() throws IOException { writer.close(); return output.toByteArray(); } void parameters(int numParam, int pos) throws Exception { writer.numParameters(numParam); super.parameters(numParam, pos); } int annotationArray(int pos, int num) throws Exception { writer.numAnnotations(num); return super.annotationArray(pos, num); } int annotation(int pos, int type, int numPairs) throws Exception { writer.annotation(copy(type), numPairs); return super.annotation(pos, type, numPairs); } int memberValuePair(int pos, int nameIndex) throws Exception { writer.memberValuePair(copy(nameIndex)); return super.memberValuePair(pos, nameIndex); } void constValueMember(int tag, int index) throws Exception { writer.constValueIndex(tag, copy(index)); super.constValueMember(tag, index); } void enumMemberValue(int typeNameIndex, int constNameIndex) throws Exception { writer.enumConstValue(copy(typeNameIndex), copy(constNameIndex)); super.enumMemberValue(typeNameIndex, constNameIndex); } void classMemberValue(int index) throws Exception { writer.classInfoIndex(copy(index)); super.classMemberValue(index); } int annotationMemberValue(int pos) throws Exception { writer.annotationValue(); return super.annotationMemberValue(pos); } int arrayMemberValue(int pos, int num) throws Exception { writer.arrayValue(num); return super.arrayMemberValue(pos, num); } /** * Copies a constant pool entry into the destination constant pool * and returns the index of the copied entry. * * @param srcIndex the index of the copied entry into the source * constant pool. * @return the index of the copied item into the destination * constant pool. */ int copy(int srcIndex) { return srcPool.copy(srcIndex, destPool, classnames); } } static class Parser extends Walker { ConstPool pool; Annotation[][] allParams; // all parameters Annotation[] allAnno; // all annotations Annotation currentAnno; // current annotation MemberValue currentMember; // current member /** * Constructs a parser. This parser constructs a parse tree of * the annotations. * * @param info the attribute. * @param src the constant pool. */ Parser(byte[] info, ConstPool cp) { super(info); pool = cp; } Annotation[][] parseParameters() throws Exception { parameters(); return allParams; } Annotation[] parseAnnotations() throws Exception { annotationArray(); return allAnno; } MemberValue parseMemberValue() throws Exception { memberValue(0); return currentMember; } void parameters(int numParam, int pos) throws Exception { Annotation[][] params = new Annotation[numParam][]; for (int i = 0; i < numParam; ++i) { pos = annotationArray(pos); params[i] = allAnno; } allParams = params; } int annotationArray(int pos, int num) throws Exception { Annotation[] array = new Annotation[num]; for (int i = 0; i < num; ++i) { pos = annotation(pos); array[i] = currentAnno; } allAnno = array; return pos; } int annotation(int pos, int type, int numPairs) throws Exception { currentAnno = new Annotation(type, pool); return super.annotation(pos, type, numPairs); } int memberValuePair(int pos, int nameIndex) throws Exception { pos = super.memberValuePair(pos, nameIndex); currentAnno.addMemberValue(nameIndex, currentMember); return pos; } void constValueMember(int tag, int index) throws Exception { MemberValue m; ConstPool cp = pool; switch (tag) { case 'B' : m = new ByteMemberValue(index, cp); break; case 'C' : m = new CharMemberValue(index, cp); break; case 'D' : m = new DoubleMemberValue(index, cp); break; case 'F' : m = new FloatMemberValue(index, cp); break; case 'I' : m = new IntegerMemberValue(index, cp); break; case 'J' : m = new LongMemberValue(index, cp); break; case 'S' : m = new ShortMemberValue(index, cp); break; case 'Z' : m = new BooleanMemberValue(index, cp); break; case 's' : m = new StringMemberValue(index, cp); break; default : throw new RuntimeException("unknown tag:" + tag); } currentMember = m; super.constValueMember(tag, index); } void enumMemberValue(int typeNameIndex, int constNameIndex) throws Exception { currentMember = new EnumMemberValue(typeNameIndex, constNameIndex, pool); super.enumMemberValue(typeNameIndex, constNameIndex); } void classMemberValue(int index) throws Exception { currentMember = new ClassMemberValue(index, pool); super.classMemberValue(index); } int annotationMemberValue(int pos) throws Exception { Annotation anno = currentAnno; pos = super.annotationMemberValue(pos); currentMember = new AnnotationMemberValue(currentAnno, pool); currentAnno = anno; return pos; } int arrayMemberValue(int pos, int num) throws Exception { ArrayMemberValue amv = new ArrayMemberValue(pool); MemberValue[] elements = new MemberValue[num]; for (int i = 0; i < num; ++i) { pos = memberValue(pos); elements[i] = currentMember; } amv.setValue(elements); currentMember = amv; return pos; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -