001: /*
002: * ElementValueInfo.java
003: *
004: * Created on April 20, 2005, 4:19 PM
005: */
006:
007: package com.yworks.yguard.obf.classfile;
008:
009: import com.yworks.yguard.ParseException;
010: import java.io.DataInput;
011: import java.io.DataOutput;
012: import java.io.IOException;
013:
014: public class ElementValueInfo {
015: protected int u1Tag;
016: protected int u2cpIndex;
017:
018: protected int u2typeNameIndex;
019: protected int u2constNameIndex;
020: protected AnnotationInfo nestedAnnotation;
021: protected ElementValueInfo[] arrayValues;
022:
023: private ElementValueInfo() {
024: }
025:
026: public static ElementValueInfo create(DataInput din)
027: throws IOException {
028: ElementValueInfo evp = new ElementValueInfo();
029: evp.read(din);
030: return evp;
031: }
032:
033: protected void read(DataInput din) throws java.io.IOException {
034: u1Tag = din.readUnsignedByte();
035: switch (u1Tag) {
036: case 'B':
037: case 'C':
038: case 'D':
039: case 'F':
040: case 'I':
041: case 'J':
042: case 'S':
043: case 'Z':
044: case 's':
045: u2cpIndex = din.readUnsignedShort();
046: break;
047: case 'e':
048: u2typeNameIndex = din.readUnsignedShort();
049: u2constNameIndex = din.readUnsignedShort();
050: break;
051: case 'c':
052: u2cpIndex = din.readUnsignedShort();
053: break;
054: case '@':
055: nestedAnnotation = AnnotationInfo.create(din);
056: break;
057: case '[':
058: int count = din.readUnsignedShort();
059: arrayValues = new ElementValueInfo[count];
060: for (int i = 0; i < count; i++) {
061: arrayValues[i] = ElementValueInfo.create(din);
062: }
063: break;
064: default:
065: throw new ParseException(
066: "Unkown tag type in ElementValuePair: " + u1Tag);
067: }
068: }
069:
070: protected void markUtf8RefsInInfo(ConstantPool pool) {
071: switch (u1Tag) {
072: case 'B':
073: case 'C':
074: case 'D':
075: case 'F':
076: case 'I':
077: case 'J':
078: case 'S':
079: case 'Z':
080: case 's':
081: pool.getCpEntry(u2cpIndex).incRefCount();
082: break;
083: case 'e':
084: pool.getCpEntry(u2typeNameIndex).incRefCount();
085: pool.getCpEntry(u2constNameIndex).incRefCount();
086: break;
087: case 'c':
088: pool.getCpEntry(u2cpIndex).incRefCount();
089: break;
090: case '@':
091: nestedAnnotation.markUtf8RefsInInfo(pool);
092: break;
093: case '[':
094: for (int i = 0; i < arrayValues.length; i++) {
095: arrayValues[i].markUtf8RefsInInfo(pool);
096: }
097: break;
098: }
099: }
100:
101: /** Export the representation to a DataOutput stream. */
102: public void write(DataOutput dout) throws java.io.IOException {
103: dout.writeByte(u1Tag);
104: switch (u1Tag) {
105: case 'B':
106: case 'C':
107: case 'D':
108: case 'F':
109: case 'I':
110: case 'J':
111: case 'S':
112: case 'Z':
113: case 's':
114: dout.writeShort(u2cpIndex);
115: break;
116: case 'e':
117: dout.writeShort(u2typeNameIndex);
118: dout.writeShort(u2constNameIndex);
119: break;
120: case 'c':
121: dout.writeShort(u2cpIndex);
122: break;
123: case '@':
124: nestedAnnotation.write(dout);
125: break;
126: case '[':
127: int count = arrayValues.length;
128: dout.writeShort(count);
129: for (int i = 0; i < count; i++) {
130: arrayValues[i].write(dout);
131: }
132: break;
133: }
134: }
135: }
|